Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement "make install" in a Makefile?

So here's the repo I'm working with: https://github.com/Garuda1/unixlib

I'd like to know where my compiled lib (unixlib.a) and where my header (unixlib.h) should be so as to be able to use the lib (under Linux-x86 or Linux-x86_64) simply by compiling with:

$ gcc my_source.c -lunixlib

and including the header in my_source.c.

I suppose I add do this to Makefile:

install:
    mv $(NAME).a $(LIB_PATH)
    mv unixlib.h $(HEADER_PATH)

but I don't know what $(LIB_PATH) and $(HEADER_PATH) are...

like image 849
Garuda1 Avatar asked Oct 06 '16 09:10

Garuda1


1 Answers

Normally in UNIX systems the headers go to $PREFIX/include, library files go to $PREFIX/lib, where PREFIX can be /usr or /usr/local or whatever. In linux, if you install your library to these directories it will be found by gcc as you want with just gcc my_source.c -lunixlib.

The PREFIX environment variable is commonly used by various build tools to supply that prefix.

I would also recommend to use DESTDIR variable, it is used by Debian packaging tools at least.

So, the makefile would be like:

# PREFIX is environment variable, but if it is not set, then set default value
ifeq ($(PREFIX),)
    PREFIX := /usr/local
endif

install: unixlib.a
    install -d $(DESTDIR)$(PREFIX)/lib/
    install -m 644 unixlib.a $(DESTDIR)$(PREFIX)/lib/
    install -d $(DESTDIR)$(PREFIX)/include/
    install -m 644 unixlib.h $(DESTDIR)$(PREFIX)/include/

Note, the install -d ... is used just in case if directory does not exist in the supplied DESTDIR for example.

You can also have a look at my non-recursive template for make to find some hints: https://github.com/cppfw/prorab/blob/master/src/prorab.mk

like image 188
igagis Avatar answered Sep 21 '22 15:09

igagis