Linux is a great operating system and it’s mostly written in the C Language. C is one of the most powerful programming languages and it can be managed and compiled by a C++ compiler and build tools. Because C and C++ is so good at producing very efficient programs suitable for intensive low-level tasks like software drivers there are C/C++ IDEs with compilers for many different operating systems like Windows, iOS, Android and Linux. 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.
Table of Contents
What does the phrase ‘operating system’ mean?
An operating system is the collection of software that manages all of the hardware resources associated with your desktop or laptop. Linux is an operating system with similar functionality and capabilities 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 actually powered by the Linux operating system which is what underpins Android. Linux is also supported by the other operating systems in virtual machines or in simulations.
The Linux operating system comprises several different key components: Bootloader , Kernel , Init System, Daemons, Graphical server, Desktop Environment, and Applications.
Why compile a C Program in 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, 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 language in Linux operating system, but there are different programming languages for Linux too. For example, RAD Studio / Delphi Version 11.1 is a pascal based and very good non-interpreted programming language for the Linux that has support to the Python language with GUI’s and graphics. Google Go is another compiled programming language for the Linux. There are some other interpreted programming languages like Python Perl, Java.
Such code may not be as fast as 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. This is one reason why C++ is the fastest and most powerful programming language. Interpreted programming languages run inside executable applications like Java, Python, or Visual Basic. This is why they are slower when executing operations, as they need to use compiled libraries for faster operations. Again, they mostly use C/C++ compilers to build these libraries. Using an interpreted programming language 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.
Can I compile a C Program in Linux with GCC ?
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 (both in the sense of free from cost but also free from being tied to a vendor or with hidden costs like using your personal data for advertising purposes), and it is very efficient with low requirements for the hardware which means older, less modern computers can often run Linux even when they struggle with modern versions of Microsoft Windows. This free C and C++ compiler can be used to compile a C program as well as C++ 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 text editor to create C files. The Sublime Text Editor is a light editor for beginners. If you want only to edit in the Linux console Vim editor is good although it can have a steep learning curve for beginners. Alterniatives include Eclipse CDT which is a modern C++ editor for Linux. There is also Spyder which is a good editor for Python and C++.
Here is a screen shot of a Linux system running the Sublime Text editor, and Terminal of Linux,
Is there a simple example of a C program?
Here is an example of a simple “hello world” C program
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.
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,
How do I compile a C program using the GNU C/C++ gcc compiler?
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 |
> ls myapp.c > gcc myapp.c -o myapp.out > ls myapp.c myapp.out > ./myapp.out Hello World |
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.