2

I have 2 projects.
The 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 my second project, I have 6 files and libpnm.a:
1. libpnm.a
2. main.c (which include matricules.h)
3. matricules.c (which include matricules.h and codebarre.h)
4. matricules.h
5. codebarre.c (hich include codebarre.h and 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 is in a separated folder)
when I use

make

I get an error:
codebarre.c:6:17: fatal error: PNM.h: No such file or directory
#include "PNM.h"
^
compilation terminated.

same error when I use

make codebarre.o

I think I don't use my library correctly. But I don't know how to.

edit: added PNM.h in the directory -> work well

Adrien
  • 25
  • 8
  • Can you show us the complete output of `make` and not just the error message? – fuz Jul 17 '15 at 10:40
  • You know that double quotes make the preprocessor look into the current directory? Maybe you intend to use angle brackets and add a `-Ipath/to/include/directory` to `CFLAGS`. – cadaniluk Jul 17 '15 at 10:43
  • The error message is the only output of make. – Adrien Jul 17 '15 at 11:14

1 Answers1

0

Your second project does not have the PNM.h file in the current working directory (according to your list). Then the compiler can't find it when compiling codebarre.c.

Either you need to copy PNM.h as well, or tell the compiler to look for include files in the directory where PNM.h is located using an -I option, e.g. with

codebarre.o: codebarre.c codebarre.h libpnm.a
    @$(CC) -o $@ -Isomedirectory -c codebarre.c $(CFLAGS)

Note that it is wrong to name libpnm.a as a dependency of the object file. The library should be a dependency of the final executable, e.g.

codebarre: main.o matricules.o codebarre.o libpnm.a
    @$(LD) -o $@ $(LDFLAGS) $^
Jens
  • 69,818
  • 15
  • 125
  • 179
  • I put the PNM.h file in my second project's direcory. It works well. But is it possible to only put libpnm.a ? I tried -I./libpnm but it doesn't work. I looked in libpnm.a and i can only see PNM.o should I add PNM.h in it ? – Adrien Jul 17 '15 at 11:11
  • @Adrien No, libraries only contain object files. You should understand that a header declares an *interface* to a library's functions and must be available for the compiler to find at compile time. A compiler or linker will *never* look for headers in libraries, it will only look in the default places and those specified with `-I`. – Jens Jul 17 '15 at 11:46