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

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. C is still very popular among developers in all OS platforms and it’s hard not to use a computer without using a C program since there are so many out there quietly chugging along playing an integral part in our daily lives.

Using a fast and reliable C or C++ compiler is very important for beginners and professionals learning to write a programs since it helps C/C++ developers in remembering which language features exist, how to use them, and even detect errors as they write their code.

Why compile a C Program on Linux?

There are two types of programming languages: Interpreted and Non-Interpreted (Compiled). All computers work with machine code – code that can be directly programmed by assembler codes – that tells the computer what to do. This is the most 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, whether it is a C or C++ Compiler, 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 tools in the Linux operating system, but there are different programming languages which can also create programs and applications for Linux too. For example RAD Studio / Delphi is a very popular version of Object Pascal and is a very good non-interpreted programming language for Linux that also has support for the Python language with GUI’s and graphics.

Compiled code may not be as fast to execute as pure assembler code, but the difference in speed is very small because both machine code and compiler-based code in text form are much more compatible with other CPU/GPUs and/or with other Operating Systems when you compile them on a machine. Virtually no programmers directly write assembler code today since it takes many times longer to create programs this way, is extremely prone to errors and is very difficult to maintain. Instead developers use high-level languages like C and C++ which are much easier to understand and yet produce programs with very fast execution speeds.

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

GNU GCC is another powerful C/C++ compiler originally written as the compiler for the Unix, Linux and GNU operating system. The GNU system was developed to be 100% free software, 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.

You can use any regular text editor to create C files – although it’s a lot easier to use a dedicated programming environment known as an IDE to create, compile and test your C and C++ programs.

Here is a screen shot of Linux system with a text editor, and the Linux Terminal:

Pardus 213 XFCE

Here is some example C code,

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 ; 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 Terminal, go to folder of your C file and type bcc32 and your file name. Be sure that you are supervisor or allowed to save files to that folder. For example you can compile your app as below,

In Linux, GNU C/C++ comes as default language installed. Generally gcc compiler is used as a C compiler and g++ compiler is used as 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,

Here is the full input and output process in Linux terminal

How to Compile A C Program In Linux
The example C program being compiled and run on Ubuntu

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?

C++C++17C++20Introduction to C++Language FeatureLearn C++Syntax

Learn How To Use Clamp (std::clamp) In Modern C++ 17 and Beyond