I have a problem with creating a Makefile for my program. First of all I'll point how my program's directory hast to look like:
- *.o files has to be in main program directory
- *.c files are in src directory
- *.h files are in include directory
- bin folder is for executable file
- lib folder is a place for library files
As I mentioned above I have *.c *.h files already placed in src and include directories. Makefile is located in main program directory. My makefile has to contain vpath or VPATH.
Using gcc I have to build the whole project.
pj, pp, pg, pr are functions .c files.
First of all I have to create object files using gcc -fPIC -c
than from pj.o and pp.o I have to build static library using ar rs
than build shared library from pg.o and pr.o using gcc -shared
. All libjp.a and libhgr.so should be placed in lib folder. After that i have to build the whole program using gcc -o $@ $^ -L./lib -I./include
and place executable file in bin directory.
It's actually not working it's only building the first lib - libjp.a
Here is my makefile:
.PHONY: clean
.SUFFIXES: .c .o .a .so
vpath %.c src
vpath %.h include
%.o: %.c
gcc -w -fPIC -c $<
lib/libjp.a: pj.o pp.o
ar rs $@ $<
lib/libgr.so: pg.o pr.o
gcc -w -shared -o $@ $<
%: %.o
gcc -o $@ $^ -L./lib -I./include
program: program.o lib/libjp.a lib/libgr.so
program.o: program.c libjp.h libgr.h
pj.o: pj.c
pp.o: pp.c
pg.o: pg.c
pr.o: pr.c
clean:
rm -f program *.o *.a *.so
Thank you for your time and help :)
gcc -c program.c gcc -w -fPIC -c pg.c -o pg.o gcc -w -fPIC -c pr.c -o pr.o gcc -shared -o libgr.so pg.o pr.o
2) Static librarygcc -c pj.c gcc -c pp.c ar rs libjp.a pj.o pp.o
3) Compiling:gcc program.o -o program libgr.so libjp.a
It's compiling, and i can run it i mean it's working. – szeejdilib/
? Part of the problem seems to be that the executable cannot find the shared library. There is more than one way to fix this, but I don't know whether you have a solution in place. – Beta