I can't understand why it has error LNK2019 "unresolvant external symbols" - visual-c++

I follow the insturction to compile glpk on windows, then I got glpk_4_52.lib and glpk_4_52.dll. In my VC 2010 project, I include head file
#include "glpk.h"
and I include glpk_4_52.lib in "additional library directory".
But after compiling it still shows:
error LNK2019, "unresolvant external symbols", _glp_create_prob, referenced in _main
and similar others.
Could anyone knows what the problem is here? Thanks!d

"Additional library directory" is not the right way to add a lib file to the link.
Either,
Add glpk_4_52.lib in Configuration Properties -> Linker -> Input -> Additional Dependencies
or
Make the glpk project a dependency of your EXE project, if they are in the same solution, bu using "Project Dependencies" and make sure that Configuration Properties -> Linker -> General -> Link library dependencies is set to Yes
I am able to reproduce your problem by using the H and LIB version 4_52 from the precompiled package on SourceForge, by forcing the calling convention of my EXE to __stdcall (/Gz) (In C++->Adanced).
You should use __cdecl (/Gd) instead!

Related

Problem compiling exiv2 code in VS 2022: Undefined Reference/symbol error

I have some code in Linux that uses exiv2 to read the metadata of a folder full of photos, depending on requirements it can reconstruct missing metadata, and stamp a logo as well as lat/long onto the photos. I'm trying to port it into windows visual studio so that I can make a GUI and let other people use it.
I downloaded the MSVS source code for exiv2, I've added the include folder and lib folders both in the include line and the linker line in the project properties dialogue. When I try to compile I get an undefined reference error whenever an EXIV2 object is called.
I'm guessing I have some sort of linking or library building issue. I built it on linux using g++ and had no problems. If I weren't trying to make a GUI I'd probably just get a windows version of g++ to compile with.
This is one of the error messages I'm getting.
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "__declspec(dllimport) public: class std::_List_iterator<class std::_List_val<struct std::_List_simple_types > > __thiscall Exiv2::ExifData::begin(void)" (_imp?begin#ExifData#Exiv2##QAE?AV?$_List_iterator#V?$_List_val#U?$_List_simple_types#VExifdatum#Exiv2###std###std###std##XZ) referenced in function "int __cdecl uptake(class std::vector<struct EANphoto,class std::allocator > &,char *,char *)" (?uptake##YAHAAV?$vector#UEANphoto##V?$allocator#UEANphoto###std###std##PAD1#Z) CAPconsole C:\Users\ [redacted] \source\repos\CAPconsole\CAPconsole.obj 1
I've tried adding include files for both the "include" and the "lib" folders to both the include line and to the linker. I also tried to compile the sample program that came with Exiv2, and I get the same error,so I'm guessing it's a VS setting issue.
So, the way to do this is as follow:
In "Properties" … "C/C++" … "General" … "Additional Include Directories", add the include and lib folders, and do the same in
"Linker" … "Additional Library Directories".
Finally, in "Linker" … "Command Line" … "Additional Options", write in "exiv2.lib".

Getting "undefined reference to" when using the lib for ARM, but not when compiling it

For one of my Qt Embedded projects I'm using a external Qt lib called SMTPEmail. This lib needs to be compiled before being included into a project, something that I managed to do successfully both for Qt 4.8 ARM and for Desktop.
The problem I'm getting is that when I include the headers into my project and include the library in the .pro, the linker gives me
(path_to_libs)/libSMTPEmail.so: undefined reference to `QSslSocket::connectToHostEncrypted(QString const&, unsigned short, QFlags<QIODevice::OpenModeFlag>)'
(path_to_libs)/libSMTPEmail.so: undefined reference to `QSslSocket::QSslSocket(QObject*)'
collect2: ld returned 1 exit status
make: *** [re8k_interface-tgt] Error 1
but only for compiling for ARM. IOW compiling the lib for both ARM and Desktop goes OK, compiling the project for Desktop using the lib goes OK but compiling it for ARM using the lib goes wrong.
Following this forum thread I suspected this could be due to missing the link to the library file of openssl (the project points to different lib folders when compiling for different environments). So I searched for all "openssl" related files inside the compiler for ARM (arm-arago-linux-gnueabi) and included in the same folder where the .so is located; same error. I then suspected the lib itself had other dependencies which were not in the path_to_libs, so I did a readelf -d libSMTPEmail.so and later in the .so.1 and readelf did return some lib dependencies that were not inside the same folder of the library. I then copied all such dependencies to the folder and got no success either.
So what could be happening? All dependencies known by me were put in place and I still get the error only for the situation where the lib is included by another project compiled for ARM.
You need to point your QMake where your libs and header file is in your .pro file;
So find where your library is assume /usr/local/include then ;
INCLUDEPATH += /usr/local/include
Add which libs you will use;
LIBS += -lSMTPEmail
You can check my answer here;
Two things stand out for me in your question:
1.
undefined reference to
This error message means that there was an error in the linking step of compilation. This occurs when you include a header to a function/class/variable but don't have the definition included in your own sources, or you do not link in a static library that does.
Searching for dependencies in libraries that are already compiled (.dll or .so) is too late, the compiler is looking for a static link, not a dynamic link.
2.
compiling the lib for both ARM and Desktop goes OK, compiling the
project for Desktop using the lib goes OK but compiling it for ARM
using the lib goes wrong.
This suggests that you are using conditional compilation in your .pro file that does a "both" compilation, a "desktop only" compilation and a "ARM only" compilation. If this is correct, you need to examine your compilation instructions for your "ARM only" compilation.
The error message itself refers to you using two functions from the QSslStock class. These are part of the QtNetwork module so you should have the following in your .pro file in order for the necessary links to be formed.
Qt += network

Is cmake work differently for Linux and WinCE platform?

We are working on the Project which contains thousands of the cmake files. Code is develop on the Linux platform. Now we are porting this project for Windows CE platform.
We are facing lots of linking error when change some of the functionality to project in Linux platform.
We are resolving this linking error by adding respective library in target_link_library of cmake file.
I am surprised how it worked for Linux? And it is failing for Windows CE.
Difference between the platform additional library :
Linux Platform
[A.Common]
[B]
[C]
[C]
[C]
[TLV]
[D]
[Boost.Filesystem]
[Boost.System]
[Boost.Thread]
[Boost.Atomic]
[Boost.DateTime]
[Boost.Chrono]
[Platform.Socket]
[D]
[C]
[C]
[Debug]
[SQLite]
[C]
[C]
[C]
[Meta]
[E]
WinCE
[A.Common]
[B]
[E]
[E]
[E]
[TLV]
[D]
[Boost.Filesystem]
[Boost.System]
[Boost.Thread]
[Boost.Atomic]
[Boost.DateTime]
[Boost.Chrono]
[Platform.Socket]
[D]
[C]
[C]
[Debug]
[SQLite]
[ZLib]
[C]
[C]
[C]
[Meta]
[E]
Any help will be appreciated.
From my experience I can confirm that moving with a CMake project to a new platform is also a check if you have setup your library dependencies correctly.
If we are talking about SHARED libraries, please first confirm that you have correctly exported your function declarations:
Creating and using shared libraries with different compilers on different operating systems
cmake link shared library on Windows
If you are using STATIC libraries, I see two possible approaches (not taking linker errors about missing symbols from system libraries into account):
You may have some if statements checking for this or that platform in your CMake code. So first check that your library dependencies are truly the same by activating GLOBAL_DEPENDS_DEBUG_MODE:
set_property(GLOBAL PROPERTY GLOBAL_DEPENDS_DEBUG_MODE 1)
Put it in your main CMakeList.txt file and compare the output on both platforms.
The linking order is also important, not only that the library is named somewhere on the linker command line. I like to quote from the Beginner's Guide to Linkers:
Another important detail to note is the order of events; the libraries are consulted only when then the normal linking is done, and they are processed in order, left to right. This means that if an object pulled in from a library late in the link line needs a symbol from a library earlier in the link line, the linker won't automatically find it.
Use the following CMake test program to see if your linker does a multi-path scan for open symbols. I've tested it with GCC 4.8.1 (doesn't work) and Visual Studio 2013 (does work). So I don't have your environments, so please do the test yourself.
The program is deliberately setup to have a wrong library linkage order by not using normal library dependencies like target_link_libraries(lib1 lib2) (a rule of thumb is to add as dependecies all libraries you directly include a header file from), but by giving the order in a global list to main:
cmake_minimum_required(VERSION 2.8)
project(WrongLinkOrderTest CXX)
set(_idx 1)
while (_idx LESS 10)
math(EXPR _next_idx "${_idx} + 1")
file(WRITE lib${_idx}.h "int lib${_idx}_func();")
file(WRITE lib${_idx}.cc "#include \"lib${_next_idx}.h\"\nint lib${_idx}_func() { return lib${_next_idx}_func(); }")
add_library(lib${_idx} lib${_idx}.cc)
# NOTE: This would fix it
#target_link_libraries(lib${_idx} lib${_next_idx})
set(_idx "${_next_idx}")
endwhile()
file(WRITE lib${_idx}.h "int lib${_idx}_func();")
file(WRITE lib${_idx}.cc "int lib${_idx}_func() { return 0; }")
add_library(lib${_idx} lib${_idx}.cc)
file(WRITE main.cc "#include \"lib1.h\"\nint main() { return lib1_func(); }")
add_executable(main main.cc)
if (CMAKE_COMPILER_IS_GNUCXX)
#target_link_libraries(main "-Wl,--start-group")
endif()
while (_idx GREATER 0)
math(EXPR _next_idx "${_idx} - 1")
# NOTE: Here it's done wrong
target_link_libraries(main lib${_idx})
set(_idx "${_next_idx}")
endwhile()
if (CMAKE_COMPILER_IS_GNUCXX)
#target_link_libraries(main "-Wl,--end-group")
endif()
By default, it will show the following error with GCC:
liblib1.a(lib1.cc.obj):lib1.cc:(.text+0x7): undefined reference to `lib2_func()'
Uncomment the target_link_libraries(lib${_idx} lib${_next_idx}) or --start-group/--end-group lines to fix it.
Additional References
How do I list the defined make targets from the command line?
Why does the order in which libraries are linked sometimes cause errors in GCC?
GCC: what are the --start-group and --end-group command line options?
Porting code to Windows and getting "error LNK2019: unresolved external symbol ...."
Use -Wl,--start-group and -Wl,--end-group for Android linking

VS2013 VC++ libcurl LNK2028 LNK2019 linking errors

I'm trying to get Libcurl working in my VC++ project on VS2013 Community. I've been toiling with this for many hours, and I've tried essentially everything on Stack and Google about it, to no avail.
My most recent attempt was sparked by this answer, that mentions libcurl as a Nuget package. This is good, because it comes with all of the .lib files required. I have however run into the same problem as this fellow, in that I'm getting a linker error.
I'm importing the following libraries as 'Additional Dependencies' in the Project Configuration settings under Linker -> Input:
libcurl.lib;libeay32.lib;ssleay32.lib;Ws2_32.lib;libssh2.lib;zlib.lib;wldap32.lib;
Obviously, each one of these .lib files is in a different directory inside the nuget package folder inside the project folder, so under Linker -> General my 'Additional Library Directories' is set to:
"PATHTOPROJECT\packages\curl.7.30.0.2\build\native\lib\v110\x64\Release\static";
"PATHTOPROJECT\packages\zlib.1.2.8.1\build\native\lib\v110\Win32\Release\static\cdecl";
"PATHTOPROJECT\packages\openssl.1.0.1.21\build\native\lib\v110\x64\Release\static\cdecl";
"PATHTOPROJECT\packages\libssh2.1.4.3.1\build\native\lib\v110\x64\Release\static\cdecl";
I am trying to use static libraries, because I don't want to drag .dll's with my project executable if I can avoid it. It's also worth noting that my most recent attempt at using libcurl also includes SSL and whatnot, but I don't actually need those extras. Really, I just need a static libcurl library, but I can't find or compile one it seems.
In my code, I'm also using this include: #include "curl/curl.h"
I've also added the Additional Include directory correctly under 'Additional Include Directories' under C/C++ -> General:
PROJECTPATH\packages\curl.7.30.0.2\build\native\include
Now, this all compiles fine as long as I don't try and use curl in the project code.
So, the errors when I do try and use curl in the code:
CURL *curl;
curl = curl_easy_init();
That piece of code generates these errors:
1>Main.obj : error LNK2028: unresolved token (0A000453) "extern "C" void * __cdecl curl_easy_init(void)" (?curl_easy_init##$$J0YAPAXXZ) referenced in function "private: class System::String ^ __clrcall Project::Main::CurlRequest(class System::String ^,class System::String ^,class System::String ^,class System::String ^)" (?CurlRequest#Main#Project##$$FA$AAMP$AAVString#System##P$AAV34#000#Z)
1>Main.obj : error LNK2019: unresolved external symbol "extern "C" void * __cdecl curl_easy_init(void)" (?curl_easy_init##$$J0YAPAXXZ) referenced in function "private: class System::String ^ __clrcall Project::Main::CurlRequest(class System::String ^,class System::String ^,class System::String ^,class System::String ^)" (?CurlRequest#Main#Project##$$FA$AAMP$AAVString#System##P$AAV34#000#Z)
There is of course more to my curl using code, but that bare minimum shows the basics of the linking error. What can I do to fix this? As I mentioned before I've tried a whole range of solutions, some of which I'll list below:
Many answers to the questions regarding problems using libcurl in VS projects mention downloading libcurl from http://curl.haxx.se/latest.cgi?curl=win32-devel-msvc, but that link doesn't work as of now, so I can't use it.
More answers say to just compile libcurl from source. The source comes with .vcproj files, none of which compile for me without hundreds of errors, so that's out.
Solved.
This Github project is a .bat file that downloads the libcurl source, compiles it, and makes the libraries (both static and dynamic). Importing the static library created by this (libcurl_a.lib) and using it in my project along with the include directory has worked.
I'll leave this question with an unaccepted answer in case someone can provide other helpful steps that would have fixed the problem I had in using the nuget package installation.
Finally.

VC++ Project: unresolved external symbol HidD_GetHidGuid

I'm trying to build a project (for ARM) to facilitate the communication over an HID device. Here are the details of what I have:
PC with Windows 7
Visual Studio 2011 Developer Preview
WDK
A microsoft library function FindKnownHidDevices() makes a call to HidD_GetHidGuid() in my project. However, Linker doesn't like this and complains:
*error LNK2019: unresolved external symbol HidD_GetHidGuid referenced in function "unsigned char __cdecl FindKnownHidDevices*
I already have all of following workarounds in my project with no success so far:
Include hid.lib & SetupAPI.lib in Linker -> Input -> Additional Dependencings under configuration manager of VS 2011
Include hidsdi.h under "VC++ Directories -> Include Directories" in Configuration Manager of VS 2011
Include hidsdi.h in the .h version of .c file which makes a call to the function HidD_GetHidGuid() using extern "C"
What might I be missing?
If the code compiles OK, then the compiler is finding the correct header files. As the linker is failing, this suggests that it cannot find the specified .lib files.
Have you downloaded and installed the WoA SDK? The required lib files may not actually be in the linker folder for ARM targets (they're not in the default installation for Studio 11 Developer Preview).

Resources