The C Language is the one of the most powerful programming languages that can be run by C++ Code Editors and compilers on Windows, Linux, Android, Mac-OS. Linux is a great operating systems and C/C++ are its main programming language of this operating system. C language is still very popular among developers in all OS platforms, and it has many pre-defined variables, functions, and libraries available today. 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 also effectively powered by the Linux operating system which is the foundation underpinning Android. Linux is also supported by the other operating systems in VMs or in simulations.
The Linux operating system comprises several different pieces: Bootloader, Kernel, Init System, Daemons, Graphical server, Desktop Environment, and Applications.
Different operating systems require different compilers. For example, on Windows 7,8,10, 11 versions you can use the latest C++ Builder IDE and its compilers, also you can use its other OS specific compilers for iOS and Android operating systems.
Table of Contents
Why compile a C Program on Linux?
There are two types of programming languages: Interpreted and Non-Interpreted (Compiled). All computers (CPU/GPU) work with machine code (code that can be directly programmed by assembler codes) that tells the computer what to do (exe files are this kind of file). 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 – 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 language in Linux operating system, but there are different programming languages for Linux too. For example, RAD Studio / Delphi Version 11.1 is Object Pascal based and very good supremely powerful easy to learn programming language which can produce native apps for Linux and almost every other major operating system for desktop and mobile devices.
Picking a fully compiled language like Delphi, C or C++ instead of using an interpreted programming language can provide significant speed and efficiency increases. Using an interpreted language like Python or Java is like being carried by a runner, while a compiled (non-interpreted) programming language is like running itself. This subtle difference turns into a huge gap when you run the same routine – for example in face recognition – millions of times a millisecond.
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 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. Here is a screen shot of a Linux system with GCC in the Linux Terminal:
Is there a simple “hello world” example C program?
Here is an example of C code,
1 2 3 4 5 6 7 8 |
#include <stdio.h> int main() { printf("Hello World !\n"); return 0; } |
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,
1 |
#include <stdio.h> |
Second, you should add the main procedure (the main program, the main function) as below,
1 2 3 4 |
int main() { } |
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.
1 2 3 4 5 6 7 8 |
#include <stdio.h> int main() { printf("Hello World"); return 0; } |
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.
How to compile my C program using GCC in the Linux terminal?
After these steps, in 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. For example, you can compile your app as shown 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,
1 2 3 4 5 |
gcc myapp.c -o myapp.out |
Be sure that you have permission to write to folder there. Now you can easily run your myapp.out as given below,
1 2 3 4 5 |
./myapp.out |
Here is the full input and output process in Linux terminal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
> ls myapp.c > gcc myapp.c -o myapp.out > ls myapp.c myapp.out > ./myapp.out Hello World > |
Using a full C++ IDE to create apps
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.