gcc - makefile include static library - C -


i have 2 projects.
first has 4 files in it:
1. main.c (which include pnm.h)
2. pnm.c (which include pnm.h)
3. pnm.h
4. makefile

cc=gcc ld=gcc cflags=--std=c99 -wall -w -wmissing-prototypes ldflags= doxygen=doxygen rm=rm  ar=ar ranlib=ranlib  all: pnm  lib: pnm.o     @$(ar) ru libpnm.a pnm.o     @$(ranlib) libpnm.a  pnm: main.o pnm.o     @$(ld) -o $@ $^ $(ldflags)  main.o: main.c pnm.h     @$(cc) -o $@ -c main.c $(cflags)  pnm.o: pnm.c pnm.h     @$(cc) -o $@ -c pnm.c $(cflags)  clean:     @$(rm) -rf *.o  allclean: clean     @$(rm) -rf pnm     @$(rm) -rf libpnm.a 

this project work perfectly.

in second project, have 6 files , libpnm.a:
1. libpnm.a
2. main.c (which include matricules.h)
3. matricules.c (which include matricules.h , codebarre.h)
4. matricules.h
5. codebarre.c (hich include codebarre.h , pnm.h)
6. codebarre.h
7. makefile

cc=gcc ld=gcc cflags=--std=c99 -wall -w -wmissing-prototypes ldflags= doxygen=doxygen rm=rm  all: codebarre  codebarre: main.o matricules.o codebarre.o libpnm.a     @$(ld) -o $@ $^ $(ldflags)  main.o: main.c matricules.h     @$(cc) -o $@ -c main.c $(cflags)  matricules.o: matricules.c matricules.h codebarre.h     @$(cc) -o $@ -c matricules.c $(cflags)  codebarre.o: codebarre.c codebarre.h     @$(cc) -o $@ -c codebarre.c $(cflags)  clean:     @$(rm) -rf *.o  allclean: clean     @$(rm) -rf codebarre 

(each project in separated folder)
when use

make

i error:
codebarre.c:6:17: fatal error: pnm.h: no such file or directory
#include "pnm.h"
^
compilation terminated.

same error when use

make codebarre.o

i think don't use library correctly. don't know how to.

edit: added pnm.h in directory -> work well

your second project not have pnm.h file in current working directory (according list). compiler can't find when compiling codebarre.c.

either need copy pnm.h well, or tell compiler include files in directory pnm.h located using -i option, e.g. with

codebarre.o: codebarre.c codebarre.h libpnm.a     @$(cc) -o $@ -isomedirectory -c codebarre.c $(cflags) 

note wrong name libpnm.a dependency of object file. library should dependency of final executable, e.g.

codebarre: main.o matricules.o codebarre.o libpnm.a     @$(ld) -o $@ $(ldflags) $^ 

Comments