Search code examples
linuxmakefileshared-librariesgnuautotools

How do you force automake to copy shared objects to /usr/local/lib?


I currently have a c++ project which depend on some external shared objects (.so). My current directory looks like this:

├── src
│   └── .cpp files
├── include
│   ├── glad
│   │   └── .h files 
│   └── fmod
│       ├── core
│       │    └── .h files 
│       └── studio
│           └── .h files 
├── lib
│   └── fmod
│       ├── core
│       │   └── .so files 
│       └── studio
│           └── .so files 
├── Makefile.am
├── configure.ac

I want to compile this project while simultaneously copying those .so files towards /usr/lib or /usr/local/lib, however I can't seem to be able to do this!

The following is my configure.ac file

AC_INIT([autoGL], 1.0)
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_PROG_CXX
AC_CONFIG_FILES(Makefile)
AC_OUTPUT

And my Makefile.am

bin_PROGRAMS = autogl
autogl_SOURCES = src/Source.cpp
autogl_SOURCES+= src/glad.c
autogl_LDADD = -lglfw -ldl
autogl_LDADD+= -L lib/fmod/core -lfmod
autogl_LDADD+= -L lib/fmod/studio -lfmodstudio
autogl_LDFLAGS = -Wl,--no-as-needed,-rpath,lib/fmod/core,-rpath,lib/fmod/studio
autogl_CPPFLAGS = -I include
autogl_CPPFLAGS+= -I include/fmod/core
autogl_CPPFLAGS+= -I include/fmod/studio
autogl_CPPFLAGS+= -I include/fmod/fsbank

You can see that I'm linking every library using the link flags -L lib/fmod/---- -library. Initially, the seventh line of my Makefile.am was only

autogl_LDFLAGS = -Wl,--no-as-needed

resulting in the following g++ code, which was successfull and gave me an executable file

g++  -g -O2 -Wl,--no-as-needed   -o autogl autogl-Source.o autogl-glad.o -lglfw -ldl -L lib/fmod/core -lfmod -L lib/fmod/studio/ -lfmodstudio

However, when tried to run this, I would get the following error:

./autogl: error while loading shared libraries: libfmod.so.12: cannot open shared object file: No such file or directory

My shared objects are not being copied to /usr/lib or /usr/local/lib.

With the addition of

autogl_LDFLAGS = -Wl,--no-as-needed,-rpath,lib/fmod/core,-rpath,lib/fmod/studio

since we are linking rpath to our lib files, the program has no problem running. However, if I run make install, the rpath being linked would be /usr/bin/lib/fmod/core and /usr/bin/lib/fmod/studio, which clearly don't have the needed files. My .so files are still not being copied anywhere. I want to copy my .so files directly to /usr/local/lib so that my program can run without me having to link it directly.

How can I force automake to copy these .so files directly to a folder of my choice? (preferable /usr/local/lib).


Solution

  • Found a solution!

    Autotools also offers the possibility to transfer data. I added the following to my Makefile.am

    flashdir=$(prefix)/lib
    flash_DATA= lib/fmod/core/libfmodL.so \
              lib/fmod/core/libfmodL.so.12 \
              lib/fmod/core/libfmodL.so.12.10 
              .....
    

    This adds all of my .so files to $(prefix)/lib, which usually is /usr/local/lib.

    However, there is a problem, particularly in Ubuntu, where /usr/local/lib is not by default on /etc/ld.so.conf.d, so libraries on /usr/local/lib are not used.

    To solve this I added the following line to my makefile.am

    install-data-hook:
        ldconfig $(prefix)/lib
    

    This creates a hook which runs AFTER the lib files are already added to $(prefix)/lib, which, when run, adds the folder to /etc/ld.so.conf.d, so now after make install, everything runs smoothly.