[Compile] gcc make cmake Makefile CMakeList.txt relationship and usage

a relationship

1 gccis a compiler . Convert source code into executable files.

When your program has only one source file, you can directly use the gcc command to compile it. But when your program contains many source files and you use the gcc command to compile them one by one, you will easily get confused and have a heavy workload, so the make tool appeared.

2 makeis a batch processing tool . Manage the compilation process throughout the project. It is based on the makefile.

3 makefileis a text file used to command make . It contains a series of rules and commands. The make tool reads the rules in the makefile to determine which files need to be recompiled and which commands to execute to complete the compilation process.

The makefile can be written by hand in some simple projects, but when the project is very large, handwriting the makefile is also very troublesome, and if the platform is changed, the makefile will have to be modified again. At this time, the Cmake tool appeared.

4 cmakeis a cross-platform build tool . cmake can more easily generate makefiles for use by make.

5 cmakelistis a text file used to command cmake .

image-20230726151254616

Reference URL: http://t.csdn.cn/iNMcN



2 gcc

Installation :

sudo apt update
sudo apt install build-essential gdb

2.1 Compilation process

  • Preprocessing-E .i expansion macro
  • Compile -S .s c->Assemble
  • Assembly-c .o Assembly->Binary
  • link -o bin file

image-20230731165711403

2.2 Compilation parameters

1 -g with debugging information

2 -O2 optimize source code

3 -l -L specifies the library file | specifies the library file path

4 -I specifies the header file search directory

5 -Wall prints warning messages

6 -w turns off warning messages

7 -std=c++11 Set compilation standard

8 -o specifies the output file name

9 -D defines macro

image-20230731165958597

image-20230731170020543

image-20230731170034849

2.3 Static libraries and dynamic libraries

1 suffix
  • Static library suffix name:.a
  • Dynamic library suffix name:.so
2 Connections and Differences
  • Static libraries are linked into the target program during compilation and become part of the program. The advantage is that the executable file does not depend on external libraries when running. The disadvantage is that the file size is larger.
  • The dynamic library will not be copied to the final executable file, saving memory and compatibility issues.

2.4 GDB debugger

  • Install

    sudo apt update
    sudo apt install build-essential gdb
    
  • Need to compile with -g

    gcc -g main.c -o main

1 Commonly used commands

image-20230726212901303

image-20230726212915786

三 make、makefile

Four cmake, cmakelist

4.1 Grammatical features

image-20230728110412455

4.2 Important commands

# CMake最小版本要求为2.8.3
cmake_minimum_required(VERSION 2.8.3)

# 指定工程名为HELLOWORLD
project(HELLOWORLD)

# 定义SRC变量,其值为sayhello.cpp hello.cpp
set(SRC sayhello.cpp hello.cpp)

# 将/usr/include/myincludefolder 和 ./include 添加到头文件搜索路径
include_directories(/usr/include/myincludefolder ./include)

# 将/usr/lib/mylibfolder 和 ./lib 添加到库文件搜索路径
link_directories(/usr/lib/mylibfolder ./lib)

# 通过变量 SRC 生成 libhello.so 共享库
add_library(hello SHARED ${SRC})

# 添加编译参数 -Wall -std=c++11 -O2
add_compile_options(-Wall -std=c++11 -O2)

# 编译main.cpp生成可执行文件main
add_executable(main main.cpp)

# 将hello动态库文件链接到可执行文件main
target_link_libraries(main hello)

# 添加src子目录,src中需有一个CMakeLists.txt
add_subdirectory(src)

# 定义SRC变量,其值为当前目录下所有的源代码文件
aux_source_directory(. SRC)
    
# 编译SRC变量所代表的源代码文件,生成main可执行文件
add_executable(main ${SRC})

image-20230731165308494

image-20230731165343782

image-20230731165410438

4.2 Important variables

image-20230731165534974

image-20230731165553971

4.3 Compilation process

  • 1 Write CMakeLists.txt
  • 2 Execute the command: cmake path to generate makefile
  • 3 Execute command: make

4.4 Two construction methods

image-20230731153412517

FiveVscode

5.0 Commonly used shortcut keys

shortcut key Function
Line comments ctrl+/
block comments ctrl+shift+A
Move current row alt+up/down
command panel ctrl+shift+p
Go to file ctrl+p
Go to row ctrl+g
Open terminal ctrl+~
variable rename F2
Open/close sidebar ctrl+B
Editor split ctrl+\
thumbnail Menu Bar: View/Appearance
Code indentation ctrl+[/]
Code formatting ctrl+shift+I
Insert a line below ctrl+enter
move to definition F12
Zoom in and out of editing area ctrl+ -/+

5.1 Interface

Four major areas : menu bar, sidebar, editing area, status bar

image-20230728095310489

5.2 Plug-ins

image-20230731164620130

5.3 .vscode folder

1 launch.json
2 task.json

Guess you like

Origin blog.csdn.net/weixin_44029896/article/details/132026326