Reputation: 507
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:
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 :)
Upvotes: 0
Views: 380
Reputation: 100856
You cannot use VPATH to locate derived (generated) files. VPATH can only be used with source files. So, these two lines, and all the associated infrastructure that goes with them, will not work as you expect:
vpath %.so lib
vpath %.a lib
If you want to understand this more fully you can read http://make.mad-scientist.net/papers/how-not-to-use-vpath/
Second, rules like this:
%.a: pj.o pp.o
ar rs $@ $^
mv *.a ./lib
violate the second rule of makefiles. If you want a target to be created in a subdirectory, then the name of the target must be the full name, in the subdirectory. And there's no point in using pattern rules if you're going to list the prerequisites explicitly in the pattern. You can use something like:
lib/libjp.a: pj.o pp.o
ar rs $@ $^
lib/libgr.so: pg.o pr.o
gcc -w -shared -o $@ $^
which means you have to change references to it:
program: program.o lib/libjp.a lib/libgr.so
and lines like this should be removed:
libjp.a: pj.o pp.o
libgr.so: pg.o pr.o
There may be other similar changes needed.
Upvotes: 2