[Turn]__declspec (dllexport) and __declspec (dllimport)

Source: Internet
Author: User

__declspec (dllexport)

__declspec (dllexport) has a function known as an export function, meaning that the function is to be called by a program other than her program.
extern "C" instructs the compiler to name the function with the C language method.
Because of the function overloading of C + + when making DLL export functions, the __declspec (dllexport) function (int,int) is decorate in the DLL, for example, decorate becomes function_int_int, and Different compiler decorate methods, resulting in the use of GetProcAddress to obtain a function address inconvenience, the use of extern "C", the above decorate will not occur, because C does not have a function overload, but this is the extern "C" Modified functions do not have the ability to overload, it can be said that extern and extern "C" is not the same thing.

__declspec (dllimport)

I believe that the person who wrote the WIN32 program, did the DLL, will be very clear __declspec (dllexport) role, it is to eliminate in the Def file manually define which functions to export a method. Of course, if your DLL is full of C + + classes, you can't specify the exported function in Def, you can only export the class with __declspec (dllexport). However, the description of __declspec (dllimport) is a bit strange in the MSDN documentation, so let's take a look at what MSDN says:

Code can be compiled correctly without using __declspec (dllimport), but using __declspec (dllimport) allows the compiler to generate better code. The compiler is able to generate better code because it can determine whether a function exists in a DLL, which allows the compiler to generate code that skips the level of indirection, which usually appears in function calls across DLL boundaries. However, you must use __declspec (dllimport) to import the variables used in the DLL.

At first glance, this passage means that the DLL's export library can be used normally without it, but in the last word, you must use __declspec (dllimport) to import the variables used in the DLL.

So let me experiment with it, assuming that you only export a simple class in the DLL, notice that I assume you have defined the Simpledll_export in the project properties
SimpleDLLClass.h

#ifdef simpledll_export#define dll_export __declspec (dllexport) #else # define Dll_export#endifclass dll_export Simpledllclass{public:simpledllclass (); virtual ~simpledllclass (); virtual GetValue () {return m_nvalue;}; Private:int M_nvalue;};

SimpleDLLClass.cpp

#include "SimpleDLLClass.h" Simpledllclass::simpledllclass () {m_nvalue=0;} Simpledllclass::~simpledllclass () {}

Then you use this DLL class, in your app include SimpleDLLClass.h, your app's project does not have to define simpledll_export so, Dll_export will not exist, this time, you in the app, Do not encounter problems. This corresponds to whether the __declspec (dllimport) definition on MSDN is working correctly. But we also did not encounter variables can not be used properly ah. Well, let's change simpledllclass, change its m_nvalue to static, and add a line to the CPP file.

int simpledllclass::m_nvalue=0;

If you don't know why you want to add this line, go back to the basics of C + +. After the change, go to link again, your app, see how the results, the result is link tells you can not find this m_nvalue. Clearly already defined, why did not have it?? It must be because I defined m_nvalue as the reason for the static. But if I must use Singleton's design pattern, then this class must have a static member, every time the link is not, that is not finished? If you have platform SDK, with the inside of the depend program to see, DLL and indeed there is this m_nvalue export AH.
Go back and look at the last sentence of the paragraph I quoted on MSDN. Let's change the SimpleDLLClass.h and change the paragraph to look like this:

#ifdef simpledll_export#define dll_export __declspec (dllexport) #else # define Dll_export __declspec (dllimport) #endif

Again link, everything is normal. The original DllImport is to better handle static member variables in the class, if there is no static member variable, then this __declspec (dllimport) does not matter.

Copyright notice: The above content is from the Internet, non-original, only for learning to use

[Turn]__declspec (dllexport) and __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.