Today in using VS to make a DLL library, because if in a single-piece mode, a static pointer is declared in the. h file, the variable is defined in the corresponding CPP file. An error occurred while exporting:
Error 2error C2491: "Sdk_functions::p instance_": the definition of DllImport static data member is not allowed
The reason for this error is that I changed the VS Auto-created export macro for me:
The following ifdef blocks are a standard way to create a simpler
//macro from a DLL. All files in this DLL are compiled with cyapi_2013_exports//symbols defined on the command line
.
This symbol should not be defined on//any other projects that use this DLL. In this way, any other item in the source file that contains this file will treat the
//Cyapi_2013_api function as imported from the DLL, which will be
treated as exported with the//symbol defined by this macro.
#ifdef cyapi_exports
#define CYAPI_API __declspec (dllexport)
#else
#define CYAPI_API __declspec ( dllimport)
#endif
As can be seen, I manually removed the corresponding 2013 characters, which led to the Cyapi_exports macro is not defined, thus using the __declspec (dllimport). __declspec (dllexport) is used to export, for me to write the export library of the people used, __declspec (dllimport) is imported, for the use of this library of people.
In MSDN, the code is 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.
and other people want to use the static variables of my library, you must use __declspec (dllimport) to import the variables used in the DLL, so corresponding to it must use __declspec (dllexport) to export the variable. It is not necessary for a general variable.