如何一次编译多个独立的CPP文件?
How to compile multiple independent CPP files in one go?
我问的不是 makefile。我有多个 .cpp 文件用于测试目的。所以在终端中,我需要写:
g++ test1 -o run1
g++ test2 -o run2
...
如果更改了 .cpp 文件,那么我将不得不再次 运行 上述命令。这种情况有解决办法吗?谢谢!
我认为makefile 无法实现这个目标。所以才这么问。我将保留上述问题不变。下面是我的 makefile,我应该如何为多个文件更改它?
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
如果你坚持不使用Make,你可以将所有命令写入一个纯文本文件并作为shell脚本执行。
EDIT 我的印象是 OP 想要将多个文件编译成一个二进制文件,而不是来自多个文件的多个二进制文件。
就像这样:
g++ file1.cpp file2.cpp -o binary
我知道你问的不是 Makefile
但对于你描述的场景,makefile 可以像这样简单(使用 GNU Make):
all: test1 test2
这会将程序 test1.cpp
和 test2.cpp
转换为可执行文件 test1
和 test2
。
为修改后的问题添加注释
如果您希望能够设置编译器和标志,那么您可以对编译器使用变量 CXX
,对编译器标志使用 CXXFLAGS
:
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
时它是 运行。
最终示例: 其中一个程序需要两个输入源文件,因此它需要一个特殊规则。另一个文件仍然像前面的示例一样自动编译。
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 prog2.cpp
在这种情况下)。
我问的不是 makefile。我有多个 .cpp 文件用于测试目的。所以在终端中,我需要写:
g++ test1 -o run1
g++ test2 -o run2
...
如果更改了 .cpp 文件,那么我将不得不再次 运行 上述命令。这种情况有解决办法吗?谢谢!
我认为makefile 无法实现这个目标。所以才这么问。我将保留上述问题不变。下面是我的 makefile,我应该如何为多个文件更改它?
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
如果你坚持不使用Make,你可以将所有命令写入一个纯文本文件并作为shell脚本执行。
EDIT 我的印象是 OP 想要将多个文件编译成一个二进制文件,而不是来自多个文件的多个二进制文件。
就像这样:
g++ file1.cpp file2.cpp -o binary
我知道你问的不是 Makefile
但对于你描述的场景,makefile 可以像这样简单(使用 GNU Make):
all: test1 test2
这会将程序 test1.cpp
和 test2.cpp
转换为可执行文件 test1
和 test2
。
为修改后的问题添加注释
如果您希望能够设置编译器和标志,那么您可以对编译器使用变量 CXX
,对编译器标志使用 CXXFLAGS
:
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
时它是 运行。
最终示例: 其中一个程序需要两个输入源文件,因此它需要一个特殊规则。另一个文件仍然像前面的示例一样自动编译。
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 prog2.cpp
在这种情况下)。