To compile a one file C++
program, ( say file.cpp )
use the g++
command:
g++ file.cpp
The name of the resulting executable is by default a.out
.
You can run it like this:
./a.out
If you want to call the executable something besides a.out
,
use the -o
option to g++
( note, that's an "o"
as in "orange", it's not a "zero". )
As an example, this is what I'd do if I wanted to compile a file
file.cpp and I wanted the executable to be called output_file
g++ -o output_file file.cpp
To run the output file, again, simply type the name, preceding it with a
./
For example, if I wanted to run an executable by the name of
output_file ( which I created with g++ -o output_file
file.cpp
) I would do this:
./output_file
If you wish to link your code to a certain function library,
you need to use the -l
argument to g++
.
For example, the functions in the math library are in the library
"libm". You compile code that requires this library as follows:
g++ -lm file.cpp
or you could use
g++ -lm -o output_file file.cpp
The -lm argument is necessary because the math functions are in the math library, not the basic C library. So you need to tell the compiler that you are using the math library.