g++ linking to external library creates undefined references - c++

I'm trying to build a simple program to test the use of an external library but am having trouble linking it with g++. See command/results:
user#user-Nuvo-2510VTC:~/Desktop/WDT_DIO/linux/test$ g++ -o main main.o -lwdt_dio
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_unlink'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_mutexattr_settype'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_close'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_spin_lock'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_spin_unlock'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_create'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_spin_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_mutexattr_init'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_spin_destroy'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_post'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_open'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_getvalue'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `sem_wait'
/usr/lib/gcc/x86_64-linux-gnu/5/../../../../lib/libwdt_dio.so: undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
It looks like it isn't linking to some basic libraries that are part of the OS?
Details:
gcc --version --> 5.4.0 20160609
ldd --version --> 2.23
OS --> Ubuntu 16.04 x64, 4.8.0-36-generic kernel

You seem to be missing a link to the pthread library. Add -pthread to your compilation command:
g++ -o main main.o -pthread -lwdt_dio
Checking the man page for sem_wait(3) shows:
Link with -pthread.
Note: sem_wait was selected randomly, all of them should specify that

If you read the errors closely it says that the undefined comes from libwdt_dio.so. It also says to which function you have an undefined reference.
In this particular case, it is the pthread library. It should probably be mentioned as a dependency in WDT lib documentation.
You can add -pthread to your g++ command if you're compiling through the command line.

Related

Can't link OpenSSL libraries properly with Mingw on OS X

I have some RSA code, however, I can't get it to compile & link properly.
build.sh:
x86_64-w64-mingw32-g++ rsa.cpp -L/usr/local/opt/openssl/lib -lcrypto -lssl -I/usr/local/opt/openssl/include/ -static-libstdc++ -static-libgcc -o main.exe
result:
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0xe6): undefined reference to `BIO_new_mem_buf'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0xef): undefined reference to `BIO_f_base64'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0xf7): undefined reference to `BIO_new'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x10b): undefined reference to `BIO_push'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x130): undefined reference to `BIO_read'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x146): undefined reference to `BIO_free_all'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x177): undefined reference to `EVP_PKEY_new'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x193): undefined reference to `EVP_PKEY_assign'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x198): undefined reference to `EVP_MD_CTX_create'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x1a5): undefined reference to `EVP_sha256'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x1c7): undefined reference to `EVP_DigestVerifyInit'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x1f1): undefined reference to `EVP_DigestUpdate'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x218): undefined reference to `EVP_DigestVerifyFinal'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x234): undefined reference to `EVP_MD_CTX_cleanup'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x254): undefined reference to `EVP_MD_CTX_cleanup'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x26e): undefined reference to `EVP_MD_CTX_cleanup'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x2ac): undefined reference to `BIO_new_mem_buf'
/var/folders/8k/t6rljvrs6l32k49b3834_njr0000gn/T//ccKr56mH.o:rsa.cpp:(.text+0x2da): undefined reference to `PEM_read_bio_RSA_PUBKEY'
collect2: error: ld returned 1 exit status
both libcrypto.a and libssl.a exist in /usr/local/opt/openssl/lib. I'm building on OS X mingw compiler a windows application.
EDIT: objdump -d /usr/local/opt/openssl/lib/libcrypto.a > ~/libcrypto.a.txt
EDIT2: Tried grouping: x86_64-w64-mingw32-g++ rsa.cpp -L/usr/local/opt/openssl/lib -Wl,--start-group -lcrypto -lssl -Wl,--end-group -lws2_32 -lwsock32 -liphlpapi -I/usr/local/opt/openssl/include/ -static-libstdc++ -static-libgcc -o main.exe, same result

undefined reference to `boost::system::system_category(), but I already have -lboost_system

Getting this error from a cpp library I'm trying to port from Linux to Windows, using code::blocks. I'm new to c++ in general but especially new to Windows and CB.
I'm pretty sure I have added the system library accordingly, by which I mean I have compiled it using:
.\b2 -j 8 variant=release link=shared threading=single
Then gone to the lib directory, found boost_system-vc120-1_55.dll and renamed it boost_system.dll
The added F:\plus\boost\boost_1_55_0\stage\lib to the Build options, search directories, linker. (And F:\plus\boost\boost_1_55_0\ in search directories, compiler)
The -lboost_system is after the cpp files in the compile command, although for some reason it's listed twice, but I tried compiling in command prompt without the first one and gave same error.
This is the build output I get:
-------------- Build: Release in sendd (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -LF:\plus\wxWidgets-3.0.2\lib\gcc_dll -LF:\plus\boost\boost_1_55_0\stage\lib -o bin\Release\sendd.exe obj\Release\gsmencoding.o obj\Release\hexdump.o obj\Release\pdu.o obj\Release\senddApp.o obj\Release\senddMain.o obj\Release\smpp.o obj\Release\smppclient.o obj\Release\sms.o obj\Release\timeformat.o obj\Release\resource.res -s -mthreads -lwsock32 -lws2_32 -lboost_system -lwxmsw30u -lboost_system -mwindows
obj\Release\smppclient.o:smppclient.cpp:(.text+0x724): undefined reference to `boost::system::system_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text+0x769): undefined reference to `boost::system::system_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text+0xb28): undefined reference to `boost::system::system_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text+0xf28): undefined reference to `boost::system::system_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text+0xfaf): undefined reference to `boost::system::system_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text+0x13a5): more undefined references to `boost::system::system_category()' follow
obj\Release\smppclient.o:smppclient.cpp:(.text.startup+0x1b): undefined reference to `boost::system::generic_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text.startup+0x25): undefined reference to `boost::system::generic_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text.startup+0x2f): undefined reference to `boost::system::system_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text.startup+0x39): undefined reference to `boost::system::system_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text.startup+0x43): undefined reference to `boost::system::system_category()'
obj\Release\smppclient.o:smppclient.cpp:(.text.startup+0x4d): undefined reference to `boost::system::system_category()'
Any ideas? Thanks!

Getting unexpected "undefined reference to" errors when compiling with a static library

I've been trying to make a static library out of this: https://github.com/onlinecity/cpp-smpp , as I'd like to make a single file executable that uses it.
So far I have gone into src/smpp/ and created the .o files with:
g++-5 -c *.cpp -std=c++11 -I.
Then made them into an .a file with:
ar rvs libsmpp.a *.o
Then I'm trying to compile a small main.cpp file that uses the library with this:
g++-5 main.cpp -I/usr/local/include -static libsmpp.a -lboost_system -lboost_regex -lboost_date_time -lpthread -lm -lglog -lgflags -std=c++11
And I get some undefined reference errors:
/usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.a(condition_variable.o): In function `std::condition_variable::~condition_variable()':
(.text._ZNSt18condition_variableD2Ev+0x1): undefined reference to `pthread_cond_destroy'
/usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.a(condition_variable.o): In function `std::condition_variable::wait(std::unique_lock<std::mutex>&)':
(.text._ZNSt18condition_variable4waitERSt11unique_lockISt5mutexE+0x8): undefined reference to `pthread_cond_wait'
libsmpp.a(smppclient.o): In function `smpp::SmppClient::sendPdu(smpp::PDU&)':
smppclient.cpp:(.text+0x22a1): undefined reference to `google::LogMessage::LogMessage(char const*, int)'
smppclient.cpp:(.text+0x22b0): undefined reference to `google::LogMessage::stream()'
smppclient.cpp:(.text+0x22d4): undefined reference to `google::LogMessage::~LogMessage()'
smppclient.cpp:(.text+0x2534): undefined reference to `google::LogMessage::~LogMessage()'
libsmpp.a(smppclient.o): In function `smpp::SmppClient::readPdu(bool const&)':
smppclient.cpp:(.text+0x29d8): undefined reference to `google::LogMessage::LogMessage(char const*, int)'
smppclient.cpp:(.text+0x29e7): undefined reference to `google::LogMessage::stream()'
smppclient.cpp:(.text+0x2a0b): undefined reference to `google::LogMessage::~LogMessage()'
smppclient.cpp:(.text+0x2a48): undefined reference to `google::LogMessage::~LogMessage()'
libsmpp.a(smppclient.o): In function `boost::asio::detail::posix_event::~posix_event()':
smppclient.cpp:(.text._ZN5boost4asio6detail11posix_eventD2Ev[_ZN5boost4asio6detail11posix_eventD5Ev]+0x14): undefined reference to `pthread_cond_destroy'
libsmpp.a(smppclient.o): In function `boost::asio::detail::posix_event::posix_event()':
smppclient.cpp:(.text._ZN5boost4asio6detail11posix_eventC2Ev[_ZN5boost4asio6detail11posix_eventC5Ev]+0x2e): undefined reference to `pthread_cond_init'
libsmpp.a(smppclient.o): In function `void boost::asio::detail::posix_event::wait<boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex> >(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&)':
smppclient.cpp:(.text._ZN5boost4asio6detail11posix_event4waitINS1_11scoped_lockINS1_11posix_mutexEEEEEvRT_[_ZN5boost4asio6detail11posix_event4waitINS1_11scoped_lockINS1_11posix_mutexEEEEEvRT_]+0x5f): undefined reference to `pthread_cond_wait'
libsmpp.a(timeformat.o): In function `boost::date_time::month_formatter<boost::gregorian::greg_month, boost::date_time::iso_format<char>, char>::format_month(boost::gregorian::greg_month const&, std::ostream&)':
timeformat.cpp:(.text._ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_10iso_formatIcEEcE12format_monthERKS3_RSo[_ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_10iso_formatIcEEcE12format_monthERKS3_RSo]+0x33): undefined reference to `boost::gregorian::greg_month::as_short_string() const'
timeformat.cpp:(.text._ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_10iso_formatIcEEcE12format_monthERKS3_RSo[_ZN5boost9date_time15month_formatterINS_9gregorian10greg_monthENS0_10iso_formatIcEEcE12format_monthERKS3_RSo]+0x56): undefined reference to `boost::gregorian::greg_month::as_long_string() const'
collect2: error: ld returned 1 exit status
It compiles fine when I link to a dynamic version of the smpp library, although that's made using the cmake files included with the smpp lib. I didn't know how to modify them to output a static library, so I just used the g++ command at the top, and added the libraries listed in them (like glog) in the main.cpp compile command.
The "undefined reference to google::logMessage" seems especially strange, as I'm pretty sure thats part of glog, which is definitely loaded and included in the smppclient.cpp file itself.
Any ideas? Thanks.

