Undefined reference to 'dlsym' and 'dlopen' - c++

I am compiling using arm-linux-gnueabi-g++ version 4.7.3.
I have the arm-linux-gnueabi libraries installed at location:
/usr/arm-linux-gnueabi/lib, it contains libdl.a, libdl.so, libdl.so.2,
and libdl-2.19.so.
libdl.so links to libdl.so.2 which links to libdl-2.19.so.
I am trying to link against the dl library (see command string below), but I always get the undefined reference errors.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
If I compile using g++ 4.8.2 using the following commend then my program compiles, links, and executes fine.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Obviously it can't find the libdl.so library; I thought that by adding the path to the location of the appropriate library by using the -L flag would fix the problem, but it didn't.
What am I missing with the ARM compiler command?

Well, I found the answer, I needed -Wl,--no-as-needed flag before the -ldl. I had run across this flag before I asked the question, but apparently mistyped it because it hadn't worked for me.
I don't understand why the flag is needed, but the code does finish linking now.
A SO user here says that it has to do with recent (2013 as of the user's post) versions of gcc linking to --as-needed.

Related

g++ - linking stage (-L flag) not working

I am new to C++ and am trying to figure out where in the compiling process my error is. Apologies, if this question is unclear, I'm not sure what information to provide.
Reference information: The directory "mbedtls/lib/" contains two .a (archive) files, "libmbedtls_SGX_t.a" and "libmbedtls_SGX_u.a".
I am using a Makefile, which runs the following commands in order. The commands seem to run error free:
cc -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -c App/Enclave_u.c -o App/Enclave_u.o
g++ -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -std=c++11 -c App/App.cpp -o App/App.o
g++ -m64 -O2 -fPIC -Wno-attributes -IApp -I/opt/intel/sgxsdk/include -Imbedtls/include -DNDEBUG -UEDEBUG -UDEBUG -std=c++11 -c App/sgx_utils/sgx_utils.cpp -o App/sgx_utils/sgx_utils.o
After running those commands, it runs this command:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 -L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread -Lmbedtls/lib/ -lsgx_uae_service_sim
However, this command produces an error:
App/Enclave_u.o: In function `Enclave_ocall_print_string':
Enclave_u.c:(.text+0x9): undefined reference to `ocall_print_string'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_free':
Enclave_u.c:(.text+0x28): undefined reference to `ocall_mbedtls_net_free'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_recv_timeout':
Enclave_u.c:(.text+0x54): undefined reference to `ocall_mbedtls_net_recv_timeout'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_send':
Enclave_u.c:(.text+0x71): undefined reference to `ocall_mbedtls_net_send'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_recv':
Enclave_u.c:(.text+0x91): undefined reference to `ocall_mbedtls_net_recv'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_usleep':
Enclave_u.c:(.text+0xa8): undefined reference to `ocall_mbedtls_net_usleep'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_set_nonblock':
Enclave_u.c:(.text+0xc9): undefined reference to `ocall_mbedtls_net_set_nonblock'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_set_block':
Enclave_u.c:(.text+0xe9): undefined reference to `ocall_mbedtls_net_set_block'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_accept':
Enclave_u.c:(.text+0x119): undefined reference to `ocall_mbedtls_net_accept'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_bind':
Enclave_u.c:(.text+0x144): undefined reference to `ocall_mbedtls_net_bind'
App/Enclave_u.o: In function `Enclave_ocall_mbedtls_net_connect':
Enclave_u.c:(.text+0x164): undefined reference to `ocall_mbedtls_net_connect'
collect2: error: ld returned 1 exit status
Makefile:184: recipe for target 'app' failed
I don't know why this error is being produced. My last command includes the flag -Lmbedtls/lib/ which should resolve these undefined references by linking the archive files (specifically - libmbedtls_SGX_u.a) to the executable. However, clearly this is not happening.
Do I need to link the archive files earlier in the compile process? I.e - instead of using the flag -Lmbedtls/lib/ in the final g++ command, should I be using it in both of the previous g++ commands?
Update: Adding the flag -Lmbedtls/lib/ to the previous 2 g++ commands (the ones that generated App.o and sgx_utils.o) did not change anything. The error remains.
As has been mentioned in the comment, only specifying a directory with -L without specifying the name of a library in that directory is useless.
So you have to additionally specify the library names with -l.
So instead of:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 \
-L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread \
-Lmbedtls/lib/ \
-lsgx_uae_service_sim
Use:
g++ App/Enclave_u.o App/App.o App/sgx_utils/sgx_utils.o -o app -m64 -O2 \
-L/opt/intel/sgxsdk/lib64 -lsgx_urts_sim -lpthread \
-Lmbedtls/lib/ -lmbedtls_SGX_t -lmbedtls_SGX_u \
-lsgx_uae_service_sim

linking failed with undefined reference to libboost_thread

I am compiling an opensource project to run on my machine which is this project. It requires boost library so I installed the Boost_1_55 library on my ubuntu machine but the compiling process was not successfully finished by printing out some error messages as follows.
libtool: link: g++ -g -O3 -Wall -DKENLM_MAX_ORDER=6 -W -Wall -Wno-sign-compare -I./.. -pthread -I/usr/include -g -O2 -o .libs/query query_main.o ./.libs/libklm.so ../util/.libs/libklm_util.so -lz -L/usr/lib/x86_64-linux-gnu -lboost_program_options -lboost_thread -lboost_system -lpthread -lrt -pthread
../util/.libs/libklm_util.so: undefined reference to `boost::thread::join()'
../util/.libs/libklm_util.so: undefined reference to `boost::thread::~thread()'
./.libs/libklm.so: undefined reference to `boost::thread::start_thread()'
collect2: ld returned 1 exit status
This answer seems the solution for my problem but the result of ls -al /usr/local/lib | grep thread showed me like below.
libboost_thread.a
libboost_thread.so -> libboost_thread.so.1.55.0
libboost_thread.so.1.49.0
libboost_thread.so.1.55.0
I don't know what else to check more. Thank you in advance for your help.
You can try to add /usr/local/lib to LD_LIBRARY_PATH like this
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
You have the static Boost library object (libboost_thread.so) but do you have the Boost development files installed? Check to see if the /usr/include/boost/thread directory exists and has *.hpp files in it. If not you may need to install the libboost-thread-dev package for your distribution or download the header files directly from Boost.org.

undefined reference to boost library

I am getting static link errors to boost's non header only library. Let's solve it together:
The final binary is supposed to be built using libraries(libbasemainif.a for example) separately built as the higher layers. One of those layers, uses boost::filesystem.
Have a look at the simplified vesrion of g++ command:
g++ /mylis/1.a /mylibs/2.a
-L/myboost/Linux-x86_64/lib64
-Wl,-rpath,/myboost/Linux-x86_64/lib64 -Wl,-Bstatic
-lboost_thread-mt -lboost_system-mt -lboost_filesystem-mt -lboost_date_time-mt
-Wl,-Bdynamic
-Wl,-rpath,/myinstall/usr/local/lib64 -L/myinstall/usr/local/lib64 -Wl,
-Bstatic -lmyblahblah-static -Wl,-Bdynamic **-lbasemainif** -lbaseif -ldl -rdynamic -lz -lrt
-L/mypackage1/Linux-x86_64/debug/lib -L /mypackage2/18.1/Linux-x86_64/debug/lib -lpthread -Wl,-rpath,$ORIGIN/../lib64
and this is the error :
/blahblah/lib/libbasemainif.a(errorreportfile.o):
In function `boost::filesystem3::remove(boost::filesystem3::path const&, boost::system::error_code&)':
/myboost/Linux-x86_64/include/boost/filesystem/v3/operations.hpp:411:
undefined reference to boost::filesystem3::detail::remove(boost::filesystem3::path const&, boost::system::error_code*)'
collect2: ld returned 1 exit status
If I remove -Wl,-Bstatic the g++ command executes successfully.But this is not an option in production.
The libboost_filesystem.a is available. And all of the laibraries in every layer used boost from the same location.
Can you please tell me why I am getting this error? Thank you
You should pass to gcc library that implements function after the piece which references it. Something like: -lbasemainif -lboost_filesystem-mt. This is how gcc linker resolves dependencies.
Here is great detailed explanation.

Compiling/linking g++ using -m32 flag on 64bit System -> manually add -ldl?

It was a long long way, but I managed to include a shared file (libpi_pi_gcs2.so) included into C++ code under Linux Mint 17 Cinnamon 64-bit. Now as the dust is saddled I have a question left... let me summarize:
I copied libpi_pi_gcs2.so in usr/local/lib.
Trying to compile main.cpp with
g++ -Wall -o test main.cpp -lpi_pi_gcs2
faild, returning
/usr/bin/ld: skipping incompatible //usr/local/lib/libpi_pi_gcs2.so when searching for -lpi_pi_gcs2
/usr/bin/ld: cannot find -lpi_pi_gcs2
collect2: error: ld returned 1 exit status
I talked with my flatmate, and he guessed maybe the shared library is only 32bit compatible.
Using -m32 let me take a step forward! But yielding new errors.
I googled and it seemed like I needed C32 std libraries. And I installed:
apt-get install ia32-libs
Again the old error messages disappeared and new one arose, after googling around I installed:
apt-get install g++-multilib
Trying to compile it now using
g++ -Wall -o test -m32 main.cpp -lpi_pi_gcs2
Let to the error messages:
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlsym'
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlopen'
//usr/local/lib/libpi_pi_gcs2.so: undefined reference to `dlclose'
The solution was the following command:
g++ -Wall -o test -m32 main.cpp -lpi_pi_gcs2 -ldl
Now my question:
Is it necessary to explicitly state -ldl i.e. link with libdl.so because the -m32 flag declares everything what follows to be 32bit and hence the linker would without explicitly
writing -ldl search for a 32bit libdl.so? I guess mine is 64 bit I am working on a 64bit system.
What do you think? More details and background informations are appreciated.
Greetings,
newandlost

Error while executing make command to create moses\scripts\training\memscore

I have to implement the machine translation system hence I am planning use moses but I facing following error while executing make command on Cygwin:
Administrator#diebold-69b7050 /cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore$ make
make all-am
make[1]: Entering directory `/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses
scripts/training/memscore'
g++ -I/usr/include -Wall -ffast-math -ftrapping-math -fomit-frame-pointer -g -O2 -o memscore.exe phrasetable.o memscore.o scorer.o lexdecom.o -lz -lm
phrasetable.o: In function `_ZlsRSoRK15PhraseAlignment':
/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore/phrasetable.cpp:111: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `__tcf_0':
/cygdrive/c/JT/NewSetup/Moses/moses-2010-08-13/moses/scripts/training/memscore/datastorage.h:31: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `_ZN14PhrasePairInfo12realloc_dataEj':
/usr/include/boost/pool/simple_segregated_storage.hpp:97: undefined reference to `boost::system::system_category()'
phrasetable.o: In function `_ZNK14PhrasePairInfo14get_alignmentsEv':
Please don't give me suggestion like linker error because I am completely fad up of trying linker option.
I think, I have some Cygwin->Boot library problem. Can you suggest me where I am wrong?
You are mssing -lboost_system on your compilation commands.
Some of the boost libraries are header only. Others need to be compiled. And the libraries sometimes depend on each other. In this case you are using some boost library which needs -lboost_system. Add it so that it gets linked with your project.
and it should be on this line. Where linking is done
g++ -I/usr/include -Wall -ffast-math -ftrapping-math -fomit-frame-pointer -g -O2 -o memscore.exe phrasetable.o memscore.o scorer.o lexdecom.o **-lboost_system** -lz -lm