比較幸運,嘗試了幾次就OK了。
g++ -c file_1.cpp
g++ -c file_2.cpp
g++ -c file_3.cpp
g++ -o file_4.out file_1.o file_2.o file_3.o file_4.cpp
參考文獻:http://homepages.gac.edu/~mc38/2001J/documentation/g++.html
Running the compiler
You can useg++
both to compile programs into object modules and to link these object modules together into a single program. It looks at the names of the files you give it to determine what language they are in and what to do with them. Files of the form name.cc
(or name.cpp
) are assumed to be C++ files and files matching name.o
are assumed to be object (i.e., machine-language) files. To translate a C++ source file, file.cc
, into a corresponding object file, file.o
, use the g++
command:
g++ -c compile-options file.ccTo link one or more object files,
file1.o
, file2.o
, ..., to produced from C++ files into a single executable file called prog
, use the following command:
g++ -o prog link-options file1.o file2.o ... other-libraries(The options and libraries clauses are described below.)
You can bunch these two steps---compilation and linking---into one with the following command.
g++ -o prog compile-and-link-options file1.cc file2.cc ... other-librariesAfter linking has produced an executable file called prog, it becomes, in effect, a new Unix command, which you can run with
./prog arguments
where arguments denotes any command-line arguments to the program