[c/c++ compilation tool] - learning of Cmake

Introduction

Table of contents

Introduction

1. The basic syntax of Cmake

2. Common commands

3. CMake commonly used variables

4. CMake compile project

5. Construction method

6. Actual combat --- CMake code actual combat


  • CMake is a cross-platform installation and compilation tool, which can describe the installation (compilation process) of all platforms with simple sentences.
  • CMake can be said to have become the standard configuration of most C++ open source projects

Project files for compiling projects on different platforms are different. For example, under Visual Studio, an msbuild file is required, and a Makefile file needs to be written under Linux, so a project needs to write multiple compilation files on different platforms. But with the cmake tool, we only need to write the CMakeLists.txt file to solve the problem of cross-platform compilation.

 Suppose a new file bar.cpp is added to a project, if we compile in linux, we need to write the makefile file, if we compile under VS, we need to write the msbuild file. as follows:

 But with the cmake tool, we only need to write the CMakeLists.txt file, and cmake will generate corresponding compilation files according to different platforms. If it is under linux, cmake will generate corresponding makefiles according to CMakeLists.txt.

 

 

1. The basic syntax of Cmake

  • directive (parameter argument)
  • Instructions are case-insensitive, parameters and variables are case-sensitive

For example:

add_excutable(hello main.cpp hello.cpp)
ADD_EXCUTABLE(hello main.cpp hello.cpp)
  • The variable uses ${}, but the variable name is used directly in the IF control statement 

2. Common commands

Specify the minimum version instruction ---cmake_minimum_required

cmake_minimum_required(VERSION 2.8.3) #指定cmake的最小版本是2.8.3版本

Define the project name ---project

project(main) #定义工程名为hello
project(main[c][java])#定义工程名为hello,且支持的语言为c和java

Set variable ---set

set(SRC main.cpp hello.cpp) #设置SRC变量为main.cpp hello.cpp

link_directories is equivalent to the -i option in the specified header file in the g++ compiler

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

Add compilation parameters --- add_compile_option

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

Compile and generate executable files --- add_executable

语法:
add_executable(目标文件 源文件 源文件)
例子:
add_executable(main main.cpp) #编译main.cpp生成可执行文件main
add_executable(main main.cpp add.cpp) #编译main.cpp add.cpp生成可执行文件main

#也可以用一个变量来表示多个源文件
set(SRC_LIST a.cpp b.cpp c.cpp)
add_executable(${PROJECT_NAME} ${SRC_LIST})
#PROJECT_NAME 是设置的工程工程名称
target_link_libraries(main hello)#将hello动态库添加需要链接的共享库中

3. CMake commonly used variables

  • CMAKE_C_FLAGS gcc compilation options

  • CMAKE_CXX_FLAGES g++ compilation options

	#在CMAKE_CXX_FLAGES 中选项后追加-std=c++11
	set(CMAKE_CXX_FLAGES ,"${CMAKE_CXX_FLAGES} -std=c++11")
  • CMAKE_BUILD_TYPE compile type
#设定编译类型为debug,如果要对项目进行调试,需要将CMAKE_BUILD_TYP设置为Debug类型
set(CMAKE_BUILD_TYPE  Debug);
#设定编译类型为realse
set(CMAKE_BUILD_TYPE  Realse);

4. CMake compile project

A CMakeLists file needs to exist in the project main directory

There are two ways to set compilation rules:

  • The subfolder containing the source file contains the CMakeLists.txt file, and the CMakeLists.txt of the main directory can be added with a subdirectory through add_subdirectory

  • The subfolder containing the source files does not contain the CMakeLists.txt file, and the compilation rules of the subdirectory are reflected in the CMakeLists.txt of the main directory.

 compilation process

The process of using CMake to build a C/C++ project under the Linux platform is as follows:

  1. Manually write CmakeLists.txt

  2. Execute cmake PATH to generate Makefile (PATH is the directory where CMakeLists.txt is located)

  3. Execute the command make to compile

5. Construction method

internal build (not recommended)

Internal construction will generate a bunch of middleware files in the same directory . These middleware files are not what we want, and they will look messy when put together with project files.

#在当前目录下,编译本目录CMakeLists.txt 生成Makefile和其他文件
cmake .
#执行make指令,生成target
make

External build (recommended)

Put compiled files and source files in different directories

mkdir build
#进入到build文件夹中
cd build
#编译上级目录的CMakeLists.txt,生成Makefile和其他文件
cmake ..
#执行make命令,生成target
make

6. Actual combat --- CMake code actual combat

Write the CMakeLists.txt file to make the add.c and main.c files generate the main executable file

main.cc file:

#include<iostream>
using namespace std;

int main()
{
    cout<<add(10,20)<<endl;
    return 0;
}

add.hpp file

#include<iostream>

int add(int x,int y);

add.cc file

#include<iostream>

int add(int x,int y);

Writing needs to create a CMakeLists.txt file under the source file

CMakeLists.txt file content:

cmake_minimum_required(VERSION 2.8.12.2) #最低版本号

project(main)                            #项目名称

set(CMAKE_BUILD_TYPE  Debug);            #设置编译类型为Debug
add_executable(${PROJECT_NAME} main.cc add.cc) #编译文件

Next, create a build directory, and enter the build directory and execute cmake ..   , then 4 files will be generated in the build directory, including a Makefile file.

 After executing make, the main file will be generated in the build directory

Guess you like

Origin blog.csdn.net/sjp11/article/details/130058596