Using g++:
Windows: To run g++ you must tell Windows where to find it - see Path section above first.
Mac: The compiler you will use is called clang. But, it sets itself up to "hijack" calls to g++
So you can still just type g++ or you can type clang++.
g++ run by itself will tell you "no input files"
g++ -c filename.cpp will compile your file into an object file filename.o
The –c says “Compile only”, do not link into a final executable program. Try this and then look for the resulting new file.
g++ filename.o will link your object file with any libraries or other code it needs and produce an executable a.exe. Try
this and look for the new exe
Technically we are using the linker program "ld"… but g++ calls it for us with some extra options set
To run your program, type a.exe On Linux/Mac, type ./a
g++ filename.cpp will assume you want to compile and link your object file in one step to directly produce an executable
a.exe
To give your executable file a better name than a.exe, use the -o flag:
g++ filename.o -o executablename
Something like:
g++ main.o -o myProgram.exe
To go directly from cpp file to final program, (compile and link code all at once), do:
g++ main.cpp -o myProgram.exe
This performs all the steps of compiling main.cpp. Then you can run your program by typing "myProgram.exe" On
Mac/Linux don't forget to put ./ before the program name.
For more complete details on compiling from the command line, refer to section 1.3-1.5 of this reference:
http://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html