[英]Is there automated way to detect which .cpp source files I should be compiling (depending on #include *.h)
[英]GNU make compiling all .cpp files to .o and include .h files
所以我有一个看起来像这样的文件夹
OS
Makefile
/include
head.h
/src
main.cpp
head.cpp
/objects
我如何使用Makefile将所有.cpp文件编译到objects文件夹,然后将包含.h的所有.o文件编译到我的实际cpp程序中。我目前有:
CPP_FILES := $(wildcard src/*.cpp)
H_FILES := $(wildcard include/*.h)
OBJ :=$(wildcard objects/*.o)
LIBS := -Wall
CC := -std=c++14
接下来我要输入什么以将所有.cpp文件制作为.o并使用.h进行编译。谢谢您的时间
您可以为此使用模式规则 (以前称为“后缀规则”)。 使用GNU make时,假设Makefile
位于顶层目录中,则可以编写以下代码来将src/
所有.cpp
文件编译为objects/
.o
文件:
objects/%.o: src/%.cpp
$(CXX) -c $(CFLAGS) $< -o $@
在GNU make语法中, $<
表示依赖关系(在这种情况下为.cpp文件), $@
表示目标(目标文件)。
.h
文件不需要编译,只需将正确的包含路径设置为CFLAGS变量包含的字符串的一部分即可:
CFLAGS += -I./include
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.