[英]GNU make compiling all .cpp files to .o and include .h files
So i have folder that looks like this 所以我有一个看起来像这样的文件夹
OS
Makefile
/include
head.h
/src
main.cpp
head.cpp
/objects
How can i use Makefile to compile all .cpp files to objects folder then compile all .o files with include .h to my actual cpp program.I currently have: 我如何使用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
What do I enter next to make all those .cpp files to .o and compile them with .h included.Thank you for your time 接下来我要输入什么以将所有.cpp文件制作为.o并使用.h进行编译。谢谢您的时间
You can use pattern rules (previously known as "suffix rules") for that. 您可以为此使用模式规则 (以前称为“后缀规则”)。 As you use GNU make, you can write this line to compile all
.cpp
files in src/
to .o
files in objects/
, assuming the Makefile
is placed in the top directory: 使用GNU make时,假设
Makefile
位于顶层目录中,则可以编写以下代码来将src/
所有.cpp
文件编译为objects/
.o
文件:
objects/%.o: src/%.cpp
$(CXX) -c $(CFLAGS) $< -o $@
In GNU make syntax $<
denotes the dependency (a .cpp file in this case), $@
denotes the target (the object file). 在GNU make语法中,
$<
表示依赖关系(在这种情况下为.cpp文件), $@
表示目标(目标文件)。
The .h
files do not need to be compiled, just set up the correct include path as part of the string the CFLAGS variable contains: .h
文件不需要编译,只需将正确的包含路径设置为CFLAGS变量包含的字符串的一部分即可:
CFLAGS += -I./include
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.