Under Windows, IDE is commonly used to compile, while under Linux, GCC is directly used to compile. The compilation process is the foundation of Linux embedded programming, as well as embedded high-frequency basic interview questions.
One, command line compilation and each subdivision compilation process
Hello.c example code:
// Public id: Linux mainland
#include <stdio.h>
int main(void)
{
printf("Hello world\n");
return 0;
}
Copy the code
Compile:
gcc hello.c -o hello
Copy the code
If necessary, answer the steps of the segmentation:
GCC -e hello.c -o hello. I # GCC -s hello. I -o hello. S # GCC hello. S -o hello.o # GCC hello.o # link phaseCopy the code
2. Compile with make
You can use the above method to compile with a small number of files. If there are many source files, you can borrow the make tool. Make compiles by parsing the Makefile to execute some GCC commands. To start, create a Makefile, such as:
hello:hello.c
gcc hello.c -o hello
Copy the code
Compile and run:
The Makefile above for more detailed compilation is as follows:
Create a Makefile with CMake
Real development rarely involves writing your own makefiles as above, which can be generated with the help of the cmake tool.
CMake is a cross-platform installation (compilation) tool that describes installation (compilation) on all platforms in a simple statement.
1. Command line operation
Run the cmake –version command to check the cmake version. If the cmake version has not been installed, run the following command to install it:
sudo apt install cmake
Copy the code
Let’s start the demonstration. We have a hello.c file in our cmake_test folder, create a cmakelists.txt file in the same folder:
Enter the following information:
cmake_minimum_required (VERSION 3.10.2)
project (cmake_test)
add_executable(cmake_test hello.c)
Copy the code
Then type the following commands in the cmake_test directory to generate the Makefile:
CD build # go to build folder cmake.. / # generate the Makefile under cmake_testCopy the code
The result is as follows:
Cmakelists. TXT file specific syntax interested friends can consult information for learning.
2. Use cmake-GUI
This is how you compile a Makefile from the command line using cmake. We can also use a graphical interface to generate makefiles. Cmake-gui is a graphical tool for Cmake. Again, here’s an example.
We put the cmakelists. TXT and hello.c files from the previous section into the newly created folder cmake-gui_test:
Enter the cmake-gui command on the terminal to start the Cmake-gu graphics tool. If it is not installed, run the following command to install it:
sudo apt install cmake-qt-gui
Copy the code
Such as:
Start the cmake – GUI:
Check the cmake-gui_test folder:
Compile and run:
Some of the compilation process and methods have been shared above.