Linking the Windows API - winapi

I was using a ITK library. When I wrote a sample program using this library, I've got following linker errors. As we can see these all "unresolved" symbols are Windows API functions and these windows API functions were used by ITK library and not my program.
Error 1 error LNK2019: unresolved external symbol _SnmpUtilVarBindFree#4 referenced in function "int __cdecl gdcm::GetMacAddrSys(unsigned char *)" (?GetMacAddrSys#gdcm##YAHPAE#Z) C:\Projects\sampleProject\bin\itkgdcm.lib(gdcmUtil.obj)
Error 2 error LNK2019: unresolved external symbol _SnmpUtilOidNCmp#12 referenced in function "int __cdecl gdcm::GetMacAddrSys(unsigned char *)" (?GetMacAddrSys#gdcm##YAHPAE#Z) C:\Projects\sampleProject\bin\itkgdcm.lib(gdcmUtil.obj)
Error 3 error LNK2019: unresolved external symbol _SnmpUtilOidCpy#8 referenced in function "int __cdecl gdcm::GetMacAddrSys(unsigned char *)" (?GetMacAddrSys#gdcm##YAHPAE#Z) C:\Projects\sampleProject\bin\itkgdcm.lib(gdcmUtil.obj)
Error 4 error LNK2019: unresolved external symbol _WSAStartup#8 referenced in function "int __cdecl gdcm::GetMacAddrSys(unsigned char *)" (?GetMacAddrSys#gdcm##YAHPAE#Z) C:\Projects\sampleProject\bin\itkgdcm.lib(gdcmUtil.obj)
Error 5 error LNK2019: unresolved external symbol __imp__UuidCreate#4 referenced in function "private: static bool __cdecl gdcm::Util::GenerateUUID(unsigned char *)" (?GenerateUUID#Util#gdcm##CA_NPAE#Z) C:\Projects\sampleProject\bin\itkgdcm.lib(gdcmUtil.obj)
Error 6 error LNK2019: unresolved external symbol _gethostbyname#4 referenced in function "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl gdcm::Util::GetIPAddress(void)" (?GetIPAddress#Util#gdcm##CA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) C:\Projects\sampleProject\bin\itkgdcm.lib(gdcmUtil.obj)
Error 7 error LNK2019: unresolved external symbol _gethostname#8 referenced in function "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl gdcm::Util::GetIPAddress(void)" (?GetIPAddress#Util#gdcm##CA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) C:\Projects\sampleProject\bin\itkgdcm.lib(gdcmUtil.obj)
Error 8 error LNK2019: unresolved external symbol _WSACleanup#0 referenced in function "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl gdcm::Util::GetIPAddress(void)" (?GetIPAddress#Util#gdcm##CA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) C:\Projects\sampleProject\bin\itkgdcm.lib(gdcmUtil.obj)
These errors have raised few questions in my mind. For some of them I think I know the answers but I need confirmation if I'm right or wrong.
These Windows API functions are used in gdcmUtil.cpp (because it says gdcmUtil.obj). So, compiler compiles gdcmUtil.cpp to (say)gdcmUtil.asm and it is assembler assembles it to gdcmUtil.obj. Since while compiling the library we are not generating any exe files. These *.obj files aren't linked against any other *.obj. So, windows API symbols are still external symbols for that module and thus left unresolved. Right?
Who is generating the *.LIB file? I mean compiler is generating *.s file and Assembler is generating *.obj file and we are not using linker. So, from where did we get *.lib file? Who packed all *.obj files to a *.lib file?
How are Windows API applications (programs that are written in C and use Windows API) are compiled? I mean, because its a standalone applications how are these WIN32 API symbols resolved? Assuming, I've written such program can anyone tell me what do I need to sucessfully build application?
I know that Windows API is implemented in kernel32.dll, gdi32.dll, user32.dll (which may in turn import some other dlls like wsock32.dll for networking API etc.) but the question is How to use these functions in Windows API functions in a C program. I mean how to dynamically link against these DLLs
In my quest to find answer to my problem of Linker errors. I discovered that I must add wsock32.lib (for WinSock 1.1) or ws2_32.lib(for WinSock 2) or snmpapi.lib to Linker Input.
Since when Windows API is being statically linked?
Since when windows API is being shipped in static libraries (*.lib files)?
In which directory can I find all these strange *.lib files?
Now, Is there any alternative solution other than adding *.lib files to linker input files? because when ever I use some new ITK library function, I get a new linker error. I've to Google to find in which *.lib file this specific symbol (window api function) belongs to then add that *.lib file to my linker input. Its driving me crazy! Is there any sane way?

