How to Compile C Programs in Linux

How to Compile C Programs in Linux
Article Word Count: 1400 Practical Index: ⭐⭐⭐⭐⭐
Common IDEs for compiling on Windows, while using gcc directly for compilation in Linux, is the foundation of Linux embedded programming and also a frequently asked question in embedded interviews.
How to Compile C Programs in LinuxCommand Line Compilation and Detailed Compilation Process
Example code for hello.c:
#include <stdio.h>

int main(void)
{ 
 printf("Hello world\n");
 return 0;
}
Compilation:
gcc hello.c -o hello
How to Compile C Programs in Linux
If necessary, you should also be able to answer the detailed steps:
gcc -E hello.c -o hello.i  # Preprocessing stage
gcc -S hello.i -o hello.s  # Compilation stage
gcc -c hello.s -o hello.o  # Assembly stage
gcc hello.o -o hello       # Linking stage
How to Compile C Programs in Linux
Using Make to Compile
For a small number of files, you can use the above method to compile. When there are many source files, you can use the make tool. Make compiles by parsing the Makefile to execute some gcc commands.
First, create a Makefile, such as:
hello:hello.c
        gcc hello.c -o hello
Compile and run:
How to Compile C Programs in Linux
The Makefile for the more detailed compilation process above is as follows:
How to Compile C Programs in Linux
How to Compile C Programs in Linux

Using CMake to Generate Makefile

In actual development, it is rare to write a Makefile by hand as shown above; instead, you can use the cmake tool to generate it.
CMake is a cross-platform installation (compilation) tool that can describe the installation (compilation process) for all platforms with simple statements.

1. Command Line Operations

First, enter<span>cmake --version</span> to check the CMake version. If it is not installed, execute the following command to install:
sudo apt install cmake
How to Compile C Programs in Linux
Next, we start the experimental demonstration. Our<span>cmake_test</span> folder contains a<span>hello.c</span> file. Create a <span>CMakeLists.txt</span> file in the same folder:
How to Compile C Programs in Linux
Enter the following content:
cmake_minimum_required (VERSION 3.10.2)
project (cmake_test)
add_executable(cmake_test hello.c)
Then, in the<span>cmake_test</span> directory, sequentially enter the following commands to generate the Makefile:
mkdir build  # Create build folder
cd build     # Enter build folder
cmake ../    # Generate Makefile in cmake_test folder
The execution result is as follows:
How to Compile C Programs in Linux
Friends interested in the specific syntax of the<span>CMakeLists.txt</span> file can refer to materials for further study.

2. Using cmake-gui

The above method is for generating Makefile using cmake from the command line. We can also use the graphical interface to generate the Makefile. cmake-gui is a graphical tool for cmake. Below, we will demonstrate with an example.
We place the previous section’s<span>CMakeLists.txt</span> and <span>hello.c</span> files into a newly created folder called cmake-gui_test:
How to Compile C Programs in Linux
In the terminal, enter <span>cmake-gui</span> to start the cmake-gui graphical tool. If it is not installed, you can enter the following command to install:
sudo apt install cmake-qt-gui
For example:
How to Compile C Programs in Linux
Launch <span>cmake-gui</span>:
How to Compile C Programs in Linux
How to Compile C Programs in Linux
How to Compile C Programs in Linux
Check the cmake-gui_test folder:
How to Compile C Programs in Linux
Compile and run:
How to Compile C Programs in Linux
The above shares some compilation processes and methods, which are basic content that needs to be mastered. If you find this article helpful, feel free to share it.
How to Compile C Programs in Linux

1

6000 Words of Practical Content | Embedded Driver Learners Must Not Miss This Article!

2

《Will Traditional Embedded C Programmers Disappear Under the New Programming Development Model?》

3

《No Way! Still Confused About the Differences Between Microcontroller Development and Linux Development?》

How to Compile C Programs in Linux
Every day, hardworking Xiaochuang,
[Share, Like, Read] Can I Have a Duck?How to Compile C Programs in Linux

Leave a Comment