Compile C on CentOS to static library - c

I'm able to compile and run my application in C on CentOS with the following parameters:
gcc test.c -o test -lpcap -lssl -lcrypto -L /usr/local/lib -L /usr/local/opt/openssl/lib/ -lhiredis
But I need to run the application on a machine where I cannot compile, nor download ex. hiredis.
So I need to compile everything together on my own CentOS - which does match in setup.
I've read about the static flag, but when doing so I get the following errors:
gcc -static test.c -o test -lpcap -lssl -lcrypto -L /usr/local/lib -L /usr/local/opt/openssl/lib/ -lhiredis
/usr/bin/ld: cannot find -lpcap
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lcrypto
/usr/local/lib/libhiredis.a(net.o): In function `_redisContextConnectTcp':
/home/alfredballe/hiredis/net.c:399: warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
collect2: error: ld returned 1 exit status
What am I doing wrong?

Related

usr/bin/ld: cannot find -lcurl: No such file or directory

I am trying to create a dynamic library, but I keep getting this error when I insert the command bellow on terminal:
gcc -o libvetoraleatorio.so -shared vetoraleatorio.o -lcurl
Then I get
/usr/bin/ld: cannot find -lcurl: No such file or directory
collect2: error: ld returned 1 exit status
That's the instructions that I am following that were provided by my teacher in college:
**Creating a dynamic library
**
Generate the source code from the dynamic library
-fPIC option (to generate position-independent code, i.e. code that works correctly no matter what memory location it is inserted into)
Use the -shared GCC option to generate the .so file
EXAMPLE:
$ gcc -c -fPIC -o aula18-apuracao.o aula18-apuracao.c -lcurl
$ gcc -o libaula18-apuracao.so -shared aula18-apuracao.o -lcurl
$ gcc -o aula18-apuracao aula18-main.c -L${PWD} -laula18-apuracao -lcurl
Note: -lcurl is because the example uses libcurl. The -L is to inform where the newly generated .so is. In this case, it is stating that it is in the current directory
Note: the library will not be included in the executable; it will be dynamically linked to the program at runtime
Note: the ldd command shows the dynamic libraries that will be loaded into an executable

How to build a Shared Library but use static glibc on Linux?

I'm trying to create a shared library on centos using gcc 4.8.2
shared library code:
//reload.c
int func(int num){
return num++;
}
link command:
gcc -fPIC -shared reload.c -o reload.so
use ldd command:
linux-vdso.so.1 => (0x00007ffe6aa93000)
libc.so.6 => /usr/lib64/libc.so.6 (0x00007f27feb97000)
/lib64/ld-linux-x86-64.so.2 (0x00007f27ff169000)
Now, Want to statically link glibc, how to write it?
like it:
ldd xxx.so
not a dynamic executable
I tried the build options, but the error.
gcc -fPIC -shared reload.c -o reload.so -Wl,-Bstatic -lc
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
thank you very much
You do not have dependencies to glibc at all for your code above, so the easiest way is to compile with the flag -nostdlib:
$ gcc -fPIC -shared reload.c -o reload.so -nostdlib
$ ldd reload.so
statically linked

Unable to make shared library while embedding Python to C

I am trying to make a shared library which consists of one C header file and two C source files, one of which calls a Python file for implementation.
The compilation comand used is
gcc -fPIC -c -I-I/usr/local/include -I/usr/local/include -I/usr/local/include/python3.4m -I/usr/local/include/python3.4m -DNDEBUG -g -fwrapv -O0 -Wall -Wstrict-prototypes -DDOUBLE_PRECISION *.c
I am able to compile all these without any error, but when I try to make a shared object file using the following command
gcc *.o -L/usr/local/lib -lpthread -ldl -lutil -lm -Xlinker -export-dynamic /usr/local/lib/python3.4/config-3.4m/libpython3.4m.a -shared -o libroughness.so
I get the following error which I am unable to resolve
/usr/bin/ld: /usr/local/lib/python3.4/config-3.4m/libpython3.4m.a(abstract.o): relocation R_X86_64_32S against `_PyObject_NextNotImplemented' can not be used when making a shared object; recompile with -fPIC
/usr/local/lib/python3.4/config-3.4m/libpython3.4m.a: could not read symbols: Bad value
collect2: ld returned 1 exit status
I am using -fPIC tag while compilation, however it still asks me to recompile with fPIC. Any help on how to resolve this case would be appreciated.

/usr/bin/ld: cannot find -lcrypto while trying to link

gcc -o clxd -m64 -L/usr/local/lib64 -L/usr/lib64 -L/usr/lib64/nptl
-Wl,-rpath,/home/y/lib64 -ldl -lrt -lpthread -lstdc++ -lgcc -lc -lm -lev -L/home/y/lib64 -Wl,-Bstatic -lboost_program_options -lboost_date_time -lboost_filesystem -lboost_regex -lboost_system -lboost_thread -llua -lcrypto -Wl,-Bdynamic -L/usr/lib
The above command is from a make file while executing make using V=1 (verbose) to understand if I have passed on directories properly or not. And it exits with the following error:
/usr/bin/ld: cannot find -lcrypto collect2: ld returned 1 exit status
My understanding from the gcc command, -Bstatic tries to statically link lib crypto (-lcrypto).
When I checked under /usr/lib64, I have the following files:
libcrypto.so -> libcrypto.so.1.0.1e
libcrypto.so.10 -> libcrypto.so.1.0.1e
libcrypto.so.1.0.1e
Does -Bstatic link the shared files statically or not? If not how do I get libcrypto.a?
I solved this on Linux/Macosx by installing libssl-dev.
sudo apt-get install libssl-dev

Linking using g++ fails searching for -lstdc++

I'm trying to use someone else's Makefile to complile a very simple c++ library. The makefile is as follows:
JNIFLAGS=-O2 -pthread -I/usr/lib/jvm/java-6-sun/include -I/usr/lib/jvm/java-6-sun/include/linux
all:
rm -f ../dist/libUtils.so
g++ $(JNIFLAGS) -c -m32 -o com_markets_utils_dates_NativeTime.o com_markets_utils_dates_NativeTime.cpp
g++ $(JNIFLAGS) -c -m32 -o DateUtil.o DateUtil.cpp
g++ -pthread -m32 -shared -fPIC -o ../dist/libUtils.so DateUtil.cpp
g++ -pthread -m32 -shared -fPIC -o ../dist/libNativeTime.so DateUtil.o com_markets_utils_dates_NativeTime.o
This compiles fine, but the linker complains:
...
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.1/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.1/libstdc++.a when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.1/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.1/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
collect2: ld returned 1 exit status
make: *** [all] Error 1
FYI, I am on Ubuntu 9.10 64bit.
Posting for future reference, a solution I found was to install g++-multilib. I had the same incompatible problem relating to -lstdc++ on g++ version 4.6.1
On further probing: g++-multilib is a dummy package which installed g++4.6-multilib which in turn installed the appropriate libstdc++.so under the /usr/lib/gcc/x86_64-linux-gnu/4.6/32 folder.
Answering my own question:
Ths solution seems to be a bit of a hack, you need to create a symlink for the 32 bit version of the library (after installing the packages mentioned in #nos's answer):
$ sudo ln -s /usr/lib32/libstdc++.so.6 /usr/lib32/libstdc++.so
Once you've done this, the linker will automagically find the correct library to use.
It seems you're compiling a 32 bit library on a 64 bit machine, however a 32 bit version of libstdc++ is not present.
Try apt-get install ia32-libs libc6-i386 libc6-dev-i386 lib32gcc1 lib32stdc++6
(btw. you're producing a .so , you should specify -fPIC when compiling your .cpp files as well)

Resources