Yes, that's right.
Who is generating what .lib file? A static library is normally generated by the lib utility. A library for a dll is normally generated by the linker.
You resolve Windows symbols by linking with the right libraries.
Again, by linking against the appropriate library (e.g., kernel32.lib, gdi32.lib and user32.lib for the three DLLs you mentioned).
It's not statically linked. You link against the library. In the case of a library for a DLL (such as those listed above) the linker uses that to embed a record into the executable that tells the loader which DLL to use to resolve those symbols. The .lib files are in the lib subdirectory of wherever you installed the SDK (typically something like C:\Program Files\Microsoft SDKs\Windows\v6.0A. Normally the SDK installer will add those where necessary so you don't have to explicitly specify that directory when linking from the command line or within Visual Studio.
There are a few, though it's open to question whether they're really an improvement. One possibility is to use a pragma to tell the linker to use a particular library:
#pragma comment(lib, "ws2_32.lib")
This is particularly useful in a header if it uses functions that depend on a particular library.

Related

LNK2019: unresolved external symbol error in Visual Studio C++

This is my code in Visual Studio C++
#include "stdafx.h"
#include<opencv\cv.h>
#include<opencv\highgui.h>
using namespace cv;
int main(int argc, char** argv[]) {
IplImage* img = cvLoadImage("logo.jpg");
cvNamedWindow("Test", CV_WINDOW_AUTOSIZE);
cvShowImage("Test", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Test");
return 0;
}
I am using OpenCV 2.4.6 and Visual Studio 2010. This is the error:
openCV_testing.obj : error LNK2019: unresolved external symbol _cvDestroyWindow
referenced in function _main
openCV_testing.obj : error LNK2019: unresolved external symbol _cvReleaseImage
referenced in function _main
openCV_testing.obj : error LNK2019: unresolved external symbol _cvWaitKey referenced in
function _main
openCV_testing.obj : error LNK2019: unresolved external symbol _cvShowImage referenced
in function _main
openCV_testing.obj : error LNK2019: unresolved external symbol _cvNamedWindow
referenced in function _main
openCV_testing.obj : error LNK2019: unresolved external symbol _cvLoadImage referenced
in function _main
Please help.
'unresolved external symbol' means that you're not linking with required library.
Go to Properties -> Linker -> Additional Library dependencies and add path to OpenCV libs.
First check
How to build applications with OpenCV inside the Microsoft Visual Studio
If you still suffer from the same problem, you could be under one of the below cases.
Your active solution platform is x86 but you are trying to link x64 OpenCV libraries.
Your active solution platform is X64 but you are trying to link x86 OpenCV libraries.
If you are under one of these cases, check
Compiling a 64-bit Application in Microsoft Visual Studio Express 2010
Add these into your code:
#pragma comment (lib, "opencv_core248d.lib")
#pragma comment (lib, "opencv_highgui248d.lib")
#pragma comment (lib, "opencv_imgproc248d.lib")
#pragma comment (lib, "opencv_video248d.lib")
#pragma comment (lib, "opencv_features2d248d.lib")
It worked for me.
i searched a lot for the same problem this was the best solution i had found and it worked for me.
Open Configuration Properties > C/C++ > General, and edit the field Additional Include Directories to add these 3 paths (for the headers):
C:\OpenCV2.3\build\include\opencv
C:\OpenCV2.3\build\include\opencv2
C:\OpenCV2.3\build\include
I know this is not about the OpenCV library, but I already had a problem importing Tiny-Process library. My .lib file was linked correctly in Configuration Properties -> Linker -> Additional Library dependencies and the Additionnal Include Directories were correctly added but the the functions definition (s) were still not found and I was getting the LNK2019 error.
To fix the issue, I had to go in the library project properties, change the Character Set property in Configuration Properties -> Advanced Character Set and change the value Use Multi-Byte Character Set to Use Unicode Character Set.
After recompiling the library and using the new .lib file, it was working.

Linker errors in pcp_main example from libjingle

I'm working to get libjingle working on windows7 using visual studio. I followed GYP methodology to build lib jingle and got libs after executing the libjingle.sln file.
Now, when I'm trying to run pcp_main.cc file, I got slashed with linker errors, after some work around, I was left with 5 linker errors. Can someone help me how to resolve those errors?
Error 5 error LNK1120: 4 unresolved externals C:\Users\username\documents\visual studio
2012\Projects\PCPTest\Debug\PCPTest.exe Error 3 error LNK2001:
unresolved external symbol "protected: virtual bool __thiscall
buzz::JingleInfoTask::HandleStanza(class buzz::XmlElement const *)"
(?HandleStanza#JingleInfoTask#buzz##MAE_NPBVXmlElement#2##Z) C:\Users\username\documents\visual
studio 2012\Projects\PCPTest\PCPTest\pcp_main.obj
Error 1 error LNK2001: unresolved external symbol "public: virtual int
__thiscall buzz::JingleInfoTask::ProcessStart(void)" (?ProcessStart#JingleInfoTask#buzz##UAEHXZ) C:\Users\username\documents\visual
studio 2012\Projects\PCPTest\PCPTest\pcp_main.obj
Error 2 error LNK2019: unresolved external symbol "public: void
__thiscall buzz::JingleInfoTask::RefreshJingleInfoNow(void)" (?RefreshJingleInfoNow#JingleInfoTask#buzz##QAEXXZ) referenced in
function "public: void __thiscall
AutoPortAllocator::SetXmppClient(class buzz::XmppClient *)"
(?SetXmppClient#AutoPortAllocator##QAEXPAVXmppClient#buzz###Z) C:\Users\username\documents\visual
studio 2012\Projects\PCPTest\PCPTest\pcp_main.obj
Error 4 error LNK2019: unresolved external symbol
_imp_InternetQueryOptionW#16 referenced in function "bool __cdecl talk_base::GetIeLanProxySettings(char const *,struct
talk_base::ProxyInfo *)"
(?GetIeLanProxySettings#talk_base##YA_NPBDPAUProxyInfo#1##Z) C:\Users\username\documents\visual
studio 2012\Projects\PCPTest\PCPTest\libjingle.lib(proxydetect.obj)
My include folder is pointing to
C:\Users\username\Desktop\libjingle\trunk\testing
C:\Users\username\Desktop\libjingle\trunk\
and lib folder to
C:\Users\username\Desktop\libjingle\trunk\build\Debug\lib
Additional dependencies have absolute paths of all the available libs.
C:\Users\username\Desktop\libjingle\trunk\build\Debug\lib\audio_processing_sse2.lib
C:\Users\username\Desktop\libjingle\trunk\build\Debug\lib\bitrate_controller.lib
C:\Users\username\Desktop\libjingle\trunk\build\Debug\lib\CNG.lib
...
...
...
The source codeI'm using is present here.
http://pastebin.com/GATFYWnW
Got the answer!
The Error 4 can be removed by adding the following line
#pragma comment(lib, "Wininet.lib")
The remaining errors correspond to the jingleInfoTask.cc and JingleInfotask.h files. Its a bit surprise to see that the solution file (libjingle.sln) doesnt have these files when it built libraries. The work around would be to add the jingleinfotask.cc and jingleinfotask.h files into xmpp folder and re-build the libjingle.sln file.
This will produce the jingleinfotask.obj and required lib files. Make sure you point the generated lib file in the additional dependencies section in linker section of project properties.

error LNK2019: unresolved external symbol - only .h files

Argh... I've been struggling lately to make Visual Studio 2010 (VC++) include a bunch of 3rd party libraries I wanna use in my project. That's the issue: The linker seems not to be able to determine every symbol that is generated in my code which come from the 3rd party libraries definitions. I've included the header files path on my include directories and also the sources path on my source directory, but it is still not working. I've googled it for a while and in most of cases, the issues is cause by a missing reference of the .lib file on linker's additional dependencies, however the library don't come with them.
Here's a piece of sample code:
#include "stdafx.h"
#include <fuzzylite\FuzzyEngine.h>
int _tmain(int argc, _TCHAR* argv[])
{
fl::FuzzyEngine eng;
return 0;
}
So that's the output VS shows
Fuzzycolors.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall fl::FuzzyEngine::~FuzzyEngine(void)" (??1FuzzyEngine#fl##UAE#XZ) referenced in function _wmain
Fuzzycolors.obj : error LNK2019: unresolved external symbol "public: __thiscall fl::FuzzyEngine::FuzzyEngine(void)" (??0FuzzyEngine#fl##QAE#XZ) referenced in function _wmain
So i wonder if is there a way to build my sources with the .h and .cpp files of my 3rd party library.
Thank you.
Caio
Check the new version of fuzzylite-2.0 at http://www.fuzzylite.com. That problem has been solved.
Windows requires to add __declspec(dllexport) to classes, which was not present in previous versions. Today, every class starts with class FL_EXPORT Engine, where FL_EXPORT is the missing definition.

Visual studio - prevent linking of static library

I am creating a static library in Visual Studio 2010. This library will be statically linked with another of my applications to produce the .exe. The thing is that I want my .exe to be statically linked against C adn C++ library (don't want dependency on msvcp100.dll and msvcr100.dll). But what ever I do I can't get it working.
If I link my static library with static C and C++ libs then I can't compile the .exe - Linker complains about "already defined symbols".
If I link my static library with C and C++ DLL then my .exe ends up having dependency on msvcp100.dll and msvcr100.dll.
How do I tell VS to link my static library with static C and C++ libs only when it's being linked in my .exe?
EDIT
Here are a fre linker errors when both static lib and .exe user /MT (i.e. static linking of runtime library):
1>msvcrt.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info##AAE#ABV0##Z) already defined in LIBCMT.lib(typinfo.obj)
1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "public: class std::locale::facet * __thiscall std::locale::facet::_Decref(void)" (?_Decref#facet#locale#std##QAEPAV123#XZ) already defined in re2_release.lib(regexp.obj)
1>msvcprt.lib(MSVCP100.dll) : error LNK2005: "protected: virtual class std::basic_streambuf<char,struct std::char_traits<char> > * __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::setbuf(char *,__int64)" (?setbuf#?$basic_streambuf#DU?$char_traits#D#std###std##MAEPAV12#PAD_J#Z) already defined in re2_release.lib(regexp.obj)
1>msvcrt.lib(MSVCR100.dll) : error LNK2005: _strtoul already defined in LIBCMT.lib(strtol.obj)
1>LIBCMT.lib(crt0init.obj) : warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
If I set /NODEFAULTLIB:LIBCMT then errors from mscvrt.lib disappear, but msvcprt.lib errors remain.
I had this problem, and libriks solution solved it. I will quote him here for those who skips reading the comments:
Some module is not using static linking of the runtime library, because your linker errors >show "msvcrt.lib" which is the import library for the DLL version. Look more carefully >through your project settings, and any other libraries you link to, making sure EVERYTHING is >using /MT.

linking with boost unresolved extern link error

Visual Studio 2005
I am linking with boost libraries release 1_33_1.
I keep getting this link error.
libboost_thread-vc80-mt-sgd-1_33_1.lib(once.obj) :error LNK2001: unresolved external symbol "public: void __thiscall std::_String_base::_Xran(void)const " (?_Xran#_String_base#std##QBEXXZ)
Does anyone have any suggestions?
Many thanks,
Edit ======
After recompiling the boost libraries using these switches:
C:\boost_1_42_0>bjam --build-dir=d:\boost_1_42 --build-type-complete --toolset=msvc-8.0 address-model=32 architecture=x86
I am getting some of the following errors:
1>msvcrtd.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info##AAEAAV0#ABV0##Z) already defined in LIBCMTD.lib(typinfo.obj)
1>msvcrtd.lib(MSVCR80D.dll) : error LNK2005: __wassert already defined in LIBCMTD.lib(wassert.obj)
Anyone have any ideas?
Many thanks,
First of all you don't have to set address-model and architecture explicitly if building 32 bit. Also your call has an error: --build-type-complete should be --build-type=complete. If I'm not wrong your call builds absolutely all libs, including filesystem, regex etc.
Obviously you need boost::thread only, so I would recommend to try this call:
C:\boost_1_42_0>bjam --build-dir=d:\boost_1_42 --build-type=complete --toolset=msvc-8.0 --with-thread
But I don't think that will solve your prob but you could give it a try.
My guess is that you somehow mixed /MD and /MT in the project settings.
See these links: First, second
What is probably happening is that your project is linking to the static debug version of the C++ runtime, which causes the static debug boost thread library to be linked and is also linking to another static library which was dynamically linked to the C++ runtime.
The sgd tag in libboost_thread-vc80-mt-sgd-1_33_1.lib means that the boost thread library you're linking with was built against the static debug version of the C++ runtime.

Resources