Linking against a .lib causes many multiply defined symbols [duplicate] - c++

I have a large and complicated project that is finally getting unit tests. I've built googleTest 1.6.0 locally with Visual Studio 2010, the project files built with cmake as the README specifies.
This project has many dependent libraries that are statically and dynamically linked. Many of them proprietary. All attempts to link generate 220 such errors. Here is a sampling:
msvcprtd.lib(MSVCP100D.dll) : error LNK2005: "public: void __cdecl std::_Container_base12::_Orphan_all(void)" (?_Orphan_all#_Container_base12#std##QEAAXXZ) already defined in gtest.lib(gtest-all.obj)
libcpmtd.lib(cerr.obj) : error LNK2005: "protected: char * __cdecl std::basic_streambuf >::_Gndec(void)" (?_Gndec#?$basic_streambuf#DU?$char_traits#D#std###std##IEAAPEADXZ) already defined in msvcprtd.lib(MSVCP100D.dll)
LIBCMTD.lib(setlocal.obj) : error LNK2005: _configthreadlocale already defined in MSVCRTD.lib(MSVCR100D.dll)
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library
fatal error LNK1169: one or more multiply defined symbols found
I've tried the /NODEFAULTLIB flag, and I've tried to ignore just msvcprtd.lib, MSVCRTD.lib, and LIBCMTD.lib, as suggested, but then I suffer from unresolved symbols...
Both the project and googleTest are compiled with x64, /MP, /MDd, and no /clr.
I've been playing around with compiler flags, wondering if there's some sort of version mismatch. Dumpbin, to the extent with which I understand it, hasn't hinted anything I might understand. I was hoping for an er

It looks like your question is answered in Google Talk's FAQ. Make sure all your libraries use the /MD(d) setting.
You may get a number of the following linker error or warnings if you attempt to link your test project with the Google Test library when your project and the are not built using the same compiler settings.
LNK2005: symbol already defined in object
LNK4217: locally defined symbol 'symbol' imported in function 'function'
LNK4049: locally defined symbol 'symbol' imported
The Google Test project (gtest.vcproj) has the Runtime Library option set to /MT (use multi-threaded static libraries, /MTd for debug). If your project uses something else, for example /MD (use multi-threaded DLLs, /MDd for debug), you need to change the setting in the Google Test project to match your project's.
To update this setting open the project properties in the Visual Studio IDE then select the branch Configuration Properties | C/C++ | Code Generation and change the option "Runtime Library". You may also try using gtest-md.vcproj instead of gtest.vcproj.

Related

Boost::python does not build in Debug mode

I am working on a system that needs to embed python in a C++ program on Windows. I installed boost with the boost python packages and everything seems to be correct. When I run CMake it finds the debug and the release libraries from Boost python (the mt and mt-gd ones) and links them correctly with the program via TARGET_LINK_LIBRARIES. However when I build the project in Debug mode the compiler refuses to build, but when I build in Release mode everything is builds fine.
Here is the way I link the library:
FIND_PACKAGE(Python3 COMPONENTS Interpreter Development REQUIRED)
FIND_PACKAGE(Boost COMPONENTS python REQUIRED)
add_executable(ExampleProject "Example.cpp")
TARGET_LINK_LIBRARIES(ExampleProject Boost::python)
TARGET_INCLUDE_DIRECTORIES(ExampleProject PUBLIC ${Boost_INCLUDE_DIR})
I have checked that the correct boost library is linked and I have used the Boost_PYTHON_LIBRARY_DEBUG, to which the compiler says it cannot open the library:
fatal error LNK1104: cannot open file 'boost_python310-vc143-mt-gd-x32-1_79.lib'
I did notice that it does not say libboost here, but when checking the linker it does say libboost, I guess the error output is just different.
When I use Boost::python in CMake it does link but it throws unresolved external symbols like this one:
error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl boost::python::throw_error_already_set(void)" (__imp_?throw_error_already_set#python#boost##YAXXZ) referenced in function "struct _object * __cdecl boost::python::expect_non_null<struct _object>(struct _object *)" (??$expect_non_null#U_object###python#boost##YAPAU_object##PAU2##Z)
I have also tried to set the Boost_USE_DEBUG_LIBS and Boost_USE_RELEASE_LIBS, but that did not change anything.
Furthermore the compiler does give the warning that release and debug are mixed:
warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
Which does indicate that they have been mixed up, but when I check the linker it uses the libboost_python310-vc143-mt-gd-x32-1_79.lib which is the Debug version of the python library.
Did I miss anything? Did I do something wrong? I do not understand why this happens and I am still fairly new to CMake and Boost, but I feel like I did all that I could to make it work.

error LNK2019: unresolved external symbol _OpenSSL_add_all_algorithms referenced in function __VerifyLicence#4

I have project working with release mode. However, when I convert and build it in DEBUG mode its showing below error:
error LNK2019: unresolved external symbol _OpenSSL_add_all_algorithms referenced in function __VerifyLicence#4
I'm not able to clearly get why this linker error. Is it due to that, I'm still using RELEASE version of OpenSSL? If yes, from where can I get DEBUG version of OpenSSL and integrate that in my code?
If No, how can I resolve this error?
My settings: Configuration Properties -> C/C++ -> Code Generation -> Runtime Libarary -> Multi-threaded Debug DLL (/MDd)
Note: Just before this error, I'm getting one warning as well:
LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library
I have ignored this warning.
Please help.

Is It possible to Linking static library into dynamic library in Visual Studio?

I am try to link a static library in a dynamic library tmxParser.lib in Visual Studio 2010 express. I have the static library set as a dependency of the dynamic library. When VS build the static library builds fine and creates tmxParser.lib. When VS builds the dynamic library I am getting linker errors. I tried to match the Runtime Library under property page -> Configuration properties -> C/C++ -> Code Generation
It doesn't help. Anyone have any idea?
2>LIBCMTD.lib(fopen.obj) : error LNK2005: _fopen already defined in s3e_d.lib(iwcrt_stdio.obj)
2>LIBCMTD.lib(open.obj) : error LNK2005: __open already defined in s3e_d.lib(iwcrt_win32.obj)
2>LIBCMTD.lib(open.obj) : error LNK2005: __sopen already defined in s3e_d.lib(iwcrt_win32.obj)
2>LIBCMTD.lib(chsize.obj) : error LNK2005: __chsize already defined in s3e_d.lib(iwcrt_win32.obj)
2>LIBCMTD.lib(read.obj) : error LNK2005: __read already defined in s3e_d.lib(iwcrt_win32.obj)
2>LIBCMTD.lib(close.obj) : error LNK2005: __close already defined in s3e_d.lib(iwcrt_win32.obj)
2>LIBCMTD.lib(lseek.obj) : error LNK2005: __lseek already defined in s3e_d.lib(iwcrt_win32.obj)
2>LIBCMTD.lib(setmode.obj) : error LNK2005: __setmode already defined in s3e_d.lib(iwcrt_win32.obj)
When you say you have it set as a dependency, do you mean you have it set as a Project Dependency or is under "Additional Dependencies" in
Project Properties -> Librarian->General -> Additional Dependencies
Generally, yes.
The problem you have seems to be that the library (is it the marmalade library?) implements some of the methods from the C++ runtime library. Probably, this lib is supposed to be used without linking the standard runtime library.
Possible solutions:
do not link c++ runtime in your DLL. That should be possible if you write just a thin wrapper
Ask the guys making the lib how they thought this is supposed to work
use the lib.exe or dumpbin.exe to:
extract all .obj files of the lib
remove the conflicting symbols from the .obj files
recreate the lib
The latter is obviously an ugly hack, and should be reserved for cases where neither source nor original developer are available anymore.

Why do I get LNK4098 conflicts with use of other libs - when trying to compile C++ in MSVS2010 Express?

My program will not successfully compile in /MT (MultiThreaded) mode. It Compiles in /MD (MultiThreaded DLL). I want to be able to use both libcurl and boost in an application I will distribute with an installer.
Compiling in: MSVS2010
This is code to replicate my problem:
#include "stdafx.h"
#include "boost/regex.hpp"
#include "curl/curl.h"
int _tmain(int argc, _TCHAR* argv[])
{
CURL *curl;
curl = curl_easy_init();
return 0;
}
This is the warning I get if in /MD mode:
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs;
use /NODEFAULTLIB:library
If I try compiling in /MT mode I get:
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _calloc already defined in
LIBCMT.lib(calloc.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _realloc already defined in LIBCMT.lib(realloc.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _free already defined in LIBCMT.lib(free.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _malloc already defined in LIBCMT.lib(malloc.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _strtoul already defined in LIBCMT.lib(strtol.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _memmove already defined in LIBCMT.lib(memmove.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _tolower already defined in LIBCMT.lib(tolower.obj)
1>MSVCRTD.lib(MSVCR100D.dll) : error LNK2005: _strtol already defined in LIBCMT.lib(strtol.obj)
...
: fatal error LNK1169: one or more multiply defined symbols found
I want to compile in /MT mode so that others can run my finished program with out having MSVS installed or needing to download anything additional. I can include and dll or lib files needed by my app in the installer.
I could disable loading the 'MSVCRTD' default library, but then compiling the with boost fails.
These are my preprocessor definitions:
WIN32
_DEBUG
_CONSOLE
BUILDING_LIBCURL
HTTP_ONLY
These are my additional dependencies:
libcurl.lib
ws2_32.lib
winmm.lib
wldap32.lib
Does anyone know what I am doing wrong?
Thanks,
William
Try setting nodefaultlib:libcmt.lib in linker options in VC.
MSVCRT*D*
LIBC*MT*.lib
flag compile the library differs from the flag compile the project (/MT,/MTD,/MD,/MDD)
Try to remove _DEBUG from precompiler directives (i.e. build release version). The libraries you link into your application use a non-debug versions and your code links to debug-version. That's why you get linker error that symbols are multiply defined: it links both debug and non-debug versions of runtime libraries.
(As far as I understood you tried to statically link all the required libraries.)
Try building libCurl with rtlibcfg=static. This will build the /Mt static version of libCURL.

How to use CLR in a different runtime environments under multiple projects in visualstudio?

Here is My Scenario.
I have an Existing Solution in visualstudio which contains 15 projects.
in some project i need to use managed c++ extensions. So first i have a workaround for sample solution contains 4 projects in those i have maintained dependencies b/w them.
and my solution is builded also.
I apply the same procedure to existing solution by adding extra project which contains managed code.so that i enable CLR in calling project(from where i call new added project methods from existing ex:demoproj) as well as project which contains main().and change the runtime from /mtd(existing one contains /mtd) to /mdd.
when i build the demoproj it builds success fully. But when i building project which contains main() it gives lot of errors like
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library
LIBCMTD.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(invarg.obj) : error LNK2005: __invalid_parameter already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(crt0.obj) : error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj)
LIBCMTD.lib(fclose.obj) : error LNK2005: _fclose already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(printf.obj) : error LNK2005: _printf already defined in MSVCRTD.lib(MSVCR80D.dll)
LIBCMTD.lib(mbstowcs.obj) : error LNK2005: _mbstowcs already defined in MSVCRTD.lib(MSVCR80D.dll)
I found this is happend because of using /clr option i think any idea???
But this case is not happend for my test solution.How to resolve this
Any help is greatly appreciated
All the dlls you link to using import libraries must have the same setting as the application for how you link to the runtime library. The errors are showing that you are mixing use of the runtime as a .dll (MSVCRTD) and as a .lib (LIBCMTD).