How To Compile And Run C/C++ Programs in Linux Using GCC
This post will guide you how to compile C or C++ programs on CentOS/ RHEL/ Ubuntu Linux systems using gcc compiler. If you wrote a C or C++ program with Vi/VIM text editor in Linux command line interface, and you want to know how to compile this C or C++ program.
In Linux system, if you want to compile a C or C++ program in Bash Termial application, you need to install GNU C and C++ Compiler collection or others developement tools or libraires. just do the following steps:
#1 install C or C++ Compiler and Development Tools
For CentOS/RHEL/Fedora Linux, you need to use the yum install command to install GNU C and C++ Compiler collection, type:
# yum groupinstall "Development Tools
For Ubuntu Linux 16.04/18.04, you need to use the apt-get command to install GNU C/C++ Compiler collection, type:
$ sudo apt-get update $ sudo apt-get install build-essential
outputs:
devops@devops-osetc:~$ sudo apt install build-essential sudo: unable to resolve host devops-osetc [sudo] password for devops: Reading package lists... Done Building dependency tree Reading state information... Done build-essential is already the newest version (12.1ubuntu2). 0 upgraded, 0 newly installed, 0 to remove and 599 not upgraded.
#2 Check Gcc Installation
If you want to check if the GCC compiler is installed successfully, you can try to check the versio and the location of the gcc compiler on CentOS or Ubuntu Linux, type the following command:
$ whereis g++ $ whereis gcc $ which gcc $which g++ $ gcc --version $ g++ version
Outputs:
devops@devops-osetc:~$ gcc --version gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. devops@devops-osetc:~$ g++ --version g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#3 Compile and Run a C or C++ program on Linux
You can use the vi/vim text editor to create a file called fio.c, and then append the following lines:
#include<stdio.h> int main() { printf("Hello World!\n") return 0; }
Then executing the following command to compile and run this c profram, type:
$ gcc fio.c -o fio $ ./fio
Or
$ g++ fio.c -o fio $ ./fio
If you want to compile multiple source files (such as: fio1.c, fio2.c) into executable, type:
$ gcc fio1.c fio2.c -o executable
If you want to allow warnings, debug symbols in the output while compiling c source files, type:
$ gcc fio.c -Wall -Og -o executable
if you want to compile the source code into assembler instructions, type:
$ gcc -S fio.c
if you want to compile the source code without linking, type:
$ gcc -c fio.c
if you want to compile the source code that contains math function, type:
$ gcc fio.c -o executable -lm
If you want to compile a C++ program that contains Xlib graphics functions, type:
$ g++ fio.C -o executable -lX11
if you want to get more details for gcc compiler, you can refer the man pages, type:
# man gcc