Possible to link my static library with a dynamic library in Xcode? - static

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

Related

Cannot compile C file using own library [duplicate]

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.

How to specify static linking for standard libraries in yocto-cmake?

I am new to cmake so, sorry if this question is very basic.
I want to build my project as statically linked with each standard library it uses.
Like, in gcc if we want to link the standard system libraries(libc, libgcc etc) as static, we specify '-static' option at the time of compilation.(Like gcc main.c -static -o main).
How we can achieve the same in cmake?
I have read multiple threads to define a how to define a library, how to build, link(as static & shared). But that is all for a custom library, I need information for standard system libraries.
[Edit]
In my project, I am using yocto at the top and the cmake is been used underneath it. running directly cmake works fine, as the generated executable has no dependencies on any shared library, all the used libraries are linked statically. But compiling from yocto causing the issue. the executable generated from the yocto build shows the dependencies on several standard shared libraries.
How we can specify static linking of standard libraries in yocto cmake?
Thanks in advance.

using i686-w64-mingw32-g++ for static libraries

I have a JNI project, which I have to make work on Windows (I am working on Linux). This project actually depends on third-party library file which is static (archived i.e .a files). I am trying to create a JNI shared library file using i686-w64-mingw32-g++ and including -static followed by static third-party library name. Following is the command I am using
i686-w64-mingw32-g++ -v -L./ -L/home/user/jre1.8.0_40/lib/amd64/ -I/user/all/apps/Linux2/x86_64/gcc/4.8.2/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.2/include -shared -o test.dll test.cpp -lstdc++ -static -thirdparty
In-spite of placing the third party library in the current working directory, I keep getting error
/user/all/apps/Linux2/src/mxe/2013_12_03/usr/bin/../lib/gcc/i686-w64-mingw32/4.8.1/../../../../i686-w64-mingw32/bin/ld: cannot find -thirdparty
Please note : I included -I/user/all/apps/Linux2/x86_64/gcc/4.8.2/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.2/include to avoid the error cannot find jni.h which I hit before including the path.
I also tried to compile using gcc, in place of g++.
Do I need to create .dll of this third-party library(currently it is archived .a containing .obj files)?
Being a newbie in cross compilers, I might be doing something wrong. Please correct me and any suggestions with this will be very helpful. Thank you.
-Wl,--export-all-symbols -Wl,--add-stdcall-alias -v adding this solved my problem

gcc/clang library compiling, include other library

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

Load shared library that uses another shared library

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.

Resources