I have a written a small c++ project on a linux box. I am able to compile and link the code properly with the -I and _L,-l commands.
I read on the net that while running the code, we need to have the path to the .so files in the LD_LIBRARY_PATH variable. Now the box that I am using is 64 bit and there is another application that uses 32 bit version of the library. So the LD_LIBRARY_PATH has paths to both versions of the library.
When I try to run my application eg. ./xyz arg1 arg2, I get the following error
./DaemonCpp: error while loading shared libraries: libldap.so: wrong ELF class: ELFCLASS32
I think this is because it is trying to load 32 bit version instead of 64 bit version. I tried changing the order in which the paths are mentioned in LD_LIBRARY_PATH such that the path to 64 bit comes first but still I get this error. Is there a way I could direct the app to use the 64 bit version of the library.
As a secondary question to my primary problem, when we are linking the c++ code, we do specify the library path using the -L and library name using -l. Then it should have known which library to use when I run the executable and I shouldn’t have got this error in the first place
In answer to your second question, you only used
-Lto specify the link time path. You need to also add-rpath=<path>for each path that you specify with-L. to get it to look at run-time as well.If you’re using the gcc/g++ driver for linking, then you should use
-Wl,-rpath=<path>e.g. library libfoo.so in
/opt/libs/lib64:In answer to your first question, if
LD_LIBRARY_PATHis causing issues, then your first port of call islddwhich shows the paths to the libraries that are being linked at run-time. Start with an empty string and work up from there.Thirdly, if the reason that
LD_LIBRARY_PATHneeds to be set is because there are libraries in /opt/lib, etc… then you should be adding these paths to your/etc/ld.so.confand usingldconfigto update the search map for these libraries. This will typically save you from needing theLD_LIBRARY_PATHvariable at all.