我想要的自动制作文件 [英] My desired automatic make file

查看:73
本文介绍了我想要的自动制作文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是make文件的新手.

I am new in make file.

我有一个程序由

main.cpp
types.hpp
application/coordinator.hpp
application/correlation.hpp
application/handler.hpp
application/settings.hpp
libraries/basket.hpp
libraries/config.hpp
libraries/graphics.hpp
...

我的文件太多了,我的文件列表将被更新很多次.我希望make文件自动识别要生成或更新的.o文件.我不想每次创建并包含一个新文件时都更新我的make文件.输出必须在名为bin

I have so many files and the list of my files will be updated so many times. I want the make file recognizes automatically which .o file to be generated or updated. I don't want to update my make file each time I create and include a new file. The output must be generated in a directory called bin

main.cpp是我唯一的cpp文件,其余文件是hpp.

main.cpp is my only cpp file and the rest of my files are hpp.

到目前为止,此链接启发了我编写以下代码:

Till now, this link has inspired me to write this code:

CC=g++
CFLAGS= -g -Wfatal-errors 

CFLAGS+= -std=c++11
LIBS= -lboost_filesystem -lboost_system

all: run

run: $(OBJS)
    $(CC) $(CFLAGS) $^ main.cpp -o $@

$(OBJS): bin/%.o : bin/%.hpp

如何将其改进为工作代码以及我想要什么?

How to improve it to working code and what I want?

推荐答案

这是另一种方案:在构建软件时生成依赖项信息.这可与多个cpp文件一起创建多个目标文件.

Here's a different scheme: generate the dependency information while you build the software. This works with multiple cpp files creating multiple object files.

CXX := g++
#CPPFLAGS := preprocessor flags, e.g. -I and -D
CXXFLAGS := -g -Wall -pedantic -Wextra -Wfatal-errors -std=c++11 -MD -MP

SOURCES := main.cpp
OBJECTS := $(SOURCES:.cpp=.o)
DEPFILES:= $(OBJECTS:.o=.d)

all: run

# Link the executable
run: $(OBJECTS)
    $(CXX) $(LDFLAGS) $^ -o $@ $(LIBS)

-include $(DEPFILES)

在构建.o文件时,-MD -MP标志告诉编译器生成依赖文件作为副作用.这些依赖文件(如果存在)将包含在makefile中.

When .o files are built, the -MD -MP flags tell the compiler to generate the dependency file as a side-effect. These dependency files are included into the makefile if they are present.

这使用GNU make的内置%.o : %.cpp规则.我们只需为其提供参数(CXXCPPFLAGSCXXFLAGS).

This uses GNU make's built-in %.o : %.cpp rule. We just supply parameters to it (CXX, CPPFLAGS, CXXFLAGS).

Make已经知道要重建.o文件.在makefile中包含.d文件的情况下,我们告诉make对象文件也取决于头文件,并且当其中一个文件更改时应重新构建.但是重建.o的规则始终是%.o : %.cpp

Make already knows to rebuild .o files if the corresponding .cpp file is newer (either GNU make's built-in rule or a hand-written one). With .d files included into the makefile, we tell make that the object file also depends on the header files and should be rebuilt when one of them changes. But the rule to rebuild the .o is always %.o : %.cpp

这篇关于我想要的自动制作文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