If you would create a library with c, that depends on another library, say cURL, you would compile it to a static library like this:
clang myLibrary.c -lcurl -c
ar myLibrary.a myLibrary.o
But, if you were to include this library in your application, you would get an error because some functions were not found (the cURL ones).
Is there a way to include libraries (in this case cURL) in the library at compile time?
I'm working on a linux machine
Related
I have a shared library (*.so) created using Real View Compiler Tools (RVCT 3.2) on windows target. Then I try to link this *.so file with my application using gcc on linux system.
What is the gcc option to link this shared library with my application linux?
My question is, is the -shared option, which is used as
gcc -shared myfile.so
..., used to create the SO file or to link the SO file? I believe it creates something like:
gcc -lmyfile.so
Is this enough? Or is there any other switch to tell the linker that it's a dynamic library (shared object)?
What worked for me was:
gcc -L. -l:myfile.so
gcc -lmyfile should be enough (provided that your library is named libmyfile.so). The linker searches for shared objects when possible and AFAIK prefers them.
I'm Linux rookie and I'm trying to move my library from windows to Linux. It is native binary (mylib.so), but it will be loaded by mono ( [DllImport()] ). I'm using a pcre (Perl Compatible Regular Expressions) in this library. When my .NET executable tries to load mylib.so it throws exception (lib not found). When I set MONO_LOG_LEVEL=debug. I says that my library is found, but pcre library is not.
I've tried to to load it dynamically (dlopen(), dlsym()). When I build executable version of my library linking it with dl (-ldl) it works fine. But when I load it from mono I got SIGSEGV.
I create this library like (for version with dl):
g++ -fPIC -c *.cpp
g++ -shared -Wl,-soname,libmylib.so.1.1 -ldl -o libmylib.so.1.1 *.o
I've create simple test program that link against mylib.so and dl (-l:libmylib.so.1.1 -ldl) and it works.
I think I need to force mylib to link with dl (or directly with pcre), but I don't know how. (I hope it's possible)
All what I want is to create library that use pcre and work under mono.
You incorrectly linked your library: if it uses libpcre you must link it, too, with the -lpcre option.
I have some code which I want to run on a machine which I do not have root access to.
That machine does not have some of the libraries needed to run this code.
Is there any way to include all dependencies when I compile? I realize the resultant file may be quite large.
What you're looking for is static compiling. Performing static compilation includes all of the libraries into the executable itself, so you don't have to worry as much about dependency chains on a specific system, distribution, etc.
You can do this with:
gcc -Wl,-Bstatic -llib1 -llib2 file.c
The -Wl passes the flags following to the linker, -Bstatic tells it to link static if possible, and then lib1, lib2, are the libs you intend to link.
Alternatively, try:
gcc -static file.c
The compilation will still need to match the architecture of the non-privileged system. And you need to have the static libraries installed on the compiling system (lib.a)
If compiled properly, it should show "not a dynamic executable" when you run:
ldd a.out
I am using Libcurl in my application with C and GNU compiler on a linux machine.
Application also uses OpenSSL libraries and some other libraries. I am trying to statically link the libraries, except for lcurl linking with other libraries works fine.
I am trying to compile as below.
gcc -static -I. -o test test.c -lz -lssl -lcrypto -lcurl
Am I doing anything wrong?
Is there a way to force static linking of some librairies (libc for exemple) ?
Libcurl is itself linked against a ton of other libraries, most of which aren't included in your compile command line. For instance, mine (on Debian Squeeze) links against:
libc
libcom_err
libcrypto
libdl
libgcrypt
libgnutls
libgpg-error
libgssapi_krb5
libidn
libk5crypto
libkeyutils
libkrb5
libkrb5support
liblber-2.4
libldap_r-2.4
libpthread
libresolv
librt
libsasl2
libssh2
libssl
libtasn1
libz
(You can get a similar list for yourself by running ldd on the library on Linux, or otool -L on Darwin.)
I am currently creating a static library that contains functionality I want to provide to some other applications. However, my static library requires libxml2.dylib and compiling my static library fails at the moment because it's having trouble accessing the dylib. Is there a way to link my static lib with a dynamic lib?
Yes, this is possible. However, the resulting static library will have a run-time dependency to libxml2. Notice that you will have to specify the location of the dynamic library at linktime using the -L switch. Assuming you have installed libxml2 using MacPorts the path is /opt/local/lib.
g++ -staticlib -o libyourlib.a file1.o file2.o -L/opt/local/lib -lxml2