CC++Introduction to C++Learn C++

How To Compile A C Program In Linux

How To Compile A C Program In Linux

Linux is a great operating system, and the C Language is the one of the most powerful programming languages. You can compile your C program with the latest C++ version of most C++ build tools as well as a number of C/C++ IDEs with compilers in all sorts of different operating systems like Windows, iOS, Android as well as Linux. C is very popular among developers in all OS platforms. Using a fast and reliable C or C++ compiler for an operating system is very important for beginners and professionals since it helps C/C++ developers in remembering which language features exist, how to use them, and even detect errors when we get them wrong.

An operating system is software that manages all of the hardware resources associated with your desktop or laptop and Linux is an operating system as same as Windows, iOS, and Mac-OS. Linux might be one of the most popular platforms in the world, because servers and mobiles with Android are powered by the Linux operating system. Linux is also supported by the other operating systems in VMs or in simulations. Linux is free and open source, and this has contributed greatly to its success and popularity.

Why write a C program in Linux?

There are two types of programming languages: interpreted and non-Interpreted (compiled). All computers work with machine code, code that can be directly understood by computer’s process, that tells the computer what to do. This is the lowest level, native and fastest code, but it requires writing many lines for simple things and is hard to generalize for all kinds of machines. A compiler (C or C++ Compiler, etc.) is a computer program that converts one programming language i.e., C/C++ code written with text into executable machine code with a linker.

GNU C/C++ compilers are the one of most popular programming platforms for the Linux operating system, but there is a good selection of different programming languages for Linux too. For example, RAD Studio / Delphi is a very good non-interpreted native programming language for Linux that also has support for the Python language with GUI’s and graphics.

Despite being ‘high level’ – which means it’s easy for humans to understand – the C language is extremely efficient and when compiled it can produce programs which achieve processing speed which is very close to pure hand-written machine code instructions.

How to compile a C program in Linux with the GNU C/C Compiler ?

GNU GCC is a powerful C/C++ compiler originally written as the standard compiler for the Unix, Linux and GNU operating systems. The GNU system was developed to be 100% free software (free from cost but also free from hidden restrictions on licenses and affiliations with businesses), and it is efficient with both 32-bit and 64-bit operations. This compiler can be used on Windows using MinGW or CygWin Linux Simulators. Compiled executable files need a small dll to run independently on Windows. It is mostly compatible with CLANG standards. Please check https://gcc.gnu.org/ for more details about GNU.

Here is a screen shot of a Linux system showing the Sublime Text editor, and the Linux Terminal,

How To Compile A C Program In Linux The Linux desktop with a C program being compiled
Pardus 213 XFCE

Is there an example of how to compile a C program in Linux?

You can use any text editor to create C files. Sublime Text Editor is a light editor for beginners. If you want only edit in console Vim editor is good. Eclipse CDT is a modern C++ editor for Linux. Spyder is a good editor for Python and C++. There are other C++ editors which can be used in different Linux distributions. Some of these are, VS Code for Linux, CodeBlocks for Linux, … Let’s write our C codes as below,

Here is an example of C codes,

Let’s explain this small example. When you start coding in C or C++, first of all, you should include libraries that you use. Generally for beginners stdio.h library is enough to do simple beginner apps. We can include this library as below,

Second, you should add the main procedure (the main program, the main function) as below,

After that you should write your programming code lines into this procedure, between { and } brackets. If you are a beginner “Hello World” examples are good to understand feature of that programming language. It is good to understand how to edit text, how to write in its format, how to compile and link, how to debug and execute, how to deploy or release. This full example below is a “Hello World” C example that can be run with C++ Builder ,

In C++, after every command you use you should put a semicolon – the “;” – to the end of your commands. In this example above we print a text with printf() function and then we can wait to get a character input (key press) with getchar(); function if needed, then we exit and return 0; which means our main app successfully run and exit.

In this example above we define main() function as a integer (int) function, that means we should return a integer value. Here, we print a text with printf() function and then we wait to get a character input (key press) with getchar(); function, sometimes this is necessary to see results when running it. Then we exit and return 0; which means our main app successfully run and exit.

After these steps, in Linux terminal, go to folder of your C file and type gcc and your file name. Be sure that you are supervisor or allowed to save files to that folder. There will be a.out as an executable file.

For example, you can compile your app as below,

How to compile a C program in Linux with the gcc compiler ?

In Linux, GNU C/C++ comes as the default programming language installed. Generally, the gcc compiler is used as a C compiler and g++ compiler is used as a C++ compiler. If your c program file is myapp.c you can compile this with gcc command as below,

Be sure that you have permission to write to folder there. Now you can easily run your myapp.out as given below,

How to compile a C program in the Linux terminal ?

Here is the full input and output process in Linux terminal,

If you are looking for C and C++ Compiler for Windows, iOS or Android,

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?