如何一次编译多个独立的CPP文件? [英] How to compile multiple independent CPP files in one go?

查看:422
本文介绍了如何一次编译多个独立的CPP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是在问Makefile。我有多个.cpp文件用于测试。因此,在终端中,我需要编写:

I'm not asking about the makefile. I have multiple .cpp files for testing purposes. So in terminal, I need to write:

g++ test1 -o run1
g++ test2 -o run2
...

如果.cpp文件已更改,则必须运行以上命令再次命令。有这种情况的解决方案吗?谢谢!

If the .cpp files are changed, then I'll have to run the above commands again. Is there a solution to this case? Thanks!

我认为makefile无法实现此目标。这就是为什么我这样问。我将完整保留上述问题。以下是我的Makefile,如何更改多个文件?

I thought makefile couldn't achieve this goal. That's why I asked in that way. I'll leave the above question intact. Below is my makefile, how am I supposed to change it for multiple files?

GCC=g++
GFLAGS=-Wall -g -std=c++11 -O3 
SRC=./test1.cpp    
OUT= test1.out    

g:
    $(GCC) $(GFLAGS) $(SRC) -o $(OUT)

clean:
    rm -rf $(OUT) ./*~ ./*.o


推荐答案

我知道您不是在问 Makefile code>,但是对于您所描述的情况,makefile可以如此简单(使用GNU Make):

I know you're not asking about a Makefile but for the scenario you described the makefile can be as simple as this (using GNU Make):

all: test1 test2

这将使程序 test1.cpp test2.cpp 到可执行文件 test1 test2

That will turn programs test1.cpp and test2.cpp into executables test1 and test2.

添加的注释用于修正问题

如果您希望设置然后使用变量 CXX 进行编译,并使用 CXXFLAGS 进行编译: / p>

If you want to be able to set the compiler and flags then you could do that using the variables CXX for the compiler and CXXFLAGS for the compiler flags:

CXX := g++ # set the compiler here
CXXFLAGS := -Wall -Wextra -pedantic-errors -g -std=c++11 -O3 # flags...
LDFLAGS := # add any library linking flags here...

# List the programs in a variable so adding
# new programs is easier
PROGRAMS := test1 test2

all: $(PROGRAMS)

# no need to  write specific rules for
# your simple case where every program
# has a corresponding source code file
# of the same name and one file per program.

clean:
    rm -f *.o $(PROGRAMS)

注意::目标 all:是默认目标,在您键入 make 没有参数。

NOTE: The target all: is the default target and it is run when you type make with no parameters.

最后的例子:其中一个程序需要两个输入源文件,因此需要特殊的规则。另一个文件仍然像前面的示例一样自动编译。

FINAL EXAMPLE: where one program takes two input source files so it needs a special rule. The other file still compiles automatically as in the previous examples.

CXX := g++ # set the compiler here
CXXFLAGS := -Wall -Wextra -pedantic-errors -g -std=c++11 -O3 # flags...

# List the programs in a variable so adding
# new programs is easier
PROGRAMS := test1 test2

all: $(PROGRAMS)

# If your source code filename is different
# from the output program name or if you
# want to have several different source code
# files compiled into one output program file
# then you can add a specific rule for that 
# program

test1: prog1.cpp prog2.cpp # two source files make one program
    $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

clean:
    rm -f *.o $(PROGRAMS)

注意: $ @ 仅表示输出程序文件名( test1 )和 $ ^ 表示列出的所有输入文件( prog1.cpp pr og2.cpp )。

NOTE: $@ simply means the output program file name (test1) and $^ means all the input files listed (prog1.cpp prog2.cpp in this case).

这篇关于如何一次编译多个独立的CPP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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