简体   繁体   English

GNU将所有.cpp文件编译为.o并包含.h文件

[英]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.

相关问题 是否有自动方法来检测我应编译的.cpp源文件(取决于#include * .h) - Is there automated way to detect which .cpp source files I should be compiling (depending on #include *.h) 在cpp文件中使用#include“ Stdafx.h” - Using #include “Stdafx.h” in cpp files 使用g ++ .cpp文件编译gcc .o文件 - Compiling gcc .o files with g++ .cpp files 包含.cpp文件而不是.h文件以使vanilla gcc能够更多地优化我的代码是一个好主意吗? - Is it a good idea to include .cpp files instead of .h files to make vanilla gcc able to optimize my code more? 是否有充分的理由在 h 文件而不是 cpp 文件中使用包含保护? - Is there a good reason to use include guards in h files instead of cpp files? 必须其中包含“ #include” file.h”的所有“ file.o”文件 - Must one include all “file.o” files where there is “#include ”file.h“ ” 在目录中查找所有 .cpp .h 文件的常规方法(包括、src 等...) - Conventional way to find all .cpp .h files in a directory (include, src, etc...) 所有#include-ed的“ .h”头文件都必须与.cpp文件位于同一文件夹中吗? - Do all “.h” header files that are #include-ed have to be in the same folder as your .cpp file? Swig C ++ to python:编译一堆.cpp和.h文件 - Swig C++ to python: compiling a bunch of .cpp and .h files #include 所有 .cpp 文件到一个编译单元? - #include all .cpp files into a single compilation unit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM