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?
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
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)
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.
I am new to boost and am trying to compile the most basic program that includes boost/asio/ssl.
I am working on ubuntu 13.10 64bit and installed the latest boost like this:
sudo apt-get install boost-all-dev
Here is the magnificent code I am trying to compile:
#include <stdio.h>
#include <boost/asio/ssl/stream.hpp>
int main(){
printf("Hi\n");
return 0;
}
I googled and googled and the few hints I was able to find suggests I use some boost library flags. So I did:
g++ my_prog.cpp -o my_prog.bin -lboost_system -lboost_thread -lpthread
and all the time I get the same error message (no matter how many -l i shove in):
/tmp/cci6dJdZ.o: In function `boost::asio::ssl::detail::openssl_init_base::do_init::do_init()':
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initC2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initC5Ev]+0x2c): undefined reference to `SSL_library_init'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initC2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initC5Ev]+0x31): undefined reference to `SSL_load_error_strings'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initC2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initC5Ev]+0x36): undefined reference to `OPENSSL_add_all_algorithms_noconf'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initC2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initC5Ev]+0x47): undefined reference to `CRYPTO_num_locks'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initC2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initC5Ev]+0xcc): undefined reference to `CRYPTO_set_locking_callback'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initC2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initC5Ev]+0xd6): undefined reference to `CRYPTO_set_id_callback'
/tmp/cci6dJdZ.o: In function `boost::asio::ssl::detail::openssl_init_base::do_init::~do_init()':
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD5Ev]+0x13): undefined reference to `CRYPTO_set_id_callback'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD5Ev]+0x1d): undefined reference to `CRYPTO_set_locking_callback'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD5Ev]+0x22): undefined reference to `ERR_free_strings'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD5Ev]+0x2c): undefined reference to `ERR_remove_state'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD5Ev]+0x31): undefined reference to `EVP_cleanup'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD5Ev]+0x36): undefined reference to `CRYPTO_cleanup_all_ex_data'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD5Ev]+0x40): undefined reference to `CONF_modules_unload'
my_prog.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD5Ev]+0x45): undefined reference to `ENGINE_cleanup'
/tmp/cci6dJdZ.o: In function `boost::asio::error::detail::ssl_category::message(int) const':
my_prog.cpp:(.text._ZNK5boost4asio5error6detail12ssl_category7messageEi[_ZNK5boost4asio5error6detail12ssl_category7messageEi]+0x1d): undefined reference to `ERR_reason_error_string'
collect2: error: ld returned 1 exit status
Or in short: it complaining about missing symbols like:
undefined reference to `SSL_library_init'
or:
undefined reference to `ERR_reason_error_string'.
Another funny fact is that if I change the include:
#include <boost/asio/ssl/stream.hpp>
for example to
#include <boost/asio/ssl/stream_base.hpp>
it compiles just fine (even without -lboost_system and all others..).
I spent a whole day on this and still can't for the life of me figure this out.
Any ideas guys?
In vs2013, when I include <boost/asio/ssl.hpp> I see
unresolved external symbol _CONF_modules_upload
unresolved external symbol _ERR_reason_error_string
I solved those issues by adding
libcrypto32MD.lib
in Linker/Additional Dependencies
I had written my program using XCode. It works without any problems on the Mac. I was told that our projects would be compiled on Linux machines so I wanted to make sure it works on Linux before submitting. When I tried to compile on Linux, it gave me some reference errors:
/tmp/cckwoehj.o: In function `main':
main.cpp:(.text+0x9): undefined reference to `ReadFile()'
/tmp/cckwoehj.o: In function `leftSearch(NODE*, int)':
main.cpp:(.text+0x38b): undefined reference to `conflict(int**)'
main.cpp:(.text+0x3ca): undefined reference to `removeAllRowsWithLiteral(int**, int)'
main.cpp:(.text+0x3ec): undefined reference to `removeAllSpecifiedLiteral(int**, int)'
main.cpp:(.text+0x4ec): undefined reference to `conflict(int**)'
main.cpp:(.text+0x522): undefined reference to `unitPropagation(int**)'
main.cpp:(.text+0x538): undefined reference to `conflict(int**)'
/tmp/cckwoehj.o: In function `rightSearch(NODE*, int)':
main.cpp:(.text+0x992): undefined reference to `conflict(int**)'
main.cpp:(.text+0x9d4): undefined reference to `removeAllRowsWithLiteral(int**, int)'
main.cpp:(.text+0x9f3): undefined reference to `removeAllSpecifiedLiteral(int**, int)'
main.cpp:(.text+0xaf3): undefined reference to `conflict(int**)'
main.cpp:(.text+0xb29): undefined reference to `unitPropagation(int**)'
main.cpp:(.text+0xb3f): undefined reference to `conflict(int**)'
collect2: ld returned 1 exit status
The stuff I found online all talks about templates. I don't use templates. I also found one about a 32 and 64 bit OS problem which I do not have.
I have a function called ReadFile that is declared in a header. I call it from main and include that header file in main. I am not sure what the problem is. If OS X compiles and runs and Linux doesn't, I am assuming there is something OS X does internally that it thinks is obvious but Linux is not written that way. Is this a linker error? I think OS X would be programmed to do that internally.
You're declaring your functions in a header, but there's no definition, leading to undefined references when you try to turn main.cpp into an executable.
You need to compile all your source files and link them together (in the right order).
g++ -c ReadFile.cpp
#...
g++ -c main.cpp
g++ main.o ... ReadFile.o -o my_executable
Where the object files to the right rely on no unresolved symbols defined in the object files to its left on the commandline.
XCode is an IDE so I guess it handles the linking order for you.
To handle all this stuff automatically you'll have to also use an IDE and/or write a Makefile.
If you insist in a single command, yes, you can, as long as you include all source files in the line (e.g. g++ *.cpp), but this forces a full recompilation for every single change.