rock_buddy
rock_buddy

Reputation: 263

Makefile for Multiple .c and .h files

I am having a 1.c 2.c....n.c files and having its dependencies .h file also... i know to create make file for multiple c files.But i don't how to create make file for which the c files are linking to .h files. If i try the makefile which i know it will give error like

make: *** No rule to make target '2.h', needed by '2.o'  .Stop.

and I don't need this type of makefile also.

program: main.o dbAdapter.o
   gcc -o program main.o dbAdapter.o

main.o: main.c dbAdapter.h
   gcc -c main.c

dbAdapter.o dbAdapter.c dbAdapter.h
   gcc -c dbAdapter.c

This will be good for 4 or 5 files. But if I have a large number of files, what is the best solution?

Upvotes: 1

Views: 3708

Answers (3)

Thomas Chafiol
Thomas Chafiol

Reputation: 2613

You can link all your .h in the Makefile by this way :

  1. Put all the .h in a same file (that we called "Include" for the exemple)

  2. Add this in your Makefile : gcc *.c -I/path/Include -iInclude

Ps: Your way to compile your .c file is a bit strange. Usually we use this:

SRC = 1.c
      2.c
      n.c

OBJ = $(SRC:.c=.o)

all:  $(OBJ)
      gcc $(SRC) -I/path/Include -iInclude    (where path is the location of your file called "Include")

Upvotes: 2

Dayal rai
Dayal rai

Reputation: 6606

If your header files are not in current directory and you included it in Makefile, Make starts looking for header files in default location but it is not able to find them in your case.

you should put 2.h header files in current directory to avoid this search.

Upvotes: 0

Belkacem REBBOUH
Belkacem REBBOUH

Reputation: 559

As long as I'm working with C, I never wrote make files that includes header files (.h) the header files are here to expose some of the data structure and methods, constants that are needed in other C modules. You don't need to create rules for header files, all you have to do is build the .o objects then the liker will do the magic for you when you create the executable file. If you need some help crating a make file you can explain here what you wanna build and I'll send you a hint. Cheers.

Upvotes: 0

Related Questions