0

I have made a DLL in Windows using the functionalities from leptonica (an imaging library) moderate them in my way as required with the attachment of other libraries (zlib, pnglib, jpeglib, jpeg2000, libtiff).

Now I want make the same in Ubuntu, here I have build all of the supporting libraries, and I am stucked at making a single dynamic library file from them as per my requirements, I have wrote a imaging.c file by invoking the functionalities and modaring the outputs as per my requirements from leptonica, and now the problem I am facing to make single dynamic library .so file from it, without any dependencies of the other ones, So that I can use it at any other application at Ubuntu, only by loading a single so file... Also I am facing a lot of permission problem when I am trying to make a thread safe log file from that imaging.c file . Here I am trying to dump a log which will generate from imaging.so when I will call it from any wrapped upper level application..

SUMAN PAN
  • 1
  • 1
  • 1

1 Answers1

3

You could create one big .so file under Linux by first compiling static versions of all the libraries that this .so file depends on (depending on the build process of the library, e.g. adding --enable-static --disable-shared as a parameters when running configure is an option).

gcc -shared -o bigfile.so yourobject1.o yourobject2.o -Lsomepath -lsomedependency

will create a .so file. Depending on the architecture, it is important that all code is compiled as position-independent (-fPIC), i.e. compile your object files as

gcc -fPIC -c yourobject1.c

This also applies to static link library dependencies (add -fPIC flags to makefiles, if necessary). If both a dynamic (.so) and a static library (.a) exist in the same directory, the linker will typically prefer the dynamic one. To override this, you can replace -Lsomepath -lsomedependency by somepath/libsomedependency.a.

Use

gcc -shared -o bigfile.so yourobject1.o yourobject2.o -Wl,--whole-archive /fullpath/libsomedependency.a

if some objects in libsomedependency.a are not referenced by libbigfile.so itself, but are referenced by the executable you want to finally link.

See a similar question/answer here: https://stackoverflow.com/a/4922106/10678162

v-joe
  • 366
  • 1
  • 4
  • 10
  • gcc -shared -o bigfile.so yourobject1.o yourobject2.o -Lsomepath -lsomedependency. I have used same procedure, but then also when I am calling the functions from other C program by loading the so file there, then it's showing the error "unreferenced function" of the dependent static libraries. – SUMAN PAN Dec 18 '18 at 02:36
  • You can double-check with `ldd bigfile.so` on the same system where you compiled `bigfile.so` whether any dependencies to dynamic libraries show up you meant to link statically. If you know in which `.a` file the missing function is defined, you can specify the full path + name of that `.a` file directly when linking `bigfile.so` instead of using `-L` and `-l`. You can check with `nm --defined-only libsomefile.a` all the defined symbols of archives and objects. – v-joe Dec 18 '18 at 05:25
  • Is it possible to call the functions of "bigfile.so" from a C file , where I will only link the "bigfile.so", So that when I am running need not to specify the full path + name.... – SUMAN PAN Dec 18 '18 at 11:25
  • I mean I want to do build an so, which will be independent, which will not depends on other libraries when I am running an wrapper program over that so file, I don't want to link any other libraries except that "bigfile.so" – SUMAN PAN Dec 18 '18 at 11:27
  • The full path to the libsomefile.a dependence needs to be specified when linking `bigfile.so`, not at runtime: `gcc -shared -o bigfile.so objectfiles.o /fullpath/libdependency.a`. I'm out of ideas. Maybe if you give details of what you're trying to accomplish, someone else can help you further. – v-joe Dec 18 '18 at 17:23
  • Yes I have specified just like that, but then also if I am not linking them also in the run time, then it's giving error "Unreferenced definition" : but working perfectly if I run it by linking other libraries like : gcc -o test test.o bigfile.so /fullpath/libdependency.so – SUMAN PAN Dec 19 '18 at 03:03
  • If `test` depends directly on objects in `libdependency.a`, you might have to make sure that these objects are linked into `libbigfile.so`, even if nothing else in `libbigfile.so` references them. This can be achieved by using the linker flag `--whole-archive`, i.e. when linking `libbigfile.so`, add `-Wl,--whole-archive` to `gcc -shared -o ...`. Edited answer correspondingly. – v-joe Dec 19 '18 at 04:30