compile static binary while using <sqlite3>? - c++

Is it possible to compile a program that uses sqlite3.h (-lsqlite3) into a static binary?
here is what happens when I try:
g++ -m64 -std=c++17 -static Database.cpp -o test.app `pkg-config --static --cflags --libs sqlite3`
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/libsqlite3.a(sqlite3.o): in function `unixDlOpen':
(.text+0x8d49): warning: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking`
Linux x86_64

Related

how to link specific version of shared library in g++

I have the following shared libraries:
libssl.so, libssl.so.1.1, libcurl.so, libcurl.so.4, libcurl.so.4.4.0, libcrypto.so, libcrypto.so.1.1.
All of the libraries are in the openssl folder.
My question is, how can I link version 1.1 of libssl? Is it done automatically?
I've tried the following:
g++ -c my_file_name.cpp -std=c++11 -w -fpermissive -lpthread --coverage $(INCLUDES) `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` -O0 -Lopenssl -lcrypto -lcurl -lssl
g++ my_file_name.o -o ex -std=c++11 -w -fpermissive -lpthread --coverage -lgtest -lgtest_main -lpthread `pkg-config --cflags glib-2.0` `pkg-config --libs glib-2.0` -O0 -Lopenssl -lcrypto -lcurl -lssl
But it seems that the link doesn't happen. As I still get errors like:
error: ‘X509_STORE_CTX_get0_chain’ was not declared in this scope
Later edit
nm libssl.so.1.1 | grep X509_STORE_CTX_get0_chain results in 0000000000218210 T X509_STORE_CTX_get0_chain. That would mean that the link that I've done does not happen.
It's worth mentioning that the error comes from a .c file included in the .cpp file.
Probably the libssl.so is a symbolic link to libssl.so.1.1 etc. etc.
The problem seems to be related to the missing implementation in one of these library of this function :X509_STORE_CTX_get0_chain.
Now you have to check if this symbol is defined in one of these library you link, or if you need to link someone else.
To check this you can execute the following command over each library:
nm libToCheck.so | grep X509_STORE_CTX_get0_chain
if no one have this symbol defined maybe you missing some library to link.
If exist, check the scope of the utilizzation of the function, it could not correspond to the scope declared in the library. Check the namespace or similar.

error in compiling a c++ code in order to use it in a django project

Recently I start a Django project in order to some image processing purposes. for some performance reasons, I wrote a function in c++ and test it independently. Now I want to call that c++ function in my python code. but when I compile it with this instruction:
g++ a.cpp `pkg-config --cflags --libs opencv` -shared -o program.so
this error occurred:
/usr/bin/ld: /tmp/ccvIlzBz.o: relocation R_X86_64_PC32 against symbol `_ZNK2cv7MatSizeclEv' can not be used when making a shared object; recompile with -fPIC
according to this error, if ّ replace -shared with -fPIC flag, it compiles but at my python code, this error occurred:
cannot open shared object file:
which means that it is not compiled correctly.
finally I build the c++ program with this instruction:
g++ a.cpp `pkg-config --cflags --libs opencv` -shared -fPIC -lm -o program.so
but when I execute my python code and call a function(sample_func) in that c++ code, it's give this error :
../program.so: undefined symbol: sample_func
how I can fix this problem?

compiling c++ projects with dlib library on linux

I want to use dlib library for my c++ projects in linux. I have installed it successfully and able to compile and run the .cpp samples files given under the dlib. I have compiled the sample files through the
"g++ -std=c++11 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp" given in [http://dlib.net/compile.html][1]
but I am unable to use the same command to run .cpp files which are the outside dlib-18.18/examples directory
Can someone help me out with compiling c++ file (with dlib library usage) from any user directory in linux?
I came to know that we have to include the path of dlib installation folder while trying to compile it from any other user directory. So I tried the command
" g++ -O3 -I/home/praneeth/computervision/.. ../dlib/all/source.cpp -lpthread -lX11 project3_face.cpp -o project_face pkg-config --cflags opencv pkg-config --libs opencv"
but it gave me the result:
g++: error: ../dlib/all/source.cpp: No such file or directory
Note: dlib-18.18 folder is present in my /home/praneeth/computervision/
I don't know how correct is this but it got compiled when I ran the command like: g++ -O3 -I/home/praneeth/computervision/dlib-18.18 /home/praneeth/computervision/dlib-18.18/dlib/all/source.cpp -lpthread -lX11 project3_face.cpp -o project_face pkg-config --cflags opencv pkg-config --libs opencv any comments on this regarding why this works?
g++ -Wl,-V -std=c++11 -o3 -I/home/user/dlib-19.6 /home/user/dlib-19.6/dlib/all/source.cpp -lpthread -lX11 -o Test Test.cpp pkg-config opencv --cflags --libs

Static compilation fails: dynamic STT_GNU_IFUNC symbol `tan' .. can not be used when making an executable

