DLL Export and Import tips (share a header file with the caller's code file)

Source: Internet
Author: User
We all know that export members use _ declspec (dllexport) and import _ declspec (dllimport). You can use these two modifiers to function, class, and variable in the declaration part of the header file, or the class member function is modified to indicate that the Member is an import member or an export member.

One trick is to use macro definition as the switch control so that the DLL project and the project that calls the DLL share a header file:
1. dllCodeDefine a macro in the source file (CPP file) and then include the header file# DefineDllapi _ declspec (dllexport)
# Include"Header. h"

2. Write this in header file header. h.# Ifdef dllapi
# Else
# DefineDllapi _ declspec (dllimport)
# Endif

3. You can use dllapi to modify the exported or imported headers in header. h.VoidDllapi func (Void);

In this way, during macro replacement, the dllapi will be replaced by the _ declspec (dllexport) bits when the DLL code source file is compiled, but the caller has not defined the dllapi, therefore, it will be replaced with _ declspec (dllimport ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.