问题
Thank you guys answering my previous problem on undefined reference to function. As you suggested, the reason under the problem is not linking the libraries. Now I have generated the executable file with: (the version of my g++ and gcc is 4.4.5. I am using Ubuntu 10.10.)
g++ -o ex_addinst ./ex_addinst.o -L/home/li/work/tools/lindo/lindoapi/bin/linux64 -m64 -llindo64 -lmosek64 -lconsub3 -lc -ldl -lm -lguide -lpthread -lsvml -limf -lirc
But there comes another problem, when I run the executable file with
./ex_addinst
errors appear: (I am not sure I should start a new problem or not currently....)
./ex_addinst: error while loading shared libraries: liblindo64.so.6.0: cannot open shared object file: No such file or directory
But liblindo64.so.6.0 exists in the folder of the lib ~/lindoapi/bin/linux64 which contains following files:
libconsub3.so libirc.so liblindojni.so libmosek64.so.5.0 lindo.par
libguide.so liblindo64.so liblindojni.so.6.0.3 libsvml.so placeholder
libimf.so liblindo64.so.6.0 libmosek64.so lindoapivars.sh runlindo
I have created a symbolic link between liblindo.so.6.0 and liblindo.so:
ln -sf liblindo64.so.6.0 liblindo64.so
There is '-llindo64' is the g++ command, so I thought liblindo64.so.6.0 should have been linked. I have tried to change -L to -Llib, but doesn't help.
Can anyone tell me what is wrong here? Thanks!
回答1:
You need to have the directory where the .so files live in in runtime linker's search path.
You can do that by changing the LD_LIBRARY_PATH environment variable like this:
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$HOME/lindoapi/bin/linux64
before starting your executable.
回答2:
If you are not going to install libraries currently under /home/li/work/tools/lindo/lindoapi/bin/linux64 into a system directory (/usr/lib, /usr/local/lib, etc.), then it is better to simply link the application such that it will just work(TM):
gcc -o ex_addinst ./ex_addinst.o \
-L/home/li/work/tools/lindo/lindoapi/bin/linux64 \
-Wl,-rpath=/home/li/work/tools/lindo/lindoapi/bin/linux64 \
-m64 -llindo64 -lmosek64 -lconsub3 -lc -ldl \
-lm -lguide -lpthread -lsvml -limf -lirc
This is preferable to always having to set LD_LIBRARY_PATH, because
- other people can run your executable (without having to set
LD_LIBRARY_PATH) and, - it doesn't slow down all the other applications (otherwise they will all search
LD_LIBRARY_PATHforlibc.so.6, etc.)
The reason your LD_LIBRARY_PATH setting didn't work (comment to Mat's answer) is that you used HOME where /home was intended.
回答3:
to sum the solution:
I add the path to ~./bashrc with:
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$HOME/.../lindoapi/bin/linux64
(after generating .o file)link the objective file with:
g++ -o ex_addinst ./ex_addinst.o -L/home/.../lindoapi/bin/linux64 -m64 -llindo64 -lmosek64 -lconsub3 -lc -ldl -lm -lguide -lpthread -lsvml -limf -lirc
来源:https://stackoverflow.com/questions/7455601/error-while-loading-shared-libraries