From Tux
Jump to: navigation, search

The gcc compiler is installed on UNLV CS servers for your use.

Compiling C++ programs

Basic compilation

To compile a program you have written: g++ source.cpp. This will produce an executable file a.out, which you can run with ./a.out

If there is a syntax error in your program, the compiler will report information about the syntax error such as the type of error, which line and column the error occurred on, and, sometimes, a proposed solution to it.

You are not required to use any particular extension for source or executable files, although it is convention to give a C++ source file the .cpp extension.

Alternate executable filenames

If you wish to compile to a different executable file than a.out, you can do so with the -o option as such: g++ -o executablefile source.cpp or g++ source.cpp -o executablefile and then run ./executablefile

Enforcing a standard

As various standards of C++ have added or removed certain features and keywords, you may want to compile with a certain standard in mind. To compile with the C++11 standard run g++ like this: g++ -std=c++11 source.cpp

Other commonly used standards include c++98, c++03, c++11, c++14, c++17.

Strict compliance

gcc does its best to compile code even when it is not strictly compliant. To enforce strict compliance a variety of flags can be turned on that enable extra warnings and turn all warnings into errors. Most extra warnings and errors that these flags generate can easily be fixed. Typical extra warnings and errors generated would include the declaration of a variable that is never used or using a variable for a calculation when it is uninitialized. To enable some of these flags when compiling, try g++ -Wall -Wextra -Wpedantic -Werror source.cpp


gcc has some tricks up its sleeve and by increasing the time it takes to compile as well as the size of the executable you can often produce executables that run faster. You can use g++ -O3 source.cpp to enable a high level of optimization. Other optimization flags exist such as -O1, -O2, -Ofast, -Os

Compiling a class and linking

If you need to compile a class into object code, first compile the class as such: g++ -c classfile.cpp which will produce classfile.o. Next, compile your main program and link in the class as such: g++ source.cpp classfile.out

Combining flags

All of the above flags can be combined.

For additional information, consult man gcc, man g++, or the documentation available at