linking c++ libraries on Linux

I'm running the following command:
g++ -m32 testLogin.cpp -L/root/c++/libs -ldvrnetsdk -o testLoginO -lpthread -lasound
the result:
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_channels'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutex_trylock'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_readi'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_access'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_strerror'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutexattr_settype'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_rate'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_close'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_malloc'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_period_size'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutex_timedlock'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutexattr_destroy'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_drain'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_free'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_create'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_open'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_set_format'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_writei'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_mutexattr_init'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_hw_params_any'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `snd_pcm_prepare'
/root/c++/libs/libdvrnetsdk.so: undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
The first thing I did after this error was to include the libraries -lpthread -lasound, I also worked on the parameters order but did not work. I appriciate any help.
did you try with just -pthread as the linker flag...sometimes it does notw ork with -lpthread...
This should work
g++ -m32 testLogin.cpp -L/root/c++/libs -ldvrnetsdk -pthread -lasound
With gcc, ordering of linking does matter.
So, try with different order.
g++ -m32 testLogin.cpp -L/root/c++/libs -lpthread -lasound -ldvrnetsdk -o testLoginO
See this question for order: Why does the order in which libraries are linked sometimes cause errors in GCC?
Alternatively, you can use start-group option.
gcc -m32 testLogin.cpp -L/root/c++/libs -Wl,--start-group -lpthread -lasound -ldvrnetsdk -Wl,--end-group -o testLoginO
Edit: As you still get the error, use nm on strings on your library and check if the symbols for which linker error is given are in the library or not. Check for library version. You might be on 64-bit kernel.

Linking errors when compiling boost asio on Linux

I have been running through a few tutorials of boost and the libraries it has.
I ran through the basic tutorial for boost:
http://www.boost.org/doc/libs/1_52_0/more/getting_started/unix-variants.html
and it worked fine. I am having issues with the asio tutorial:
http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/tutorial/tutdaytime1/src.html
It looks like linking errors, but I am including the same path as before:
g++ -I /usr/local/boost_1_52_0 test.cpp -o example
Error:
/tmp/cce4EZME.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x57b): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x587): undefined reference to `boost::system::generic_category()'
test.cpp:(.text+0x593): undefined reference to `boost::system::system_category()'
/tmp/cce4EZME.o: In function `boost::system::error_code::error_code()':
test.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/cce4EZME.o: In function `boost::asio::error::get_system_category()':
test.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
/tmp/cce4EZME.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
test.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
/tmp/cce4EZME.o: In function `boost::asio::detail::posix_thread::join()':
test.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2b): undefined reference to `pthread_join'
/tmp/cce4EZME.o: In function `boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)':
test.cpp:(.text._ZN5boost4asio6detail12posix_thread12start_threadEPNS2_9func_baseE[_ZN5boost4asio6detail12posix_thread12start_threadEPNS2_9func_baseE]+0x29): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
assuming you have installed your boost libraries in your /usr/lib directory and boost header files in /usr/include/boost and you have saved your code in a file named daytime1.cpp, you have to use the following command to compile the source code you have linked to:
$ g++ daytime1.cpp -o daytime -L /usr/lib/ -lboost_system -lboost_thread -lpthread