Telling omake to use static version of a c library

108 views Asked by At

I'm using omake to build a native binary executable. After it links and i try to run it, it fails to run giving the following error:

error while loading shared libraries: libprotobuf-c.so.1: cannot open shared object file: No such file or directory

Is there a way, at compile time, to tell the executable to pick up the static version: libprotobuf-c.a and not the shared on?

2

There are 2 answers

0
inquisitive On BEST ANSWER

There doesn't seem to be a global flag that can be passed to ld, the linker, that enforces the linker prefer static libraries to dynamic ones when available. In my case, I set the library name explicitly like so:

OCAML_LINK_FLAGS += -cclib -l:libprotobuf-c.a

1
Tim On

I'm not familiar with omake but I believe flag to ocamlc you are looking for is dllpath:

   -dllpath dir
          Adds  the  directory dir to the run-time search path for shared C libraries.  At link-
          time, shared libraries are searched in the standard search path (the one corresponding
          to  the  -I option).  The -dllpath option simply stores dir in the produced executable
          file, where ocamlrun(1) can find it and use it.

If you can configure omake to pass the appropriate -dllpath argument to ocamlc, you should be good to go.

Under the hood I believe this is using the rpath feature (runtime library search path) of ld, the GNU linker. See https://linux.die.net/man/1/ld . There is also a chrpath utility to change the rpath of an already-built executable.

Another option is running your executable with LD_LIBRARY_PATH set so that the shared library is on the load path. You could also install the shared library system wide if appropriate. A final option is to load the library manually when your application boots using dlopen.

The correct choice depends on how you will distribute this and to who you will distribute this, if at all. Keep in mind if you use rpath/dllpath the end user is unlikely to have protobuf installed in the same location you do.