Question

Yes I know there are hundreds of posts about LNK2001 on Stackoverflow already. But NONE of them solved my problem. So I post my solution here.

Symptom: An ATL DLL compiles fine as Debug but fails with the above error compiled as Release.

Stuck for an hour searching in internet and finding usless hints like "Did you define a main() function?" I think that I must publish the solution for my specific problem.

Was it helpful?

Solution

IMPORTANT: LNK2001 can have millions of causes!

If my solution does not help in YOUR specific case and if this page does neither: http://msdn.microsoft.com/en-us/library/f6xx1b1z%28v=vs.71%29.aspx

then be aware to include as many keywords into your search as possible. Like in my case "LIBCMT.lib LNK2001 _main". This is crucial to avoid drowning in millions of search results.

LIBCMT.LIB defines functions like memcpy, swprintf etc, which are the CRT functions. (C Runtime Library)

I use some of these functions in my project. So why does the Debug version build and the Release version does not?

The cause is that the CRT library requires initialization, it has a startup code which must be called once (normally during _main)

If the project defines _ATL_MIN_CRT the CRT startup code is excluded from the Exe,Dll in Release builds to reduce the file size.

Knowing this the solution is simple:

Solution A) Remove _ATL_MIN_CRT from the project settings

Solution B) At the end of the file Stdafx.h put this line:

#undef _ATL_MIN_CRT

That solved the problem.

As I reuse my class in other projects I put at the begin of my class:

#ifdef _ATL_MIN_CRT
    #error _ATL_MIN_CRT is not valid for this project
#endif

This assures that reusing my code in another project in the future I will be warned immediately.

Elmü

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top