I'm trying to compile expect statically on my PC, but I seem to run into some strange compiling errors.
I was able to compile tcl statically like this:
./configure --disable-load --disable-shared
Then add to the makefile CFLAGS variable -static keyword. Now after compiling I seem to have a static tcl, and a library:
-rw-rw-r-- 1 jlumme jlumme 3284332 loka 2 11:27 libtcl8.6.a
After this, I configure expect:
./configure --with-tclconfig=${TCL_DIR}/unix --disable-shared --disable-load
And then compile (first dynamic):
$ gcc -pipe -O2 -fomit-frame-pointer -Wall -Wl,--export-dynamic -o expect exp_main_exp.o -L/home/jlumme/Downloads/compile/expect5.45 -lexpect5.45 -L/home/jlumme/Downloads/compile/tcl8.6.4/unix -ltcl8.6 -lieee -lm -lpthread -lz
The result looks ok:
$ file expect
expect: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=a613c260944117f43bcaebc43c51c421dce273a7, not stripped
But now if I want to attempt a static compilation, I get errors that I dont understand:
gcc -pipe -O2 -fomit-frame-pointer -Wall -Wl,--export-dynamic -o expect exp_main_exp.o -L/home/jlumme/Downloads/compile/expect5.45 -lexpect5.45 -L/home/jlumme/Downloads/compile/tcl8.6.4/unix -ltcl8.6 -lieee -lm -lpthread -lz -static
/home/jlumme/Downloads/compile/tcl8.6.4/unix/libtcl8.6.a(tclUnixCompat.o): In function `TclpGetGrGid':
tclUnixCompat.c:(.text+0x41c): warning: Using 'getgrgid_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/home/jlumme/Downloads/compile/tcl8.6.4/unix/libtcl8.6.a(tclUnixCompat.o): In function `TclpGetGrNam':
tclUnixCompat.c:(.text+0x31c): warning: Using 'getgrnam_r' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
<HANDFUL OF WARNINGS REMOVED>
/usr/bin/ld: dynamic STT_GNU_IFUNC symbol `tan' with pointer equality in `/usr/lib/gcc/x86_64-linux-gnu/4.9/../../../x86_64-linux-gnu/libm.a(s_tan.o)' can not be used when making an executable; recompile with -fPIE and relink with -pie
collect2: error: ld returned 1 exit status
I dont really understand what this final linking problem is, can someone help ?

Compiling with g++ and having GraphicsMagick++ as static library

The following command works
g++ file.cpp $( GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs )
but i want a static executable that is not linked to the library, so i have tried
g++ file.cpp $( GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs ) -static
but it only generates the following errors
/usr/bin/ld.bfd.real: /usr/lib/gcc/x86_64-linux-gnu/4.6/crtbeginT.o: relocation R_X86_64_32 against `__DTOR_END__' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-linux-gnu/4.6/crtbeginT.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
adding -fPIC gives the same result.
I'm using the GraphicsMagick++ version that comes with the standard repository in Ubuntu 12.04 64 bit.
Since GraphicsMagick++ is a fork of ImageMagick some old trick for ImageMagick will probably work but i have never used ImageMagick, only GraphicsMagick++.
To link statically against that particular library, use -Wl,-Bstatic before it, i.e.:
g++ $( GraphicsMagick++-config --cppflags --cxxflags ) -fPIC file.cpp -o file -Wl,-Bstatic $( GraphicsMagick++-config --ldflags --libs )
If you then wanted to pass more libraries, and link dynamically against them, you can pass -Wl,-Bdynamic to switch back and add more libraries afterwards.
Edit: Also note the specific argument order. First compiler flags, then file, then linker flags and libraries at the end. With other order, you can run into random failures. Really.