I'm working on a c++ project witch use mongodb as database.
I want to static link the mongodb driver.
I use this command to build the executable binary.
g++ -o ox one.cpp -pthread -Wall -std=c++11 \
-I /opt/mongo-cxx-driver/include/bsoncxx/v_noabi/ \
-I /opt/mongo-cxx-driver/include/mongocxx/v_noabi/ \
-L /opt/mongo-cxx-driver/lib/ \
-L /opt/mongo-c-driver/lib/ \
-Wl,-Bstatic -lmongocxx -lbsoncxx -lmongoc-1.0 -lbson-1.0 \
-Wl,-Bdynamic -lgcc_s -lstdc++ -lcrypto -lssl -lrt
ldd ox reports that mongodb driver has been static linked
linux-vdso.so.1 => (0x00007ffd1a99d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7f8e4e0000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7f8e15e000)
libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007f7f8dd19000)
libssl.so.1.0.0 => /lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007f7f8dab0000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7f8d8a8000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7f8d689000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f8d2bf000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7f8cfb7000)
/lib64/ld-linux-x86-64.so.2 (0x000055fc81736000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7f8cdb2000)
Now, I can simply copy the ox file to another computer (same os, same version) to use it.
But, My question is that, How to write rule in CMakeList.txt, to tell cmake which libraries I want to static link, which libraries I want to dynamic link ?
If you wanna dynamically link a library, you must have a .so version of that library; likewise, if you wanna statically link a library, you must have a .a version of that library. Of course, this is for Linux like system
Related
I'm trying to compile llvm-ar (and only that) from LLVM statically because I need a static ar implementation for another purpose. AFAICS the official LLVM building documentation doesn't discuss this usage, only the option to build with shared libs, which is disabled by default anyway. Adding -static to CLAGS or the CMake option LLVM CFLAGS doesn't change the build process at all.
This is the build process I'm running right now:
cmake -DLLVM_ENABLE_PROJECTS=llvm-ar -G "Unix Makefiles" ../llvm
make -j3 llvm-ar
which then builds a dynamic llvm-ar:
$ ldd bin/llvm-ar
linux-vdso.so.1 (0x00007fff46154000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007faa84536000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007faa84319000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007faa840ef000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007faa83d66000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007faa839c8000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007faa837b0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007faa833bf000)
/lib64/ld-linux-x86-64.so.2 (0x00007faa87163000)
How can I cause this target to be compiled statically? I'd also like to build with musl-gcc instead of the system default.
Using target_link_libraries in my CMakeLists.txt file results in only some of the shared-object library dependencies being set in the built library.
In my CMakeLists.txt file I have this:
set(STIREN_LIBS)
list(APPEND STIREN_LIBS
/usr/lib/x86_64-linux-gnu/libcairo.so
/usr/lib/x86_64-linux-gnu/libboost_filesystem.so
/usr/lib/x86_64-linux-gnu/libboost_regex.so
/usr/lib/x86_64-linux-gnu/libicuuc.so
/usr/lib/x86_64-linux-gnu/libproj.so.12
/usr/lib/x86_64-linux-gnu/libharfbuzz.so
/usr/lib/x86_64-linux-gnu/libxml2.so
)
If I call target_link_libraries on that, as follows:
target_link_libraries(${PROJECT_NAME} PRIVATE ${STIREN_LIBS})
I get this as the result of objdump -p
Dynamic Section:
NEEDED libcairo.so.2
NEEDED libboost_filesystem.so.1.65.1
NEEDED libboost_regex.so.1.65.1
NEEDED libproj.so.12
NEEDED libxml2.so.2
NEEDED libstdc++.so.6
NEEDED libm.so.6
NEEDED libgcc_s.so.1
NEEDED libc.so.6
NEEDED ld-linux-x86-64.so.2
SONAME libstiren.so
It is missing the dependencies for libicuuc and lifharfbuzz. I confirm that those file paths are valid.
If I omit the target_link_libraries, I get this in the output of objdump -p
Dynamic Section:
NEEDED libstdc++.so.6
NEEDED libm.so.6
NEEDED libgcc_s.so.1
NEEDED libc.so.6
NEEDED ld-linux-x86-64.so.2
SONAME libstiren.so
So why is it that only some dependencies making it through?
I have created an application where it uses libssl which apparently uses libcrypto. I have kept these libraries at a user-defined location /path/to/xyz/lib/
---------------------------------------------------------------------------------------------------------------------------------
I have written a CMake for the application which links the libraries as follows:
set(GCC_LINK_FLAGS "-L/path/to/xyz/lib -lcurl -lpthread -lcrypto -lssl //other libs")
add_executable(Foo ${APP_SOURCE_FILES})
target_link_libraries(Foo ${GCC_LINK_FLAGS})
The CMake didnot give any error and created the Makefile which creates the executable.
---------------------------------------------------------------------------------------------------------------------------------
But When I run the executable. it throws the below error
./Foo: error while loading shared libraries: libcrypto.so.3: cannot open shared object file: No such file or directory
I have checked the dynamic linking of libssl.so as follows and found the libcrypto isn't linked...
[root#localhost build]# ldd /path/to/xyz/lib/libssl.so.3
linux-vdso.so.1 => (0x00007fffa5fa7000)
libcrypto.so.3 => not found
libdl.so.2 => /lib64/libdl.so.2 (0x00007f17ea772000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f17ea556000)
libc.so.6 => /lib64/libc.so.6 (0x00007f17ea193000)
/lib64/ld-linux-x86-64.so.2 (0x0000563b0856f000)
I have checked the /path/to/xyz/lib/pkgconfig/libssl.pc file and found this suspicious for this error
prefix=/path/to/xyz/lib/
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: OpenSSL-libssl
Description: Secure Sockets Layer and cryptography libraries
Version: 3.0.0-dev
Requires.private: libcrypto
Libs: -L${libdir} -lssl
Cflags: -I${includedir}
QUESTION
What does Requires.private: libcrypto do?
And how to solve this error?
--------------------------------------------------------------EDIT--------------------------------------------------------------
I have Done following edits in the cmake file
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "/path/to/xyz/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
add_executable(SpeechToText ${APP_SOURCE_FILES})
target_link_libraries(Foo ${GCC_LINK_FLAGS})
install (TARGETS Foo RUNTIME DESTINATION /path/to/project/directory)
I am getting segmentation fault by this change.
Install the project...
-- Install configuration: ""
-- Installing: /path/to/project/directory/Foo
-- Set runtime path of "/path/to/project/directory/Foo" to "/path/to/xyz/lib"
[root#localhost build]# ../Foo
Segmentation fault (core dumped)
my problem sound a bit strange
I have build a library , named libJsonCpp.so, which I used in some of progam and now I have simply increased the revision number of that library, from 2.12 to the 2.13.
[enzo#P0101222 Test]$ ls -la /prd/b_BCM/bin/libJsonCpp.so*
lrwxrwxrwx 1 enzo aesys 18 Mar 16 16:52 /prd/b_BCM/bin/libJsonCpp.so -> libJsonCpp.so.2.13
lrwxrwxrwx 1 enzo aesys 18 Mar 16 14:26 /prd/b_BCM/bin/libJsonCpp.so.2 -> libJsonCpp.so.2.13
lrwxrwxrwx 1 enzo aesys 22 Mar 16 14:26 /prd/b_BCM/bin/libJsonCpp.so.2.13 -> libJsonCpp.so.2.13.001
-rwxr-xr-x 1 enzo aesys 286939 Mar 16 14:26 /prd/b_BCM/bin/libJsonCpp.so.2.13.001
The strange thing is that the programs that is using this library try to load it twice, the new reivison and the old ones
When I check the program to see what library it required I have an output like this:
[enzo#P0101222 MySampleProgram]$ ldd MySampleProgram
libJsonCpp.so.2.13 => /prd/b_BCM/bin/libJsonCpp.so.2.13 (0x4002a000)
libxerces-c-3.0.so => /usr/local/lib/libxerces-c-3.0.so (0x40061000)
libnsl.so.1 => /lib/libnsl.so.1 (0x40438000)
libpthread.so.0 => /lib/libpthread.so.0 (0x4044c000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x40461000)
......
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
libJsonCpp.so.2.12 => not found
As you can see the program look for the new library ( correct )
libJsonCpp.so.2.13 => /prd/b_BCM/bin/libJsonCpp.so.2.13
but also for the old ones, whic of course was been removed
libJsonCpp.so.2.12 => not found
I have checked in all the system to found where there should still be a reference to the old revision but I found nothing.
I also rebuild the program e delete the cache files, but the problem is still here.
So I'm asking if someone had some suggestion about what to check to find out this strange problem.
Regards, Enzo
added 17.3.2015
During the link of the program I have the following error:
it seems that the link /prd/b_BCM/bin/libJsonCpp.so point ti the old revision, but the links are all linked to the new ones ( see above )
g++ -Wcast-qual -Wshadow -Wcast-align -Wnon-virtual-dtor -Wno-long-long -Wbad-function-cast -Wundef \
-Wall -pedantic -W -Wpointer-arith -Wmissing-prototypes -Wwrite-strings -ansi -D_XOPEN_SOURCE=500 \
-D_BSD_SOURCE -Wcast-align -Wconversion -fno-builtin -g -O2 -O0 -g3 -Wall \
-I.. -Ibcm_plant -Ibcm_panel -Ibcm_display -Ibcm_common -I../bcm_panel -I../bcm_display -I../bcm_common \
-fno-exceptions -fno-check-new -fexceptions -rdynamic \
-o MySampleProgram gestconfigbcmmessage.o clsmsgwritestatus.o testcr01.o util_panel.o updatefirmwaredisplay.o main.o \
/prd/b_BCM/bin/libJsonCpp.so ./bcm_plant/libbcm_plant.a ./bcm_panel/libbcm_panel.a ./bcm_display/libbcm_display.a \
./bcm_common/libbcm_common.a /usr/local/lib/libxerces-c.so -lnsl -lpthread /usr/lib/libstdc++.so \
-L/usr3/BUILD/gcc/gcc-3.4.6/i686-pc-linux-gnu/libstdc++-v3/src \
-L/usr3/BUILD/gcc/gcc-3.4.6/i686-pc-linux-gnu/libstdc++-v3/src/.libs -L/usr3/BUILD/gcc/gcc-3.4.6/gcc \
-L/usr/local/lib -L/prd/b_BCM/src/b_BCM_common/modules -leasyzlib -lftplib -Wl,--rpath -Wl,/usr/local/lib -Wl,--rpath -Wl,/usr/local/lib
/usr/bin/ld: warning: libJsonCpp.so.2.12, needed by /prd/b_BCM/bin/libJsonCpp.so, not found (try using -rpath or -rpath-link)
make[1]: Leaving directory `/home/enzo/Autostrade/prd/b_BCM/src/MySampleProgram/MySampleProgram'
Well, I found the probem.
For some strange reason the new lib contain a reference to the old ones.
$ ldd /prd/b_BCM/bin/libJsonCpp.so.2.13
libpthread.so.0 => /lib/libpthread.so.0 (0x4004d000)
libJsonCpp.so.2.12 => not found
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x80000000)
Luckily was enough to rebuild the new library and the wrong dependency disappeared
Thank Jonathan for you support.
Reagards, Enzo
I'm trying to emulate a firmware with qemu, but I'm getting an error while trying to execute it.
First of all I extracted the firmware filesystem to a folder, and copied de qemu-mipsel file to it:
bin cdrom dev etc home init lib linuxrc mnt opt proc qemu-mipsel root sbin sys tango tmp udev usr var
Then I executed the emulator with chroot:
chroot . ./qemu-mipsel ./bin/ls
And I get the following error:
chroot: failed to run command `./qemu-mipsel': No such file or directory
Googling the error I found that it means that qemu depends on libraries not included in the chroot environment so I searched for them:
linux-vdso.so.1 => (0x00007fffe79ff000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fcd9da57000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fcd9d855000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fcd9d55d000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcd9d2db000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcd9d0be000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcd9cd34000)
/lib64/ld-linux-x86-64.so.2 (0x00007fcda007a000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fcd9caf7000)
And some libraries are missing in ./lib/:
linux-vdso.so.1
libgthread-2.0.so.0
libglib-2.0.so.0
/lib64/ld-linux-x86-64.so.2
libpcre.so.3
How could I solve this problem? Can I create a symbolic link to the system library or should I copy them? Also linux-vdso.so.1 is not present on the system, where could I get it? Regards.
linux-vdso.so.1 is in reality the kernel. The other files, you get them from your GNU/Linux distribution of choice (e.g. Debian, from the source packages eglibc, pcre3 and glib2.0). You must indeed copy them into the chroot. For /lib64/ld-linux-x86-64.so.2 the pathname must be exact, as that path is hard-coded into the binary.
It seems you’re running Debian already, from the multiarch’d paths, which is good, because you’ll need to distinguish between host and target libraries.
Alternatively, you can compile and link qemu-mipsel statically.