22

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...

Milan
  • 1,743
  • 2
  • 13
  • 36
Garuda1
  • 331
  • 1
  • 2
  • 3
  • 2
    Why `mv`? This removes the header file from your source directory. `cp` or `install` are usually used here. The target paths are usually something like `$(prefix)/lib` and `$(prefix)/include`; where do you want to install your files? – Karsten Koop Oct 06 '16 at 10:04
  • I want to install them where they need to go, basically if I create `foo.c`, in which I `#include `, I want to be able to compile it with `gcc -lunixlib foo.c` – Garuda1 Oct 06 '16 at 10:09
  • "Where they need to go" is distro/flavor dependent and in your case on the default Include/Lib pathes for gcc. You should go for `/usr/local` as *prefix*, meaning `LIB_PATH=$(prefix)/lib` and `HEADER_PATH=$(prefix)/include` like Karsten Koop wrote with `prefix=/usr/local`. `/usr/` is in general managed by the distros package manager and `/usr/local` commonly used for "local installations" - [you can read about that in the FHS](https://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard). Builders can override it using `make install prefix=/home/example` – makadev Oct 06 '16 at 10:34
  • @Garuda1 The point Karsten is making is you should use *`install`* so that you can not only specify the permission modes but it's also self-documentary (if you want to go for that). At the very least use *`cp`*! - *`mv`* is simply wrong. – Pryftan Apr 26 '18 at 16:42

1 Answers1

35

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

igagis
  • 1,959
  • 1
  • 17
  • 27
  • 1
    And to add: man pages go in *`/usr/local/share/man`* (though there are additional subdirectories there that are for the sections of the man page...should you wish to go that way - the system counterpart would be with the prefix being *`/usr`*). And also similar is *`/usr/src`*. While I'm at it I'll add that for 64-bit libraries it should be *`/usr/lib64`*. – Pryftan Apr 26 '18 at 16:44
  • 1
    The thing I don't like about "install" is it is just a glorified "cp". It always does it, even if unnecessary. That is it still installed, even if the installed file is newer, or even the same file (md5sum). Scripts for example that are running may go 'weird' if overwritten when unnecessary. Is there a smarter way for make files to install just the changes and updates? – anthony Jul 18 '19 at 00:56
  • You can also use `install -D …`, which will automatically create any directories as needed when copying SOURCE to DEST. https://linux.die.net/man/1/install – Denilson Sá Maia Mar 15 '23 at 10:26
  • 1
    @DenilsonSáMaia it depends on which `install` utility you have, `GNU` one or `BSD`. See https://en.wikipedia.org/wiki/Install_(Unix) – igagis Mar 16 '23 at 11:06