1

sorry for my bad english first of all...

Here's my problem: I have my application composed by main.c (main program), functions.c (code), functions.h (prototypes) and headers.h (typedefs, defines, etc).

In the main's code (and in functions.c, of course) I'm including functions.h, while in functions.h I'm including headers.h.

How should i compile headers.h in a makefile? I don't have headers.c: do I need to create headers.o anyway?

Here's my makefile's code:

application: main.o functions.o
  gcc -o application main.o functions.o

main.o: main.c functions.o
  gcc -c -Wall -pedantic main.c

functions.o: functions.c functions.h
  gcc -c -Wall -pedantic functions.c

clean: 
  rm -f *.o

Everything works fine if i manually compile headers.h with

gcc -c -Wall -pedantic headers.h

EDIT: this is no longer true. I don't know why 5 minutes ago it works.

getting a .gch file. Where am I supposed to write that line in the makefile?

Thank you!

Simone Serra
  • 181
  • 1
  • 14
  • the posted makefile will fail to rebuild the object file if a header file changes. Suggest learning how to have the makefile (with the help fo gcc) create the dependency files and how to incorporate all of that into your makefile. this link shows one method: – user3629249 Jan 10 '16 at 22:26

4 Answers4

4

You don't compile a header. Rather, you can do:

main.o: main.c headers.h
  gcc -c -Wall -pedantic main.c

functions.o: functions.c functions.h headers.h
  gcc -c -Wall -pedantic functions.c
mikedu95
  • 1,725
  • 2
  • 12
  • 24
  • There is no reason for `main.o` to depend on `function.o`. But there should be a rule `program: main.o functions.o` – Basile Starynkevitch Jan 08 '16 at 11:09
  • add `CFLAGS += -Wall -pedantic` to the top and remove the bodies (compiler invocations) from the rules; i.e., `make` has implicit rules to compile objects. – rubicks Jan 10 '16 at 02:13
2

Just add the header right here:

                                         v
functions.o: functions.c functions.h headers.h
  gcc -c -Wall -pedantic functions.c

And add headers in main. You can remove functions.o, unless you want main.o to depend on functions.o:

main.o: main.c functions.h headers.h
  gcc -c -Wall -pedantic main.c

You do not need to create headers.o, you should not compile headers.

2501
  • 25,460
  • 4
  • 47
  • 87
2

You don't have to compile headers. You can do :

SRCS =  ./main.c \
        ./function.c \

OBJS = $(SRCS:.c=.o)

all: application

application:    $(OBJS)
                gcc -Wall -pedantic $(OBJS) -I./ -o output_name

clean:
                rm -f $(OBJS)

and then : make all

rsz
  • 178
  • 2
  • 13
1

In addition to other answers....

[I'm] getting a .gch file. Where am I supposed to write that line in the makefile?

.gch extensions is for GCC pre-compiled headers. This is not a feature for newbies, and it is not a feature useful for small (less than half a million source lines) software projects. See this answer for more about pre-compiled headers, which you probably don't need.

Hence, after fixing your Makefile and committing your source code to your version control system (if you don't use one, use now git before doing anything else!), you could rm -vif *.gch

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547