Error 2 C2491:XX: the definition of DllImport static data member is not allowed

Source: Internet
Author: User

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.



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.