I am experimenting with how to distribute a simple graphics library for Linux using a Makefile. The shared library is called libsgtk.so. Here is my first attempt for the Makefile:

CFLAGS= -pthread \
       -I/usr/include/gtk-3.0 \
       -I/usr/include/pango-1.0 \
       -I/usr/include/atk-1.0 \
       -I/usr/include/cairo \
       -I/usr/include/gdk-pixbuf-2.0 \
       -I/usr/include/glib-2.0 \

all: libsgtk.so

libsgtk.so: window.o
    gcc -shared -o libsgtk.so window.o

window.o: window.c
    gcc $(CFLAGS) -c -fpic window.c

     rm window.o

    mv libsgtk.so /usr/local/lib

As you can see, I have hard coded the installation path to /usr/local/lib. Is this a recommended approach? What should I do if a library with the same name already exists in that path? Should this be checked in the Makefile, or is there some other tools that I should use that could provide me with an alternative installation path?


I think you should use variables. For example:

  mv libsgtk.so ${DESTDIR}

In this case the user can overwrite the default value with make DESTDIR=/usr/lib (note the ? above).

If you want you can test it. Maybe it's not a bad idea. The simplier method is to use mv -n (or mv -nv) instead of mv: doesn't overwrite the existing file. Its main drawback the return value of mv is zero so the make will not know that the moving wasn't success.

But if you would use cp (copy) instead of mv it will be good: the cp -n is return 1 (nonzero) when the destination file exists. So the solution (test included):

  cp -n libsgtk.so ${DESTDIR}

Only a helping question: do you know about PHONY targets in Makefile? You should use it for install and clean targets (remark: you should delete libsgtk.so too in clean).

  • 2
    It would be more in line with how other installation software works to set PREFIX?=/usr/local and then install the library in "$PREFIX/lib. Also, the install utility may be useful here instead of cp. – Kusalananda Mar 24 '18 at 19:23
  • Yes, of course. But would better with LIBDIR=${PREFIX}/lib :) – uzsolt Mar 25 '18 at 8:06
  • Or, just write the configure.ac and Makefile.am files from the start... :-) – Kusalananda Mar 25 '18 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.