C: 如何用 makefile 编译包含在另一个头文件中的头文件?
C: How to compile an header included in another header with makefile?
首先抱歉我的英语不好...
这是我的问题:
我的应用程序由 main.c(主程序)、functions.c(代码)、functions.h(原型)和 headers.h(类型定义、定义等)组成。
在主要代码中(当然在 functions.c 中)我包含了 functions.h,而在 functions.h 中我包含了 headers.h。
我应该如何在 makefile 中编译 headers.h?我没有 headers.c:我还需要创建 headers.o 吗?
这是我的 makefile 代码:
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
如果我用
手动编译headers.h一切正常
gcc -c -Wall -pedantic headers.h
编辑:这不再是真的。我不知道为什么 5 分钟前它有效。
正在获取 .gch 文件。我应该把那行写在 makefile 的什么地方?
谢谢!
你不编译header。相反,您可以这样做:
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
只需在此处添加 header:
v
functions.o: functions.c functions.h headers.h
gcc -c -Wall -pedantic functions.c
并在 main 中添加 headers。您可以删除 functions.o,除非您希望 main.o 依赖于 functions.o:
main.o: main.c functions.h headers.h
gcc -c -Wall -pedantic main.c
您不需要创建 headers.o,您不应该编译 header。
您不必编译 headers。你可以这样做:
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)
然后:make all
除了其他答案....
[I'm] getting a .gch file. Where am I supposed to write that line in the makefile?
.gch
扩展适用于 GCC pre-compiled headers。这不是适合新手的功能,也不是对小型(少于 50 万行源代码)软件项目有用的功能。有关 pre-compiled headers 的更多信息,请参阅 this answer,您可能不需要。
因此,在修复您的 Makefile
并将您的源代码提交到您的版本控制系统之后(如果您不使用版本控制系统,请使用 now git 在做任何事情之前!),你可以 rm -vif *.gch
首先抱歉我的英语不好...
这是我的问题: 我的应用程序由 main.c(主程序)、functions.c(代码)、functions.h(原型)和 headers.h(类型定义、定义等)组成。
在主要代码中(当然在 functions.c 中)我包含了 functions.h,而在 functions.h 中我包含了 headers.h。
我应该如何在 makefile 中编译 headers.h?我没有 headers.c:我还需要创建 headers.o 吗?
这是我的 makefile 代码:
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
如果我用
手动编译headers.h一切正常gcc -c -Wall -pedantic headers.h
编辑:这不再是真的。我不知道为什么 5 分钟前它有效。
正在获取 .gch 文件。我应该把那行写在 makefile 的什么地方?
谢谢!
你不编译header。相反,您可以这样做:
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
只需在此处添加 header:
v
functions.o: functions.c functions.h headers.h
gcc -c -Wall -pedantic functions.c
并在 main 中添加 headers。您可以删除 functions.o,除非您希望 main.o 依赖于 functions.o:
main.o: main.c functions.h headers.h
gcc -c -Wall -pedantic main.c
您不需要创建 headers.o,您不应该编译 header。
您不必编译 headers。你可以这样做:
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)
然后:make all
除了其他答案....
[I'm] getting a .gch file. Where am I supposed to write that line in the makefile?
.gch
扩展适用于 GCC pre-compiled headers。这不是适合新手的功能,也不是对小型(少于 50 万行源代码)软件项目有用的功能。有关 pre-compiled headers 的更多信息,请参阅 this answer,您可能不需要。
因此,在修复您的 Makefile
并将您的源代码提交到您的版本控制系统之后(如果您不使用版本控制系统,请使用 now git 在做任何事情之前!),你可以 rm -vif *.gch