How to Compile C++ Code ======================= Single Source File ------------------ Assume the file name is "Program.cc", and the shell prompt is ">". >g++ -Wall Program.cc -o Program Option "-Wall" turns on all compiler warnings. Option "-o" specifies the name of the outputted executable. Depending on the compiler, you may have to explicity request that a certain language standard be used: >g++ -Wall -std=c++26 Program.cc -o Program Multiple Source Files --------------------- Assume the source files are "Source1.cc" and "Source2.cc", and the shell prompt is ">". You may have written your own header files as well. Since header files are not compiled on their own, their presence is irrelevant. Again, DO NOT compile a header file! # Compile both source files (note the "-c" option) >g++ -Wall -c Source1.cc >g++ -Wall -c Source2.cc Option "-c" tells g++ to compile but not link. Linking would produce errors as the source file does not contain the code for the entire program. # Link the object files to produce the executable >g++ Source1.o Source2.o -o Program # If you're using code NOT from the standard library you # must link to it as well using the "-l" option # Below I link against the realtime library because I'm # using clock_gettime, a function in the realtime library. >g++ Source1.o Source2.o -o Program -lrt If you change a source file, you must recompile that file and relink the object files. Running an Executable --------------------- >./Program -------------------------------- Guide by Gary M. Zoppetti, Ph.D.