PDA

View Full Version : The linker appears not to be linking.



orfner
14th September 2011, 22:13
I'm using QT 4.7.3 with the MSVC 9.0 compiler.
I'm trying to link to a lib in a separate VS2010 project.

The code compiles just fine, but I keep getting "unresolved external symbol" errors. (So far I'm only attempting to call the constructor of object I need to link to, nothing complicated).

I tried the following methods of linking in my .pro file:



LIBS += -L\\C:\\PathToLib -lLibname
LIBS += -L\\C:\\PathToLib\\Libname.lib
LIBS += C:\\PathToLib\\Libname.lib
LIBS += Libname.lib #I copied the lib to the current directory


I'm not getting any errors saying that it can't find the library, only that it can't find the symbols in it. It almost seems like the linker is just completely ignoring it.

I thought that there may have been a problem with differences between MSVC 9.0 and MSVC 10.0, so I changed all the settings in QT to use 10.0 (to match the VS project I'm trying to link to), but absolutely nothing changed.


Is there anything else I can try, or any reason the linker might be ignoring my lib?

Thanks

high_flyer
15th September 2011, 11:41
Its not enough to change the pro file when using MSVS. (visual studio is not readin the pro file at all, it will generate one based on its own project settings though, or read a pro file and generate its own properties out of it)
You have to specify the lib path and lib in the project settings in MSVS.

totem
15th September 2011, 13:40
@high_flyer : I dont agree with that, if you re-run qmake on the pro file it should be enough to build the corresponding .vcproj files. At least i never edit MSVS settings directly.

orfner, do you link dynamically or staticly ? If you chose the dynamic way, does your class exports its symbols ?

high_flyer
15th September 2011, 14:02
if you re-run qmake on the pro file it should be enough to build the corresponding .vcproj files.
With out telling MSVS to re-read the pro file?

totem
15th September 2011, 16:34
Sorry we are a little bit off-topic here.. What I meant by "re-run qmake on the pro file" is the following :

qmake -tp vc -recursive myproj.pro
This will regenerate vcproj file(s), and if MSVS is running it will detect the change and reload project. Whatever, as I said I never edit project settings through MSVS, and it's not the point here (unless orfner forgot to re-run qmake)

high_flyer
15th September 2011, 16:53
unless orfner forgot to re-run qmake
That was my point...
The pro file and vcproj file are not "connected".
If you change one, you have to update the other.
But as long as you are working on MSVS, it makes sense to change the pro file, only at the end, when you know your project settings don't change any more - and not change the por file, and update the vcproj every time you do...
but that is of course a subjective preference issue, each to his own. :)

totem
15th September 2011, 17:58
I knew we were talking about the same thing. I see your point now.

orfner
15th September 2011, 18:51
@high_flyer : I dont agree with that, if you re-run qmake on the pro file it should be enough to build the corresponding .vcproj files. At least i never edit MSVS settings directly.

orfner, do you link dynamically or staticly ? If you chose the dynamic way, does your class exports its symbols ?


I'm linking statically.
I also tried forcing linkage using
#pragma comment(lib, "libName.lib") in the cpp file that uses the symbols from the libs, but to no avail.

I should probably clarify, I'm building this from within QT Creator using MSVC, not using the Visual Studio plugin, so there are no .vcproj files generated

My build arguments are:



qmake: qmake.exe ProjectName.pro -r -spec win32-msvc2008 "CONFIG+=release"

Make: jom.exe in C:\ProjectPath\ProjectName-build-desktop

totem
16th September 2011, 09:17
You can get rid of these pragma statements as it's compiler-specific and should be replaceable by an appropriate pro file configuration.
Does your pro file properly define the library libName as statically built ?
As well, could you provide the linker error messages ?

stampede
16th September 2011, 09:24
You said:

I'm trying to link to a lib in a separate VS2010 project.
But qmake settings are:

qmake.exe ProjectName.pro -r -spec win32-msvc2008 "CONFIG+=release"
Have you tried to change it to use win32-msvc2010 ?

orfner
17th September 2011, 00:07
Does your pro file properly define the library libName as statically built ?
Yeah, I'm fairly certain that the way I'm including them in the pro file specifies the libs as static:

LIBS += C:/PathToLib/LibName.lib


Have you tried to change it to use win32-msvc2010 ?
Yes, and it didn't change anything. I'm currently in the (really long) process of building QT 4.7.4 with MSVC-2010 to see if that makes any difference.

In other news: I'm not sure what I did, but the linking errors changed...



warning LNK4229: invalid directive '/FAILIFMISMATCH:_MSC_VER=1600' encountered; ignored
warning LNK4229: invalid directive '/FAILIFMISMATCH:_ITERATOR_DEBUG_LEVEL=0' encountered; ignored
warning LNK4229: invalid directive '/FAILIFMISMATCH:_MSC_VER=1600' encountered; ignored
warning LNK4229: invalid directive '/FAILIFMISMATCH:_ITERATOR_DEBUG_LEVEL=0' encountered; ignored
error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl std::_Xout_of_range(char const *)" (__imp_?_Xout_of_range@std@@YAXPBD@Z) referenced in function "public: void __thiscall std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >::_Xran(void)const " (?_Xran@?$basic_string@_WU?$char_traits@_W@std@@V? $allocator@_W@2@@std@@QBEXXZ)
error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl std::_Xout_of_range(char const *)" (__imp_?_Xout_of_range@std@@YAXPBD@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl std::_Xout_of_range(char const *)" (__imp_?_Xout_of_range@std@@YAXPBD@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl std::_Xout_of_range(char const *)" (__imp_?_Xout_of_range@std@@YAXPBD@Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl std::_Xlength_error(char const *)" (__imp_?_Xlength_error@std@@YAXPBD@Z) referenced in function "protected: void __thiscall std::vector<class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> >,class std::allocator<class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > > >::_Xlen(void)const "
error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl std::_Xlength_error(char const *)" (__imp_?_Xlength_error@std@@YAXPBD@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl std::_Xlength_error(char const *)" (__imp_?_Xlength_error@std@@YAXPBD@Z)
error LNK2001: unresolved external symbol "__declspec(dllimport) void __cdecl std::_Xlength_error(char const *)" (__imp_?_Xlength_error@std@@YAXPBD@Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_ostream<wchar_t,struct std::char_traits<wchar_t> > & __thiscall std::basic_ostream<wchar_t,struct std::char_traits<wchar_t> >::write(wchar_t const *,__int64)" (__imp_?write@?$basic_ostream@_WU?$char_traits@_W@ std@@@std@@QAEAAV12@PB_W_J@Z) referenced in function "public: virtual void __thiscall pugi::xml_writer_stream::write(void const *,unsigned int)" (?write@xml_writer_stream@pugi@@UAEXPBXI@Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct std::char_traits<char> >::write(char const *,__int64)" (__imp_?write@?$basic_ostream@DU?$char_traits@D@st d@@@std@@QAEAAV12@PBD_J@Z) referenced in function "public: virtual void __thiscall pugi::xml_writer_stream::write(void const *,unsigned int)" (?write@xml_writer_stream@pugi@@UAEXPBXI@Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_istream<char,struct std::char_traits<char> > & __thiscall std::basic_istream<char,struct std::char_traits<char> >::read(char *,__int64)" (__imp_?read@?$basic_istream@DU?$char_traits@D@std @@@std@@QAEAAV12@PAD_J@Z) referenced in function "struct pugi::xml_parse_result __cdecl `anonymous namespace'::load_stream_impl<char>(class pugi::xml_document &,class std::basic_istream<char,struct std::char_traits<char> > &,unsigned int,enum pugi::xml_encoding)" (??$load_stream_impl@D@?A0x818da123@@YA?AUxml_pars e_result@pugi@@AAVxml_document@2@AAV?$basic_istrea m@DU?$char_traits@D@std@@@std@@IW4xml_encoding@2@@ Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_istream<char,struct std::char_traits<char> > & __thiscall std::basic_istream<char,struct std::char_traits<char> >::seekg(__int64,int)" (__imp_?seekg@?$basic_istream@DU?$char_traits@D@st d@@@std@@QAEAAV12@_JH@Z) referenced in function "struct pugi::xml_parse_result __cdecl `anonymous namespace'::load_stream_impl<char>(class pugi::xml_document &,class std::basic_istream<char,struct std::char_traits<char> > &,unsigned int,enum pugi::xml_encoding)" (??$load_stream_impl@D@?A0x818da123@@YA?AUxml_pars e_result@pugi@@AAVxml_document@2@AAV?$basic_istrea m@DU?$char_traits@D@std@@@std@@IW4xml_encoding@2@@ Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_istream<wchar_t,struct std::char_traits<wchar_t> > & __thiscall std::basic_istream<wchar_t,struct std::char_traits<wchar_t> >::read(wchar_t *,__int64)" (__imp_?read@?$basic_istream@_WU?$char_traits@_W@s td@@@std@@QAEAAV12@PA_W_J@Z) referenced in function "struct pugi::xml_parse_result __cdecl `anonymous namespace'::load_stream_impl<wchar_t>(class pugi::xml_document &,class std::basic_istream<wchar_t,struct std::char_traits<wchar_t> > &,unsigned int,enum pugi::xml_encoding)" (??$load_stream_impl@_W@?A0x818da123@@YA?AUxml_par se_result@pugi@@AAVxml_document@2@AAV?$basic_istre am@_WU?$char_traits@_W@std@@@std@@IW4xml_encoding@ 2@@Z)
error LNK2019: unresolved external symbol "__declspec(dllimport) public: class std::basic_istream<wchar_t,struct std::char_traits<wchar_t> > & __thiscall std::basic_istream<wchar_t,struct std::char_traits<wchar_t> >::seekg(__int64,int)" (__imp_?seekg@?$basic_istream@_WU?$char_traits@_W@ std@@@std@@QAEAAV12@_JH@Z) referenced in function "struct pugi::xml_parse_result __cdecl `anonymous namespace'::load_stream_impl<wchar_t>(class pugi::xml_document &,class std::basic_istream<wchar_t,struct std::char_traits<wchar_t> > &,unsigned int,enum pugi::xml_encoding)" (??$load_stream_impl@_W@?A0x818da123@@YA?AUxml_par se_result@pugi@@AAVxml_document@2@AAV?$basic_istre am@_WU?$char_traits@_W@std@@@std@@IW4xml_encoding@ 2@@Z)


All of the FAILIFMISMATCH:_MSC_VER=1600 stuff makes me think my problems are in fact coming from different versions of MSVC, even though I changed it to use 2010.

My conclusion: writing portable software sucks

stampede
17th September 2011, 10:07
My conclusion: writing portable software sucks
Well, you are trying to connect components both compiled with M$ compilers, I wouldn't call that portable :P In your situation I'd rather say: building software with M$ compilers sucks...
Have you tried to search for the help on some kind of VisualStudio-related forums ?

orfner
23rd September 2011, 21:20
So I changed stuff up a bit, and now I have a much better idea of what's going on.

I'm implicitly linking by including the header files, referencing the .lib in the pro file, and placing the .dll in the execution folder.

Right now I'm only using functions from one class from the dll. Some of the functions link, some of them don't. Dependency walker confirms that they're all being exported just fine.

The functions that don't link take a std::wstring or a std::wstring* as a parameter.

Both the dll and the qt project use the same compiler (MSVC2010), so it intuitively wouldn't have a problem with basic types...

Any ideas?

wysota
24th September 2011, 00:56
Using the same compiler is not enough. As far as I know you can define the width of strings (char vs wchar) for Microsoft compilers using some defines. Maybe you have a mix of that? Have you tried getting rid of Qt code completely and compiling purely C++ code in your project? Of course not one written from scratch but rather using the same libraries and header files.

orfner
27th September 2011, 01:30
So for anyone else getting this problem, I just figured it out. :cool:

The linker was breaking because Visual Studio by default maps wchar_t to __wchar_t, and QT does not.

To fix: Go to Project Settings -> Configuration Properties -> C/C++ -> Language and uncheck the "Treat WChar_t As Built in Type" property.