Reputation: 11
I'm trying to figure out a simplified way for installing C and C++ code and libraries built from them, primarily for Linux. I think GNU Autotools, CMake, etc. would be overkill for what I'm trying to do.
An example: I've written some code which, when 'make' is run, builds to create a library 'example.a'. (I'm going with static linking thus far. Shared libraries will introduce still more issues; I'll crawl before I walk.) Use of the functions requires including 'example1.h', 'example2.h', etc.
With Autotools, one would run 'make install' or 'sudo make install' to copy example.a to /usr/local/lib and the include files to /usr/local/include. If executables had been built, those would get copied to /usr/local/bin.
I've tried adding the following lines to my makefile:
install:
cp example.a /usr/local/lib
cp example1.h /usr/local/include
cp example2.h /usr/local/include
cp example /usr/local/bin
This seems to work, mostly. Other projects are then able to "see" the .h files and build correctly. But the .a file is not found; the only way to get it to be linked is to explicitly give the path as /usr/local/lib/example.a. (Though gcc insists it's looking for libraries in that path.) So:
Question 1: Should my user-built libraries be put in /usr/local/lib? Or am I using the wrong directory?
Now, my next problem: this has to be done with 'sudo make install'. I'm thinking that for some projects of this ilk, where only one user will be making use of the code in question, the libraries should go someplace such as $HOME/lib, include files to $HOME/include, executables to $HOME/bin.
Question 2: Is there a "standard" way to install includes/libraries for just the current user, rather than doing so for all users? (Thus avoiding the need for root use, and I'd think it would be a "cleaner" system if only the user using the program had to deal with these files.)
Upvotes: 0
Views: 349
Reputation: 60145
You should probably use install
to copy with explicit permissions. example.a
probably isn't being found because it doesn't start with lib (a stupid requirement, I know).
Putting your lib
, include
, and bin
under $HOME/
.local is probably the closest thing to a per-user standard setup.
Edit:
I just tested it and it /usr/local/lib
works fine for me:
foo.c
#include <stdio.h>
void foo() { puts("foo"); }
Compile and install:
gcc -c foo.c
ar crs libfoo.a foo.o
install -m 0755 /usr/local/lib #0644 should do too
In a different directory:
main.c
void foo(void);
int main() { foo(); return 0; }
Compile, link, and run:
gcc main.c -lfoo
./a.out
Upvotes: 1