nowox
nowox

Reputation: 29136

How to deploy a shared library?

I would like to "quickly" deploy a shared library on my Ubuntu. It is for a short term project so I don't want to use auto-tools here, but do everything manually.

So I built my library with this:

%.o: %.c
    $(CC) -fPIC -c $(CFLAGS) -o $@ $< -MMD -MF $(@:.o=.d)

lib%.so: %.o | dist
    $(CC) -shared -o dist/$@ $^

dist:
    mkdir -p dist

install: lib
    mkdir -p $(PREFIX)/lib/foobar
    mkdir -p $(PREFIX)/include/foobar
    cp dist/*.so $(PREFIX)/lib/foobar
    cp dist/*.h $(PREFIX)/include/foobar
    ldconfig $(PREFIX)/lib/foobar/

In another project, I would like to use libfoo.so now located in /usr/lib/foobar/libfoo.so. So I've built it with:

$(CC) test.c -lfoo 

Unfortunately I have this issue:

/usr/bin/ld: cannot find -lfoo

I now that I can do -L/usr/lib/foobar/libfoo.so but this location should be known by my operating system.

Am I forced to put it directly into /usr/lib? I have the same issue with /usr/local/lib which doesn't seem to be the default route to be used with gcc ... -l...

How should I normally deploy a shared library?

Upvotes: 1

Views: 862

Answers (2)

Milag
Milag

Reputation: 1996

The list of directories from ld.so.conf has system-wide impact; the runtime linker will search those dirs when starting any dynamic binary. Unless you actually want additional system-wide overhead, it's more efficient to privately search another dir on a custom / as-needed basis. Private-search is ideally suited for cases of 1-off or single-use or rarely-used custom libs.

For the 1 or few bins that reference those libs, bins can be rebuilt with a directive for the runtime linker to privately search 1+ custom dirs; eg:

gcc -L/usr/local/lib64 -Wl,-rpath=/usr/local/lib64 -lblah

For more details, see gcc and ld manual pages for the respective options -Wl, and -rpath.

Upvotes: 3

alani
alani

Reputation: 13079

To make a directory known to the dynamic linker (ld.so) so that it can be found at run-time without depending on LD_LIBRARY_PATH:

  • list it in /etc/ld.so.conf (or in an include file under /etc/ld.so.conf.d, if the main /etc/ld.so.conf file has an appropriate include statement to enable this)
  • then run /sbin/ldconfig

As regards the build-time linker (ld), it is normal to expect to have to specify the library location explicitly using the -L flag on the compiler, normally with the directory as argument e.g. -L/usr/lib/foobar. However, according to the manual page for the compile-time linker, ld, the search path for libraries does contain (after everything else) the directories referenced by /etc/ld.so.conf. So although ld.so.conf is primarily intended for use with the run-time linker as the name suggests, the build-time linker will in fact find your library once you have listed the directory there.

Upvotes: 1

Related Questions