How to Compile C Code With G++
Programming languages like C require that source code be translated into an executable program a CPU can understand. This is accomplished using a compiler. Compilers translate the syntax of the C language into machine code, which can then be executed by the CPU. A popular compiler for the C language is the GNU compiler suite. You can install the GNU compiler suite on Windows and Mac OS X. It usually comes preinstalled on Linux.
Instructions
-
-
1
Open up a command prompt or terminal. This is accomplished differently depending on what system you are using. In Windows, you can access the command prompt by clicking on the "Start Menu," then selecting "All Programs" and "Accessories." The command prompt icon will appear in the "Accessories" menu. In Mac OS X, open the "Utilities" folder and click on the "Terminal" to launch it. In Linux, you can access the terminal by pressing the "Alt" key and "F2" simultaneously and typing "gnome-terminal" in the box that appears. Once you have opened up the command prompt or terminal, proceed to the compilation of your C program.
-
2
Navigate to the folder that contains your C source code. This is accomplished using the "Change Directory" command, which is universal across Windows, Mac OS X, and Linux. For example, if your source code was located in a directory named "Programs" in your "Documents" folder, you could type the following into the terminal:
cd Documents/Programs
-
-
3
Compile the source code using the "g++" command. The "g++" command requires a source code file and a name for the executable program it produces. For example, if your source code file is named "soureCodeFile.C," and you want your program to be named "programName," then you can type the following:
g++ sourceCodeFile.C --o programName
-
4
Press the "Enter" key to compile the program. Your program will produce an output file named "programName" in the same directory as your source code.
-
1