VS2005 Compiler dll Error, error C2491: ' cmyclass::function ': definition of dllimport Function not allowed

Source: Internet
Author: User

When I write the C + + (except MFC) program under VS2005, I don't like the few subordinate files "stdafx.h" and "stdafx.cpp" that the wizard generates. Because if you put your code on another compiler, you need to change a lot of things. I am now accustomed to using a header file I wrote "Start.h", each time manually introduced.

The same is true today when making DLLs in C + +, generating a project name of MyTest, and then manually defining the introduction of the DLL (this is just an example)

#ifndef MYDLL # ifdef mydll_exports # define MYDLL __declspec (dllexport) # Else # define MYDLL __declspec (dllimport) # End If #endif

Use as follows:

MyTest.h extern MYDLL int nmytest; MYDLL int fnmytest (void); MyTest.cpp MYDLL int nmytest=0; This is a example of an exported function. MYDLL int fnmytest (void) {return 0;}

2 errors were reported at the time of the final compilation.

Error C2491: ' nmytest ': definition of dllimport function not allowed

Error C2491: ' fnmytest ': definition of dllimport function not allowed

Then it was completely silent, and later on MSDN, there was an article about it.

#define MYDLL __declspec (dllimport) is a useful statement when it is found to be predefined.

Then I add a pre-defined above, define MYDLL __declspec (dllexport), this can compile the past, but how to look like the lily. Finally, a C + + auto-generated DLL was introduced to make a comparison, and finally found:

The original is a predefined macro is not set to, I built the project name is MyClass, the system on the custom generated myclass_exports such a macro, and then I manually changed to Mydll_exports to compile successfully.

Here's where the compiler resets the predefined macros, I use the VS2005 English version:

Preprocessor, Project--XX property Pages, C + +;

Change the wizard-generated DLL in the properties of the preprocessor definitions to your own definition.

In this example, the myclass_exports is changed to Mydll_exports.

Summary: VS is really strong enough for you to fix everything. I still ungrateful .... Ha ha

Due to the level of limited, what is the problem, please crossing, thank you.

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.