I am getting this linker error.
mfcs80.lib(dllmodul.obj) : error LNK2005: _DllMain#12 already defined in MSVCRT.lib(dllmain.obj)
Please tell me the correct way of eliminating this bug. I read solution on microsoft support site about this bug but it didnt helped much.
I am using VS 2005 with Platform SDK
I had the same error message, but none of the answers here solved it for me.
So if you Encounter that Problem when creating a DLL Project that uses MFC, it can be resolved by entering the following line:
extern "C" { int _afxForceUSRDLL; }
to the cpp file where DllMain is defined. Then your own DllMain implementation is used, rather than the one from dllmain.obj.
When we try to use MFC library, we surely will include afx.h directly
or indirectly, then MFC(afx.h) tell the linker to find the symbol of
__afxForceUSRDLL and put that object which contains __afxForceUSRDLL into the program, so linker searches and puts dllmodule.obj into our
program, because __afxForceUSRDLL is defined in dllmodule.cpp.
That’s the common scenario. When we want to use our own DllMain in a
mfc dll project, linker complains that there are two DllMain, one in
our code, one in Dllmodule.obj.
So we need to tell the linker to add our dllmain.obj for
__afxForceUSRDLL. So we need to define __afxForceUSRDLL in our own cpp file where our own DllMain is defined, then the linker will ignore
mfc’s dllmodule.obj and see only one DllMain and never complains.
Source: http://social.msdn.microsoft.com/Forums/en-US/0d78aa6b-1e87-4c01-a4a7-691335b7351a/how-to-build-mfc-application-dll-in-visual-c-2010
If you read the linker error thoroughly, and apply some knowledge, you may get there yourself:
The linker links a number of compiled objects and libraries together to get a binary.
Each object/library describes
what symbols it expects to be present in other objects
what symbols it defines
If two objects define the same symbol, you get exactly this linker error. In your case, both mfcs80.lib and MSVCRT.lib define the _DllMain#12 symbol.
Getting rid of the error:
find out which of both libraries you actually need
find out how to tell the linker not to use the other one (using e.g. the tip from James Hopkin)
If you're defining your own DllMain, in your project settings you need to set 'Use of MFC' in the 'Configuration Properties/General' to 'Use Standard Windows Libraries'.
You should do a clean rebuild after changing it.
In my project I was able to solve this problem by adding mfcs80.lib and msvcrt.lib as additional dependencies in the project settings. The 'additional dependencies' can be found under Linker -> Input.
In the debug configuration that would have to be mfcs80d.lib and msvcrtd.lib respectively.
By the way, I am working with Visual Studio 2010, so in my case the MFC lib is called mfc100.lib.
I am not sure why this worked. It is not necessary to add these lib files as additional dependencies because I already set 'Use of MFC' to 'Use MFC in a shared dll'. I guess that by specifying these libraries as additional dependencies they are linked in a different order.
This solution is more or less the same as the one suggested on the Microsoft site: http://support.microsoft.com/kb/148652, except I did not need to type anything in the 'Ignore specific default libraries' box.
For me the direct cause was indeed a missing _afxForceUSRDLL symbol reference, but the indirect cause was a missing _USRDLL macro definition. It is defined by default by the VC wizard, but occasionally devs erase it erroneously.
Here it is in more words.
MSDN knowledge base ID Q148652.
http://support.microsoft.com/kb/148652
Cause:
Visual C++ compiles the source files in alphabetical order, and passes the compiled object files to the linker in alphabetical order.
If the linker processes DLLDATAX.OBJ first, the source code references DllMain, which the linker loads from MSVCRTD.LIB(dllmain.obj).
The linker then processes an object file compiled from a C++ file that contains #include "stdafx.h", which references the symbol
__afxForceUSRDLL, which the linker loads from MFC42D.LIB(dllmodul.obj). This object module also contains an implementation for DllMain,
causing the conflict.
I have a very similar problem. [mfcs110d.lib(dllmodul.obj) : error LNK2005: _DllMain#12 already defined in MSVCRTD.lib(dllmain.obj)] and the solution was add mfcs110d.lib to Additional Dependencies
For all those who are experiencing this error in ATL projects (mostly when trying to add MFC support), here's the solution I found after days of frustration!
First of all, this link was more helpful to me than all the others. It pointed me into the right direction. The problem occurs, if the "generated files" (containing the proxy and stub code, just as the type guids) for some reason have been removed and readded to the project. This causes Visual Studio to add them in the wrong order!
Usually you first come up with the "ATL requires C++ compilation" error, but you may have fixed this by turning out the Yc/Yu (precompiled headers) setting for that file.
What you should do next is unloading your project and edit it. Search for the item groups that define the build and include order (ClCompile and ClInclude). Check their order and settings.
The compiles should appear in this order:
dllmain.cpp (with CompileAsManaged set to false and PrecompiledHeader left empty).
Library source (MyLib.cpp, containing DllCanUnloadNow and so on)
Proxy/Stub code (MyLib_i.c; with same settings as dllmain.cpp)
stdafx.cpp (with PrecompiledHeader set to Create)
All the other library source files (your actual libraries content)
xdlldata.c (with the same settings as dllmain.cpp)
The includes should then be ordered like this:
dllmain.h
MyLib_i.h
Resource.h
stdafx.h
targetver.h
... (actual library headers)
xdlldata.h
Fixing the build order fixed my project and I was able to create a new clean build.
I have personally got rid of this error this way: right-clicked project in the Solution Explorer, selected Properties from pop-up menu, clicked Linker tab and added mfcs71ud.lib into Additional Dependencies. If you're using Visual Studio 2005, it should be "80" instead of "71" and so on.
In my case I had a problem with the preprocessor directives.
For some reason _USRDLL was defined, when it should not have been.
To check this, go to the menu Project , select Project Properties , then select the snippet Configuration Properties --> Preprocessor .
The preprocessor directives will be found there.
Declare the mfc80ud.lib and mfcs80ud.lib in the Additional Dependancies field in the Project Properties -> Linker Tab -> Input of Visual Studio to fix the issue.
Just #undef the _USRDLL before including afx.h, or even better, edit your project configuration and remove the macro.
This is the usual configuration for a MFC extension DLL: Build Settings for an MFC DLL
Make sure you include "Stdafx.h" at the top of each .cpp file. I was getting the exact same error and had a single .cpp file that did not include this header at all. Adding the #include solved the problem.
I found solution Here
Visual Studio 2010 library linking order
this is: /FORCE:MULTIPLE
in linker options
I had to mix ATL and MFC together , to use
[module(name = "mymodule")]; construction in MFC application together with "__hook" keyword
I found this which helped me:
http://support.microsoft.com/kb/148652
Basically the linker order was incorrect. the CRT libs were getting linked before the MFC libs. Turns out, the MFC libs had to get linked FIRST, and then the CRT libs could be linked.
Yucko Microsoft!!
There is a common theme running through some of the answers here.
Avishek Bose:-
Declare the mfc80ud.lib and mfcs80ud.lib in the Additional
Dependancies field in the Project Properties -> Linker Tab -> Input of
Visual Studio to fix the issue.
vmb100:-
I am working with Visual Studio 2010, so in my case the MFC lib is
called mfc100.lib.
joseAndresGomezTovar:-
I have a very similar problem. [mfcs110d.lib(dllmodul.obj) : error
LNK2005: _DllMain#12 already defined in MSVCRTD.lib(dllmain.obj)] and
the solution was add mfcs110d.lib to Additional Dependencies
So the general case seems to be to first find the name of the library to add ...
and then to add it ....
Note that there seem to be some prerequisites and/or alternative solutions.
This can also occur if your solution has multiple projects that export the same symbols. For example if you have sub-project that builds foo.dll with a foo.def file that exports DoFoo and a sub-project for bar.dll with a bar.def file that exports DoFoo, a collision will occur and this is the error you will see when you link.
Related
I am working on a project using c++/windows forms (visual studio 2010), we have 4 projects:
1 project containing GUI windows forms {managed code} and this is the exe project
other 3 projects {non-managed code} and all are static libraries.
in the 4 projects we don`t use precompilied headers stdafx.h , and common language runtime support is the Pure MSIL Common Language Runtime Support (/clr:pure).
every project include the other 3 projects as additional include directories , and link library dependencies set to yes.
We have:
Warning LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library
This warning appeared for the 3 static libraries projects in the same object files (.NETFramework,Version=v4.0.AssemblyAttributes.obj).
We want to eliminate it, but after some search, most topics speak about the precompiled headers to be a reason while we don not use it.
Any new ideas about why this warning exist and how to eliminate it?
Disclaimer: This solution is indeed terrible, and this code should not be added to production build! It is only useful to "hide" the warning.
A better solution would be to remove the file in question from the compilation, as it is anyway useless.
Original post:
I had a the problem with this warning originating from several dependencies, and I found that it was caused by some translation unit being empty, i.e. empty source files.
These files actually had a content but it was deactivated for visual studio so I just added at the beginning:
__declspec( dllexport ) void getRidOfLNK4221(){}
And now my project compiles without any warning :)
Hope it helps, even if this is a late answer!
I had a c++ static library with an empty module in it, linking which in non-optimized mode gave me that LNK4221 warning. (in Visual Studio)
I went to Librarian->All Options, and added "/ignore:4221" to Additional Options.
So, the warning has disappeared.
You get this warning because you use only template classes in cpp file.
To get rid of it, insert one simple function with no template.
See this:
https://learn.microsoft.com/en-us/cpp/error-messages/tool-errors/linker-tools-warning-lnk4221
In summary, the file exports nothing, so linker does not link, as it thinks it's a waste of effort
This was kind of just my particular problem but if your .cpp doesn't define any new functions or has empty bodies for constructor functions, you will get this warning. I used an initialization list for my constructor so the body was empty.
https://devblogs.microsoft.com/cppblog/linker-warning-lnk4221-and-some-tips-to-avoid-it/
In ” Visual Studio 2008 Command Prompt”, enter the following commands (Note: We use command line here to specify the order of .obj files; Visual Studio 2008 will supply linker with .obj files in alphabetical order)
cl /c a.cpp b.cpp
link /lib /out:test.lib a.obj b.obj
And LNK4221 will be thrown for a.obj as the following.
a.obj : warning LNK4221: no public symbols found; archive member will be inaccessible
For the above case, atlbase.h (shipped with Visual Studio) contains some definitions of symbols, which will be included in both a.obj and b.obj. Additionally, there is a function, func1, defined in b.obj. Linker will process OBJ files in Last In First Out manner, so when it is processing a.obj, it cannot find any new public symbols in it because b.obj provide all the public symbols that a.obj has, LNK4221 will be thrown. If command line 2 is replaced with following
link /lib /out:test.lib b.obj a.obj
Now the warning is gone!
I'm using boost in my project. I've downloaded pre-compiled binaries from here http://boost.teeks99.com/
When linking I receive such error:
Error 18 error LNK2005: "public: void __cdecl boost::thread::join(void)" (?join#thread#boost##QEAAXXZ) already defined in boost_thread-vc110-mt-1_52.lib(boost_thread-vc110-mt-1_52.dll) C:\Oleg\projects\MBClient\FastNativeAdapter\libboost_thread-vc110-mt-1_52.lib(thread.obj) FastNativeAdapter
Why boost contains two lib with so similar name, what is the difference between them?
libboost_thread-vc110-mt-1_52.lib
boost_thread-vc110-mt-1_52.lib
How to fix linking error?
upd I've compiled boost myself. I've added boost_1_53_0\stage\lib directory to linker. This directory actually contains 3 "copies" of "each" file, for example:
boost_atomic-vc110-mt-1_53.dll
boost_atomic-vc110-mt-1_53.lib
libboost_atomic-vc110-mt-1_53.lib
So It's clear what compiler claims about. Somehow it can't understand which version of lib file to use. It's likely connected with static/dinamic linking, but I still can not find the solution. I'm sure my problems is pretty common so I hope someone can suggest me what to do.
I've tried to delete all "libboost*" files from folder but then I receive such error:
Error 15 error LNK1104: cannot open file 'libboost_date_time-vc110-mt-1_53.lib'
I've tried to delete all "boost*lib" files from folder but then I receive such error:
Error 15 error LNK1104: cannot open file 'boost_thread-vc110-mt-1_53.lib'
Then I copied boost_thread-vc110-mt-1_53.lib back and I receive a lot of errors like that:
Error 16 error LNK2005: "public: virtual __cdecl boost::detail::thread_data_base::~thread_data_base(void)" (??1thread_data_base#detail#boost##UEAA#XZ) already defined in boost_thread-vc110-mt-1_53.lib(boost_thread-vc110-mt-1_53.dll)
So when there are no boost_thread-vc110-mt-1_53.lib compiler claims that it's missing, when there is boost_thread-vc110-mt-1_53.lib compiler claims that "function is already defined". Probaly somehow I do use dinamic and static linking at the same time or something like that?
upd2 i've uncommented #define BOOST_ALL_DYN_LINK as suggested here and now code compiles! i'm investigating if everything else is fine. however i didn't understand why I should uncomment #define BOOST_ALL_DYN_LINK so comments are welcome.
Edit: Initial statement removed since an edit to the post changed the situation.
Based on http://www.boost.org/doc/libs/1_53_0/more/getting_started/unix-variants.html#library-naming (as provided by Igor R.):
libboost_thread-vc110-mt-1_52.lib is a static lib (no need for the DLL)
boost_thread-vc110-mt-1_52.lib is the import lib for the DLL
You only need to use one of these.
Another idea/solution to try if you hit the error LNK1104: cannot open file 'libboost_date_time-*.lib' error:
In our project, we include the boost/date_time.hpp file. We define the constant BOOST_ALL_NO_LIB instead of BOOST_ALL_DYN_LINK in our project settings to tell boost not to automatically select which libraries to link against.
See the Boost documentation for more information about this option.
So you could add BOOST_ALL_NO_LIB in Project Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions and check whether this linker error goes away.
I think first of all you need to correct your question. Do you mean (I guess you already know the difference between DLL and LIB )
libboost_thread-vc110-mt-1_52.lib
boost_thread-vc110-mt-1_52.dll
or
libboost_thread-vc110-mt-1_52.lib
boost_thread-vc110-mt-1_52.lib
Anyway, it seems the issue is you're mixing static(libboost_thread-vc110-mt-1_52.lib) and shared (boost_thread-vc110-mt-1_52.lib) boost libraries. But without the working environment and platform details I cannot purpose an exact solution. If you work in Visual Studio ,then you can go to right click on project file > properties > linker > input > ignore specific library and add libboost_thread-vc110-mt-1_52.lib there and try.
asking myquestion myself.
need to uncomment #define BOOST_ALL_DYN_LINK (refer to description)
I was advised by some of you not to long ago to use FreeImage as a library for image processing in C++.
I now have some trouble in getting the library to work (still relatively new here).
I've tried loading the various vcxproj and sln tiles and they gave me a blank project. Since there isn't any installation instructions provided for that, I gave up on making it a visual studio solution.
I next tried the old-fashion way of compiling the source code using the Makefile and then adding "FreeImage/Source" to the linker. While the IDE does not raise any red flags when I call functions declared in FreeImage.h, it gave me a bunch of "error LNK2019: unresolved external symbol" during compilation, as if the functions do not exist. What I suspect is that the IDE could not find the .cpp files that define the said functions, but I still get that same problem when I added FreeImage/Source/FreeImage to the linker.
Now when I directly included some of the .cpp files (i.e. Plugin.cpp and FreeImage.cpp) for a test, I get even more unresolved external symbol errors as well as things like "inconsistent dll linkage" for this within... for example FreeImage.cpp:
const char * DLL_CALLCONV
FreeImage_GetVersion() {
static char s_version[16];
sprintf(s_version, "%d.%d.%d", FREEIMAGE_MAJOR_VERSION, FREEIMAGE_MINOR_VERSION, FREEIMAGE_RELEASE_SERIAL);
return s_version;
}
So, I am totally stuck. What am I doing wrong? I felt I've followed the adequate steps in adding library dependencies, such as adding the specific folders that are immediate parents to the relevant .h and .cpp files in C/C++ -> General -> Additional Included Directories and Linker -> General -> Addition Library Directories.
Some help will be greatly appreciated!
Using FreeImage v3.15.3 I had no problems converting the VS2008 project to VS2010.
Also the building worked as expected. But when I linked to the static lib, I got some unresolved externals. First I tried al kinds of tricks setting /MT /MD linking, but that did not solve these linking problem.
After reading Some Newbie's comment I dug into freeimage.h. There I found a macro switch FREEIMAGE_LIB that controls the calling conventions of the function.
Use a #define FREEIMAGE_LIB before including the freeimage.h file. That way you can easily static link to FreeImage.lib
I have inherited a VC++ solution and from one project (named "IntersonWinDriver") I'm trying to link to a .lib file that is generated by a different project (named "Utility"). I noticed that in a different project, named "MasterVu", I found "Utility.lib" included among the linker command line options.
In order to duplicate this for the IntersonWinDriver project, under the properties window I selected Configuration Properties->C/C++->General. I added the folder containing the code for the Utility project under the "Additional Include Directories" option. I then added the Utility project as one of the project dependencies for IntersonWinDriver.
The problem is that when I look at the linker command line options, I don't see any reference to Utility.lib as with the MasterVu project. I could manually add a reference to Utility.lib under Configuration Properties->Linker->Input, but I would rather not. What am I missing here?
As you may guess, I'm getting an unresolved external symbol error due to any reference in IntersonWinDriver to code written under the Utility project.
Hmm...I see that under Common Properties->Framework and References I can add the Utility project. Now I feel silly for asking this question too soon...
At work we have an MFC Extension DLL that built fine with 1.35 but when built with 1.43 causes the following error:
error LNK2005: __pRawDllMain already defined in ApObs.obj
If I activate BOOST_LIB_DIAGNOSTIC the old build lists:
linking to lib file: libboost_thread-vc71-mt-gd-1_35.lib
and
linking to lib file: libboost_thread-vc71-mt-gd-1_43.lib
So no change there with which library is linked
The full error message is:
libboost_thread-vc71-mt-gd-1_43.lib(tss_pe.obj) : error LNK2005: __pRawDllMain already defined in ApObs.obj
I have done a diff on tss_pe.cpp in both library versions and they are identical, so I'm not sure what the problem is.
You can get the message to disappear by defining BOOST_THREAD_USE_DLL but then we would need to ship BOOST_THREAD-VC71-MT-GD-1_43.DLL so I don't think this is the best solution.
There is known incompatibility between MFC and statically linked boost Thread, both trying to hook into DllMain to initialize stuff. This has been introduced in boost 1.37.
From the author of boost::thread:
If you can ensure that
on_process_exit from boost/thread/detail/tss_hooks.hpp is called when
the DLL is unloaded then you can patch libs/thread/src/win32/tss_pe.cpp
to remove the use of _pRawDllMain.
(He recommends to use the DLL version, though.)
There seem to have been efforts to make this patching unnecessary, but apparently to no avail so far...
This happens because of the linking order.
You can change the linking error by manually adding the libs in the Additional Dependencies.
In my case putting libboost_thread-vc71-mt-gd-1_43.lib BEFORE the other conflicting lib solved the problem:
Project -> Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies
libboost_thread-vc80-mt-1_40.lib;mfcs80u.lib;%(AdditionalDependencies)
Note that in my case it was conflicting with mfcs80u.lib