C++ linker unresolved external symbols - c++

I'm building an application against some legacy, third party libraries, and having problems with the linking stage. I'm trying to compile with Visual Studio 9. My compile command is:
cl -DNT40 -DPOMDLL -DCRTAPI1=_cdecl
-DCRTAPI2=cdecl -D_WIN32 -DWIN32 -DWIN32_LEAN_AND_MEAN -DWNT -DBYPASS_FLEX -D_INTEL=1 -DIPLIB=none -I. -I"D:\src\include" -I"C:\Program Files\Microsoft Visual Studio
9.0\VC\include" -c -nologo -EHsc -W1 -Ox -Oy- -MD mymain.c
The code compiles cleanly. The link command is:
link -debug -nologo -machine:IX86
-verbose:lib -subsystem:console mymain.obj wsock32.lib advapi32.lib
msvcrt.lib oldnames.lib kernel32.lib
winmm.lib [snip large list of
dependencies] D:\src\lib\app_main.obj
-out:mymain.exe
The errors that I'm getting are:
app_main.obj : error LNK2019:
unresolved external symbol
"_\_declspec(dllimport) public: void
__thiscall std::locale::facet::_Register(void)"
(__imp_?_Register#facet#locale#std##QAEXXZ)
referenced in function "class
std::ctype<char> const & __cdecl
std::use_facet<class std::ctype<char>
(class std::locale const &)" (??$use_facet#V?$ctype#D#std###std##YAABV?$ctype#D#0#ABVlocale#0##Z)
app_main.obj : error LNK2019:
unresolved external symbol
"__declspec(dllimport) public: static
unsigned int __cdecl
std::ctype<char>::_Getcat(class
std::locale::facet const * *)"
(__imp_?_Getcat#?$ctype#D#std##SAIPAPBVfacet#locale#2##Z)
referenced in function "class
std::ctype<char> const & __cdecl
std::use_facet<class std::ctype<char>
(class std::locale const &)" (??$use_facet#V?$ctype#D#std###std##YAABV?$ctype#D#0#ABVlocale#0##Z)
app_main.obj : error LNK2019:
unresolved external symbol
"__declspec(dllimport) public: static
unsigned int __cdecl
std::ctype<unsigned
short>::_Getcat(class
std::locale::facet const * *)"
(__imp_?_Getcat#?$ctype#G#std##SAIPAPBVfacet#locale#2##Z)
referenced in function "class
std::ctype<unsigned short> const &
__cdecl std::use_facet<class std::ctype<unsigned short> >(class
std::locale const &)"
(??$use_facet#V?$ctype#G#std###std##YAABV?$ctype#G#0#ABVlocale#0##Z)
mymain.exe : fatal error LNK1120: 3
unresolved externals
Notice that these errors are coming from the legacy code, not my code - app_main.obj is part of the legacy code, while mymain.c is my source. I've done some searching around, and what that I've read says that this type of error is caused by a mismatch with the -MD switch between my code and the library that I'm linking to. Since I'm dealing with legacy code, a solution has to come from my environment. It's been a long time since I've done C++ work, and even longer since I've used Visual Studio, so I'm hoping that this is just some ignorance on my part. Any ideas on how to get these resolved?

These are standard library references. Make sure that all libraries (including the standard library) are using the same linkage. E.g. you can't link statically while linking the standard lib dynamically. The same goes for the threading model used. Take special care that you and the 3rd party library use the same linkage options.
This can be a real pain in the *ss.

Check this on MSDN:
/MD Causes your application to use the multithread- and DLL-specific version of the run-time library.
/MT Causes your application to use the multithread, static version of the run-time library.
Note: "... so that the linker will use LIBCMT.lib to resolve external symbols"
So you'll need a different set of libraries.
How I went about finding out which libraries to link:
Find a configuration that does link, and add /verbose option.
Pipe the output to a text file.
Try the configuration that doesn't link.
Look in the verbose output from step 2 for the symbols that are unresolved ("_declspec(dllimport) public: void thiscall std::locale::facet::Register(void)" in your case) and find the used libraries.
Add those libraries to the list of libraries you're linking to.
Old skool but it worked for me.
Jan

If you still wish to get the project to compile using VS2008 (or in the future) I can suggest using a binary editor to view the object file in question mainapp.obj.
Here is an example from a small project of mine.
The zdbException.obj contains the following excerpt
DEFAULTLIB:"libc
pmtd" /DEFAULTLI
B:"uuid.lib" /DE
FAULTLIB:"uuid.l
ib" /include:?id
#?$num_put#DV?$o
streambuf_iterat
or#DU?$char_trai
ts#D#std###std##
#std##2V0locale#
2#A /include:?id
#?$numpunct#D#st
d##2V0locale#2#A
/DEFAULTLIB:"LI
BCMTD" /DEFAULTL
IB:"OLDNAMES" /E
DITANDCONTINUE
Note the entry /DEFAULTLIB:"LIBCMTD". This indicates the object file was compiled with the static c run-time multi-threaded debug.
There is also the possibility that the functions referenced in the obj are deprecated in the standard run-time lib shipped with VS2008.

After trying to get this stuff to compile under VS 2008, I tried earlier versions of VS - 2005 worked with warnings, and 2003 just worked. I double checked the linkages and couldn't find any problems, so either I just couldn't find it, or that wasn't the problem.
So to reiterate, downgrading to VS 2003 fixed it.

Related

Link errors with GraphicsMagick

I downloaded and compiled GraphicsMagick, 1.3.23, Q16, x64, StaticMT version. I had to convert the Visual Studio 7 solution generated by GraphicsMagick's build utility to Visual Studio 2015 format. I linked my project to CORE_DB_magick_.lib and CORE_DB_Magick++_.lib.
When the linker ran, it produced unresolved external symbols when linking InitializeMagick() and DestroyMagick()
1>wtd.lib(WebController.obj) : error LNK2019: unresolved external symbol __imp_DestroyMagick referenced in function "public: __cdecl Wt::WebController::~WebController(void)" (??1WebController#Wt##QEAA#XZ)
1>wtd.lib(WebController.obj) : error LNK2019: unresolved external symbol __imp_InitializeMagick referenced in function "public: __cdecl Wt::WebController::WebController(class Wt::WServer &,class std::basic_string,class std::allocator > const &,bool)" (??0WebController#Wt##QEAA#AEAVWServer#1#AEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##_N#Z)
I can't understand why the symbols are not being linked. Any ideas?
Apparently, GraphicsMagick Static versions do not link properly.
What is your project type? I had a similar problem when trying to link the GraphicsMagick libraries to a DLL.
The clue here is that __imp is the function decoration for DLL imports, so you're trying to link with DLL functions.
The problem is that the header magick/common.h, when linked to a DLL project, reads the current configuration of the Visual Studio pre-processor environment to determine which mode the library is in, which is obviously wrong if you're trying to link static libraries into your DLL, for example. In this case, it defines MagickExport to __declspec(dllimport).
AFAIK this is a bug in the library. For proper static build support, magick/common.h needs to do something like read information from the magick/magick_config.h to determine what mode the library was actually built in and define MagickExport appropriately.
Since your library is statically linked, you can fix this by commenting out everything in the define:
#if defined(MSWINDOWS) && !defined(__CYGWIN__)
and replacing it with:
#define MagickExport
#define ModuleExport
#define MagickGlobal

LNK2019: Undefined external symbol _Getgloballocale, Visual Studio 2013

I have the following linker error in one project of a Visual Studio 2013 solution:
error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" (__imp_?_Getgloballocale#locale#std##SAPEAV_Locimp#12#XZ) referenced in function "class std::ctype<char> const & __cdecl std::use_facet<class std::ctype<char> >(class std::locale const &)" (??$use_facet#V?$ctype#D#std###std##YAAEBV?$ctype#D#0#AEBVlocale#0##Z)
In all projects I linked the run-time library dynamically (/MD).
I considered the hints in the following posts:
C++ linker unresolved external symbols
What libraries do I
need to link my mixed-mode application to?
Boost -
Unresolved external symbols when compiling linking with /MT instead
of /MD
Link error 2001:unresolved external symbol
Linker errors between multiple projects in Visual C++
but was not able to solve this issue.
Actually, I am not even aware of where in the project _Getgloballocale is used. Maybe it would also help to know the lib in which _Getgloballocale is located.
The projects use the following libraries:
cURL
Protobuf
libboost_thread-vc120-mt-1_56.lib
libboost_system-vc120-mt-1_56.lib
libboost_python-vc120-mt-1_56.lib
libboost_filesystem-vc120-mt-1_56.lib
In all projects I linked the run-time library dynamically (/MD).
As others noted, verifying this might be less obvious than appears. For one, some of your libraries might drag in external dependencies that do rely on a mis-matching runtime.
Suggest you link with /VERBOSE on (in your EXE project, properties / linker / general / show progress), and search the output dump for MSVCR. You might catch a different version (msvcr100.lib) or a different configuration (msvcr120d.lib). Also try to search for LIBCMT - that is the library for static linking of the runtime. These typically appear as part of a /DEFAULTLIB linker directive, and you should be able to understand from the dump in which library context this directive is present.
You can also post here the verbose output (or the relevant snippets), and we can try to help interpret it.

boost LNK2019 error

I read through the boost documentation using the '5.3.4 Invoke b2' and followed up with forum threads that explained details of linking the boost library to the header and linker directories, in the boost help '4.1 Build From the Visual Studio IDE', and found this cool boost related wiki that explained the bjam.exe controls.
placed #include <boost/thread/thread.hpp> in the main.cpp
And, I get this linker error, and the existing help threads have identified the problem is with x64:
Directory path and x32/x64
unrelated to Visual studio
unresolved
The error:
error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (?generic_category#system#boost##YAAEBVerror_category#12#XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'posix_category''(void)" (??__Eposix_category#system#boost##YAXXZ) main.obj
error LNK2019: unresolved external symbol "class boost::system::error_category const & __cdecl boost::system::system_category(void)" (?system_category#system#boost##YAAEBVerror_category#12#XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'native_ecat''(void)" (??__Enative_ecat#system#boost##YAXXZ) main.obj
they are similar, something about 'posix_category' and something about 'native_ecat'
So, I tried to build the x64 boost library, and found conflicting instructions on where to put these:
did not say "Note for x64 users: Add the address-model=64 option to bjam (after the threading argument) in order to build static libs with the 64-bit compiler."
cant find the link but one said to place x64 lib in the vs2008 bin?
Also, I tried changing the vs2008 configuration back to x32 -> solutionExplorer/solution_properties/configuration_manager/active_solution_platform - Win32, closed and reopened visual studio - relinked the additional directories C/C++/general and Linker/general to Boost/root and Boost/stage/lib - and it compiled without error.
My best guess at the b2 commands is --toolset=msvc-9.0 address-model=64 --build-type=complete --stagedir=lib\x64 stage
Please give concise instructions for how to build and install x64 version of boost on VS2008. Also, what was the wiki talking about for release and debug - they are not in Boost invocation?
use c++ 11
It more or less includes all the features of boost I wanted to use, also compiles in g++ without figuring out how to build the boost library on my university server and then reference into a remote g++ build - pretty much solved everything with that

linker error when using Qt and Boost

When I am using Qt (v4.7.4) and Boost (tried v1.47 and v1.48) together in my c++ project, I get a linker error caused by a class that includes <boost\filesystem.hpp>. I just set up Qt and before the code was working without any problems.
This is error message:
...obj : error LNK2001: unresolved external symbol "private: static class std::codecvt const * & __cdecl boost::filesystem3::path::wchar_t_codecvt_facet(void)" (?wchar_t_codecvt_facet#path#filesystem3#boost##CAAAPBV?$codecvt#GDH#std##XZ)
...obj : error LNK2001: unresolved external symbol "void __cdecl boost::filesystem3::path_traits::convert(char const *,char const *,class std::basic_string,class std::allocator > &,class std::codecvt const &)" (?convert#path_traits#filesystem3#boost##YAXPBD0AAV?$basic_string#GU?$char_traits#G#std##V?$allocator#G#2##std##ABV?$codecvt#GDH#5##Z)
...obj : error LNK2001: unresolved external symbol "void __cdecl boost::filesystem3::path_traits::dispatch(class boost::filesystem3::directory_entry const &,class std::basic_string,class std::allocator > &,class std::codecvt const &)" (?dispatch#path_traits#filesystem3#boost##YAXABVdirectory_entry#23#AAV?$basic_string#GU?$char_traits#G#std##V?$allocator#G#2##std##ABV?$codecvt#GDH#6##Z)
...obj : error LNK2001: unresolved external symbol "void __cdecl boost::filesystem3::path_traits::convert(unsigned short const *,unsigned short const *,class std::basic_string,class std::allocator > &,class std::codecvt const &)" (?convert#path_traits#filesystem3#boost##YAXPBG0AAV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##ABV?$codecvt#GDH#5##Z)
...exe : fatal error LNK1120: 4 unresolved externals
EDIT:
Here I found someone having this problem coming to this conclusion:
this really is a Qt issue. Using wchar_t as a native type you have to
recompile Qt using the same compiler switch. There even is a bug in the
tracker: https://bugreports.qt.io/browse/QTBUG-9617
In general, you will have to be very careful and do not mix wchar_t
compiler settings in your projects as they will become incompatible.
So I recompiled Qt setting /Zc:wchar_t, but it didn't show any effect. I still get the same error.
I think you are on the right track, but it sounds like your -Zc:wchar_t didn't "stick." We had to do the same thing to make Qt happy with Google Breakpad and the ICU library. We changed the /Zc:wchar_t setting in (QT_SOURCE)\mkspecs\win32-msvc2008\qmake.conf and compiled Qt from source, and after that everything works.
When you build your project that uses Qt and Boost, you should see this option in the compiler output. Something like:
cl -c -nologo -Zm200 -Zc:wchar_t ... (etc.)
If you've already build Qt without this option, you may have to do a make confclean first to ensure everything really gets rebuilt with the new settings.
It sounds like -Zc:wchar_t will be the default in Qt 5.
Using boost-1.49, Qt 4.4 and VS2005 had the same problem. Going to project properties, then setting "Configuration Properties -> C/C++ -> Language -> Treat wchar_t as Built-in Type" to "Yes" fixed the problem.
Qt probably changed the tipology of your program with regards the configuration of the runtime: consequently, the boost library you use (filesystem), that come in many configuration accessed by naming convention - connot be found.
For instance, multithread runtimes require mt somewhere in library name (I hope I remember well, but anyway see the docs where details are fully documented). This naming is fairly transparent to the programmer, due to pragmas that boost programmers appropriately used to ease the library usage under different compilers.
You should miss the non-wchar filesystem.lib. When I used Windows, I used boost Jam to interface with Visual C++ (may be that goes back to the past millenium!). I hope it's still usable.

Boost::FileSystem Linking Problem

I downloaded and built the boost libraries (version 1.47.0) on Windows 7 (64bit) following the instructions here.
Now when I want to use the Boost::Filesystem library I can include the header file without issue and it compiles my code file. The problem arises at linking. I get the following errors;
main.obj : error LNK2019: unresolved external symbol "class boost::filesystem3::file_status __cdecl boost::filesystem3::detail::status(class boost::filesystem3::path const &,class boost::system::error_code *)" (?status#detail#filesystem3#boost##YA?AVfile_status#23#AEBVpath#23#PEAVerror_code#system#3##Z) referenced in function "bool __cdecl boost::filesystem3::exists(class boost::filesystem3::path const &)" (?exists#filesystem3#boost##YA_NAEBVpath#12##Z)
main.obj : error LNK2019: unresolved external symbol "private: static class std::codecvt<wchar_t,char,int> const * & __cdecl boost::filesystem3::path::wchar_t_codecvt_facet(void)" (?wchar_t_codecvt_facet#path#filesystem3#boost##CAAEAPEBV?$codecvt#_WDH#std##XZ) referenced in function "public: static class std::codecvt<wchar_t,char,int> const & __cdecl boost::filesystem3::path::codecvt(void)" (?codecvt#path#filesystem3#boost##SAAEBV?$codecvt#_WDH#std##XZ)
(amongst others)
It might be worth noting that when I first tried to build the project it said it couldn't find the .lib file libboost_filesystem-vc100-mt-1_47.lib. I hadn't specifically told it it needed that file so not sure how it figured that out? Either way I pointed the linker to the correct directory and then it gave the above errors.
Does anybody know how to fix this problem? Thanks.
Edit: I'm using VS2010 toolchain through eclipse CDT to build the system. The complete compile command is
cl /c /EHs /MD /Zi /I"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include" /I"C:\boost_1_47_0" /nologo <SOURCE_FILE>
and the linker command
link /debug /nologo /libpath:C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64 /libpath:C:\boost_1_47_0\stage\lib /libpath:C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64 /OUT:<EXE_NAME> <OBJECT_FILES>
Rebuild the Boost library with address-model=64 b2 command line switch. This builds 64 bit libraries.
If you're on linux and happen upon this article looking for the fix the fix is (at least on ubuntu 12.10) to install the development package for boost filesystem:
sudo apt-get install libboost-filesystem-dev
That installs the correct libraries for linking to and all works well.