简体   繁体   English

多个.c和.h文件的Makefile

[英]Makefile for Multiple .c and .h files

I am having a 1.c 2.c....nc 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. 我有一个1.c 2.c .... nc文件,还有它的依赖项.h文件...我知道要为多个c文件创建make文件。但是我不知道如何为此创建make文件c文件链接到.h文件。 If i try the makefile which i know it will give error like 如果我尝试我知道的makefile将给出错误

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

and I don't need this type of makefile also. 而且我也不需要这种类型的makefile。

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. 这对于4或5个文件将是很好的。 But if I have a large number of files, what is the best solution? 但是,如果我有大量文件,什么是最佳解决方案?

You can link all your .h in the Makefile by this way : 您可以通过以下方式将所有.h链接到Makefile中:

  1. Put all the .h in a same file (that we called "Include" for the exemple) 将所有.h放在同一个文件中(例如,我们称其为“ Include”)

  2. Add this in your Makefile : gcc *.c -I/path/Include -iInclude 将此添加到您的Makefile中:gcc * .c -I / path / Include -iInclude

Ps: Your way to compile your .c file is a bit strange. 附言:您编译.c文件的方式有点奇怪。 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")

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. 只要使用C,我就从来没有编写过包含头文件(.h)的make文件,头文件在这里是为了公开一些数据结构和方法,以及其他C模块所需的常量。 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. 您不需要为头文件创建规则,只需要做的就是构建.o对象,那么当您创建可执行文件时,喜欢者将为您做魔术。 If you need some help crating a make file you can explain here what you wanna build and I'll send you a hint. 如果您需要有关制作Make文件的帮助,可以在此处解释您要构建的内容,我会给您提示。 Cheers. 干杯。

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. 如果您的头文件不在当前目录中,而您将其包含在Makefile中,Make会开始在默认位置查找头文件,但无法在您的情况下找到它们。

you should put 2.h header files in current directory to avoid this search. 您应该将2.h头文件放在当前目录中以避免此搜索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM