This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I am trying to use the library gnuplot-iostream in Netbeans with Windows/Cygwin, but merely including it in my source leads to compilation problems. I have already downloaded boost with the Cygwin interface. Yet, the message I get is:
mkdir -p dist/Debug/Cygwin_4.x-Windows
g++ -o dist/Debug/Cygwin_4.x-Windows/welcome_1 build/Debug/Cygwin_4.x-Windows/welcome.o -L../../../../../cygwin64 -L../../../../../cygwin64/lib/curl -L../../../../../cygwin64/bin -lcygcurl-4
build/Debug/Cygwin_4.x-Windows/welcome.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:222:(.text+0x51ca): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:223:(.text+0x51d6): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:224: undefined reference to `boost::system::system_category()'
/usr/include/boost/system/error_code.hpp:224:(.text+0x51e2): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `boost::system::system_category()'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/Cygwin_4.x-Windows/welcome_1.exe' failed
make[2]: *** [dist/Debug/Cygwin_4.x-Windows/welcome_1.exe] Error 1
I haven't made any reference to the library besides including it:
#include "gnuplot-iostream.h"
I also can't find any mention of this error. Does anybody know what I am doing wrong?
Thanks
I believe you are saying the problem is with not properly linking to the boost libraries, correct?
You see that -lcygcurl-4? This actually links your code with the curl implementation. The library search -L<xxx> pathes you have for boost look fine, but you'll have still need to specify particular libraries found there and need being linked to your program.
If you specify linking additional libraries using the -l<yyy> option, these are resolved as looking up lib<yyy>.a (or lib<yyy>.lib) in your actual build environment.
As mentioned just applying an #include <yyy.hpp> isn't enough to tell the toolchain (linker) where the actual implementation comes from. Add the library the same way you've added that cygcurl-4 libraray with your IDE/build system.
Transferred discussion from comments, to make this an answer (poor I know and I've marked the duplicates already. It's just because this simple clarification doesn't fit a comment very well apparently)
Just to keep this registered if someone else needs it. To solve the problem it was needed to add a -lboost_system-mt in the linker. To do so in Netbeans one has to right-click on the project, choose Properties, then go to Linker (which is under Build). There will be an option for Compilation Line. The flag should be added there.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 5 months ago.
I am trying to implement my own thumbnail provider by following Microsoft's thumbnail provider code sample. The code has been coming together nicely, but now I needed to figure out a way to compile (on Windows). MSYS2 appears to be a popular option.
g++ -shared -o GrmgThumbnailProvider.dll -lshlwapi -lgdi32 Dll.cpp GrmgThumbnailProvider.cpp
This causes errors:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Zyl\AppData\Local\Temp\cc5HnXOH.o:Dll.cpp:(.text$_ZN13CClassFactory14QueryInterfaceERK5_GUIDPPv[_ZN13CClassFactory14QueryInterfaceERK5_GUIDPPv]+0x31): undefined reference to `QISearch'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Zyl\AppData\Local\Temp\ccXzQLhd.o:GrmgThumbnailProvider.cpp:(.text+0x36f): undefined reference to `__imp_CreateDIBSection'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Zyl\AppData\Local\Temp\ccXzQLhd.o:GrmgThumbnailProvider.cpp:(.text$_ZN18CGrmgThumbProvider14QueryInterfaceERK5_GUIDPPv[_ZN18CGrmgThumbProvider14QueryInterfaceERK5_GUIDPPv]+0x31): undefined reference to `QISearch'
collect2.exe: error: ld returned 1 exit status
The libraries are definitely found: g++ complains if I change the library names in the -l argument to some nonsense. How is it these references cannot be resolved despite the required libraries being both present and found?
QISearch
CreateDIBSection
I saw this post here: ld: undefined symbol vs ld: undefined reference and I don't think it really talks about what the differences are between these 2 linker errors.
I usually get undefined reference linker errors when the linker can't find the definition of something (or I declared it and forgot to define it), but it seems undefined symbol serves the same purpose (the answer in the above linked post gives an example that seems to be aligned with when undefined reference arises, but maybe I'm missing some subtlety here?), so I'm a bit confused. Can someone clarify, perhaps with examples, of when each one arises during linking?
Edit: I didn't know the terminology for linker errors could be dependent on if using gnu, clang, etc...
In my case, I saw the undefined symbol error when using clang++ and the error was
stderr: ld.lld: error: undefined symbol: custom::linear::adapt()
Is undefined symbol the clang counterpart to gnu's undefined reference?
This question already has answers here:
How to use the fmt library without getting "Undefined symbols for architecture x86_64"
(3 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed last year.
I'd like to make a python-like dynamic integer class in C++ as an experiment. It requires me to change many integers to string types. As in here: https://www.zverovich.net/2013/09/07/integer-to-string-conversion-in-cplusplus.html it states that fmt format_int will be best for that kind of job.
So I installed fmt with command sudo apt-get install libfmt-dev
I added a header #include <fmt/format.h>
Used it in a simple test main
int main()
{
std::cout<<fmt::format_int(124236695253045068).str();
}
And tried to compile with g++ -lfmt -std=c++17 dynamic\ mem.cc
Output of the compiler is as follows:
/usr/bin/ld: /tmp/ccLBcSNm.o: in function `fmt::v6::format_int::format_decimal(unsigned long long)':
dynamic mem.cc:(.text._ZN3fmt2v610format_int14format_decimalEy[_ZN3fmt2v610format_int14format_decimalEy]+0x94): undefined reference to `fmt::v6::internal::basic_data<void>::digits'
/usr/bin/ld: dynamic mem.cc:(.text._ZN3fmt2v610format_int14format_decimalEy[_ZN3fmt2v610format_int14format_decimalEy]+0xad): undefined reference to `fmt::v6::internal::basic_data<void>::digits'
/usr/bin/ld: dynamic mem.cc:(.text._ZN3fmt2v610format_int14format_decimalEy[_ZN3fmt2v610format_int14format_decimalEy]+0xfa): undefined reference to `fmt::v6::internal::basic_data<void>::digits'
/usr/bin/ld: dynamic mem.cc:(.text._ZN3fmt2v610format_int14format_decimalEy[_ZN3fmt2v610format_int14format_decimalEy]+0x113): undefined reference to `fmt::v6::internal::basic_data<void>::digits'
/usr/bin/ld: /tmp/ccLBcSNm.o: in function `std::make_unsigned<long>::type fmt::v6::internal::to_unsigned<long>(long)':
dynamic mem.cc:(.text._ZN3fmt2v68internal11to_unsignedIlEENSt13make_unsignedIT_E4typeES4_[_ZN3fmt2v68internal11to_unsignedIlEENSt13make_unsignedIT_E4typeES4_]+0x2b): undefined reference to `fmt::v6::internal::assert_fail(char const*, int, char const*)'
collect2: error: ld returned 1 exit status
Do you have any ideas what went wrong? I don't usually link non-standard libraries so I don't have any idea what to do about it.
Use
$ g++ -std=c++17 dynamic\ mem.cc -lfmt
fmt is provided as a static library (.a). With those, the order is important as the linker takes out of a library only the objects which are needed to provide symbols to other objects or libraries which precede them in the command line. If you start with a library, there is only main which is missing and usually libraries don't provide main, so they are ignored. When putting the library after your source code, the symbols missing in your code are searched in the library.
(In case of circular dependencies, you may even have to provide a library several times)
I'm currently trying to build a simple IceCast supplier using libshout.
Unfortunately, I am unable to resolve some linker errors:
Invoking: Cygwin C++ Linker
g++ -L"D:\Programme\Cygwin\usr\x86_64-w64-mingw32\sys-root\mingw\include\ogg" -L"D:\Programme\Cygwin\usr\x86_64-w64-mingw32\sys-root\mingw\include" -L"D:\Programme\Cygwin\usr\x86_64-w64-mingw32\sys-root\mingw\include\vorbis" -o "LibshoutJavaAdapter.exe" ./src/LibshoutJavaAdapter.o -lshout -lvorbis -logg -lssl -lcrypto
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../lib/libshout.a(tls.o): In function `tls_setup_process':
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:200: undefined reference to `SSL_is_init_finished'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:200:(.text+0x74): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SSL_is_init_finished'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:203: undefined reference to `SSL_is_init_finished'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:203:(.text+0x8f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SSL_is_init_finished'
/usr/lib/gcc/x86_64-pc-cygwin/5.4.0/../../../../lib/libshout.a(tls.o): In function `tls_setup':
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:66: undefined reference to `OPENSSL_init_ssl'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:66:(.text+0x4ad): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `OPENSSL_init_ssl'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:67: undefined reference to `OPENSSL_init_ssl'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:67:(.text+0x4b9): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `OPENSSL_init_ssl'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:68: undefined reference to `SSLeay_add_all_algorithms'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:68:(.text+0x4be): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `SSLeay_add_all_algorithms'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:69: undefined reference to `OPENSSL_init_ssl'
/cygdrive/d/Programme/utility/libshout-2.4.1/src/tls.c:69:(.text+0x4c7): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `OPENSSL_init_ssl'
collect2: error: ld returned 1 exit status
I'm using CygWin's GCC 64 bit version, butalso got a 32 version to compile with if needed.
The build is triggered using make
My actual code up to now is pretty basic (I'm only just getting started with this library)
#include <iostream>
#include <shout/shout.h>
int main() {
shout_init();
std::cout << "!!!Hello World!!!" << std::endl; // prints !!!Hello World!!!
return 0;
}
I use the following libraries for the linker in this Order(-l):
shout -> the actual library i try to use
vorbis
ogg
ssl
crypto
From the look of the error I suppose, that this is related to ssl somehow, but i did include those didn't I ?
I already tried rotating the order, but that only created more errors, so I suppose the order is correct ...
Edit 1:
As nnovich-OK suggested, I altered my code like this to check OpenSSL:
#include <iostream>
//#include <shout/shout.h>
#include <ctype.h>
#include <openssl/ssl.h>
int main() {
//shout_init();
OPENSSL_init();
OPENSSL_INIT_SETTINGS * test = NULL;
OPENSSL_init_ssl(0,test);
//SSL_is_init_finished();
std::cout << "!!!Hello World!!!" << std::endl; // prints !!!Hello World!!!
return 0;
}
There resulting Linking error looks like this:
g++ -L"D:\Programme\Cygwin\usr\x86_64-w64-mingw32\sys-root\mingw\include\ogg" -L"D:\Programme\Cygwin\usr\x86_64-w64-mingw32\sys-root\mingw\include" -L"D:\Programme\Cygwin\usr\x86_64-w64-mingw32\sys-root\mingw\include\vorbis" -o "LibshoutJavaAdapter.exe" ./src/LibshoutJavaAdapter.o -lshout -lvorbis -logg -lssl -lcrypto
./src/LibshoutJavaAdapter.o: In function `main':
/cygdrive/d/Install und andere Sachen/Eclipse/C++/LibshoutJavaAdapter/Debug/../src/LibshoutJavaAdapter.cpp:18: undefined reference to `OPENSSL_init_ssl'
/cygdrive/d/Install und andere Sachen/Eclipse/C++/LibshoutJavaAdapter/Debug/../src/LibshoutJavaAdapter.cpp:18:(.text+0x27): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `OPENSSL_init_ssl'
collect2: error: ld returned 1 exit status
I see that there is a problem with OPENSSL_init_ssl from ssl.h but not with OPENSSL_init from crypto.h (witch intern is included by ssl.h)
Does that gives anyone any idea?
Any hints to resolving this Problem appreciated
I don't have the same setup to try, but provided info seems clear enough. Linker can't find functions from ssl library, so you need to focus on linking ssl. I think, issue scenario can be reduced to simple main function calling OPENSSL_init_ssl() (parameters doesn't matter, since you aren't going to execute it) and build process involving linking only openssl. Resolving this linking failure will help to resolve your current case. So let the Google and SO be with you :)
PS I'd rather put all this into comment, but SO newcomers aren't allowed to.
Edit:
Subsequent troubleshooting (check comments below) revealed actual root cause. System has several libssl instances (e.g. some app installation brought another version) and linker used instance not suitable for current build (e.g. 32bit version in building 64bit app). So the solution was to put linker into verbose mode and to check whether library paths are as expected.
I am trying to follow some sqlite C++ tutorial to be able to access a database from within C++ code on Linux. sqlite3 is installed and working, but when I try to compile the example code given on the link (Create a Table) I get the following error:
g++ build/test.o -o bin/test -pthread -L lib
build/test.o: In function `main':
/home/alexander/Projects/Test/src/test.cpp:22: undefined reference to `sqlite3_open'
/home/alexander/Projects/Test/src/test.cpp:24: undefined reference to `sqlite3_errmsg'
/home/alexander/Projects/Test/src/test.cpp:39: undefined reference to `sqlite3_exec'
/home/alexander/Projects/Test/src/test.cpp:42: undefined reference to `sqlite3_free'
/home/alexander/Projects/Test/src/test.cpp:46: undefined reference to `sqlite3_close'
collect2: error: ld returned 1 exit status
Do I need to install something else? Or do I need to set some paths?
I have no practice in compiling straight from the shell.
In CodeBlocks it is necessary to point the paths of the “sqlite3.h” header and the “libsqlite3.so” dll.
If the dll is not specified in Linker Settings, exactly the mentioned problem occurs.