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
...
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
That will turn programs test1.cpp
and test2.cpp
into executables test1
and test2
.
ADDED NOTES for amended question
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)
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)
NOTE: $@
simply means the output program file name (test1
) and $^
means all the input files listed (prog1.cpp prog2.cpp
in this case).
If you insist in not using Make, you could write all the commands into a plain text file and execute it as a shell script.
EDIT I was under the impresstion that the OP wanted to compile multiple files into one binary, not multiple binaries from multiple files.
Just do something like this:
g++ file1.cpp file2.cpp -o binary