简体   繁体   English

为C ++程序创建健壮的Makefile

[英]Creating a robust Makefile for C++ program

Consider the following trivial Makefile. 考虑以下琐碎的Makefile。 Notice that there are two classes with corresponding .h and .cpp files for each. 请注意,有两个类,每个类都有对应的.h和.cpp文件。

output: main.o class1.o class2.o
    g++ main.o class1.o class2.o -o output

main.o: main.cpp
    g++ -c main.cpp

class1.o: class1.cpp class1.h
    g++ -c class1.cpp

class2.o: class2.cpp class2.h
    g++ -c class2.cpp

clean:
    rm *.o output 

Is this Makefile robust? 这个Makefile是否健壮? How can I make it more robust? 如何使其更坚固? Can someone provide a more robust version of this Makefile? 有人可以提供此Makefile的更强大版本吗? It will be running on a 64 bit Red Hat Linux or CentOS machine. 它将在64位Red Hat Linux或CentOS计算机上运行。

If it matters, I am using: 如果有关系,我正在使用:

-gcc --version 4.8.4 (From the GNU Compiler Collection) -gcc --version 4.8.4(来自GNU编译器集合)

-GDB version: GNU gdb (Ubuntu 7.7.1 -0ubuntu5~14.04.2) 7.7.1 -GDB版本:GNU gdb(Ubuntu 7.7.1 -0ubuntu5〜14.04.2)7.7.1

I would write something like 我会写类似

CXX = g++
# mandatory build flags
AM_CXXFLAGS = -Wall -W -std=gnu++11
# optional build flags
CXXFLAGS = -O2 -Werror

# mandatory link flags
AM_LDFLAGS = -Wl,-as-neeeded

output: main.o class1.o class2.o
    ${CXX} ${AM_LDFLAGS} ${LDFLAGS} $(filter %.o,$^) -o $@

%: %.cpp
    ${CXX} ${AM_CXXFLAGS} ${CXXFLAGS} $< -c -o $@

class1.o: class1.cpp class1.h
class2.o: class2.cpp class2.h
  • the *FLAGS are following automake notation: AM_* mean mandatory flags, the normal flags contain local settings (eg debugging or optimiziation). *FLAGS使用以下自动制作符号: AM_*表示强制性标志,常规标志包含本地设置(例如调试或优化)。

    Usually, AM_CPPFLAGS and CPPFLAGS with preprocessor flags ( -I ... ) should be used too but I omitted them here 通常, AM_CPPFLAGS应使用带有预处理器标志( -I ... )的AM_CPPFLAGSCPPFLAGS ,但在此将其省略

  • the output target might need ${LIBS} too which have been omitted here output目标可能也需要${LIBS} ,此处已省略

  • dependency tracking is more tricky and not implemented; 依赖跟踪更加棘手, 没有实现; you will have to play with -dM compiler option here... 您将不得不在此处使用-dM编译器选项...

When your program consists only of the listed 5 files, you can write 当程序仅包含列出的5个文件时,您可以编写

output_SOURCES = main.c class1.c class2.c class1.h class2.h

output: ${output_SOURCES}
        ${CXX} ${AM_CXXFLAGS} ${CXXFLAGS} ${AM_LDFLAGS} ${LDFLAGS} $(filter %.cpp,$^) -o $@ ${LIBS}

directly. 直。

EDIT: 编辑:

For automatic dependency tracking, you can write 对于自动依赖性跟踪,您可以编写

DEPGENFLAGS = \
    -MD -MF ${@D}/.${@F}.d -MT '$@'

%: %.cpp
    ${CXX} ${DEPGENFLAGS} ${AM_CXXFLAGS} ${CXXFLAGS} $< -c -o $@

-include .deps.main.o.d
-include .deps.class1.o.d
-include .deps.class2.o.d

I don't think your original makefile is far off the mark. 我认为您的原始makefile不会超出预期范围。 I've just made a few additions: 我刚刚添加了一些内容:

  1. Added a default all target - it's good to be explicit 添加了默认的all目标-明确表示好
  2. Use the CXXFLAGS environment variable in all the calls to g++ 在对g ++的所有调用中使用CXXFLAGS环境变量
  3. Make main.o depend upon class1.h and class2.h as discussed 使main.o依赖于class1.hclass2.hclass2.h
  4. Specify that clean is a PHONY target just to be on the safe side if you add build artifact called clean 如果添加名为clean构建工件,则指定cleanPHONY目标,只是PHONY安全考虑

ALL := output

all : $(ALL) 

output: main.o class1.o class2.o
    g++ $(CXXFLAGS) main.o class1.o class2.o -o output

main.o: main.cpp class1.h class2.h
    g++ $(CXXFLAGS) -c main.cpp

class1.o: class1.cpp class1.h
    g++ $(CXXFLAGS) -c class1.cpp

class2.o: class2.cpp class2.h
    g++ $(CXXFLAGS) -c class2.cpp

.PHONY : clean

clean:
    rm *.o output 

How can I make it more robust? 如何使其更坚固?

Use implicit rules, keep it simple. 使用隐式规则,保持简单。

LDLIBS+=-lstdc++
output: main.o class1.o class2.o

clean:
    rm *.o output 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM