[CMake] Introductory course under windows 10

[CMake] Introductory course under windows 10

Tip: The blogger has selected many blog posts from big guys and personally tested them to be effective. He shares his notes and invites everyone to study and discuss them together.



Preface

CMake (Cross-Platform Make) is an open source cross-platform build tool used to manage the build process of software projects. It uses a simple configuration file (CMakeLists.txt) to describe the build rules of the project, and then based on These rules generate build scripts or project files for different platforms and compilers.
The main role of CMake is to provide a platform-independent build system that enables developers to easily build their software projects on different operating systems and compilers, so it provides developers with A way to simplify and automate the build process while supporting various mainstream programming languages ​​such as C, C++, Python, etc.
The main functions and advantages of CMake:

  1. Cross-platform: CMake can generate build scripts or project files for different operating systems (such as Windows, Linux, macOS, etc.) and different compilers (such as GCC, Clang, Visual Studio, etc.), allowing developers to use them on different platforms Same CMake configuration to build the project.
  2. Simplified configuration: CMake uses a simple and flexible configuration language to describe the build rules of the project. By writing the CMakeLists.txt file, developers can specify source files, library dependencies, compilation options, target generation and other build-related information.
  3. Automated build: CMake automatically detects the source files and dependencies of the project and generates appropriate build rules. It can automatically adjust the build process according to the requirements of the target platform and compiler, including compilation, linking, and installation steps.
  4. Multiple generation options: CMake supports multiple generation options, including generating Makefiles, IDE project files (such as Visual Studio, Xcode, etc.), Ninja build files, etc. Developers can choose the most appropriate build system according to their needs.
  5. Modularity and extensibility: CMake provides a rich set of modules and function extension interfaces, allowing developers to customize and extend the build process. It supports the use of CMake modules to handle specific build tasks, such as finding third-party libraries, Generate documentation, etc.
    CMake is a powerful and flexible build tool that simplifies the building process of cross-platform software projects. By using CMake, developers can manage and build their projects more efficiently and reduce the time required to build cross-platform software projects. The amount of manual configuration and build effort on different platforms and compilers.

cmake installation

Download cmke at official website address, click "Download" to enter the download page, select the cmaker installation of the Windows operating system, mind selecting "cmake -x.xx.x-rc3-windows-x86_64.msi" file; open the installation package, follow the prompts to install cmaker to your computer, open the command line tool, enter the "cmake --version" command, if you can see cmaker version information, the installation is successful.


First introduction to cmake: Create a new helloworld project

  1. Create the helloworld.c file and write the most basic helloworld code.

    #include <stdio.h>
    int main(int argc, char **argv)
    {
          
          
        printf("hello\n");
        return 0;
    }
    
  2. Create the "CMakeLists.txt" file. Note that the name here cannot contain any errors, otherwise subsequent configuration generation will fail.

    # 指定cmake的最小版本
    cmake_minimum_required(VERSION 3.5)
    # 指定工程名字
    project(hello_world)
    # 生成可执行文件xxx.exe
    add_executable(hello_world helloworld.c)
    
  3. cmake configures and generates the project, and creates a new build folder in the same directory of CMakeLists.txt and helloworld.c. Usually the configuration-generated project is placed in the build folder in the same directory of CMakeLists.tx and helloworld.ct. , to prevent the generated project files from being confused with the original files. cmd enters the build path and executes the command to compile the code. cmake uses the Visual Studio 2019 version by default.

    # 生成vs工程(windoes下) ..表示在上级目录寻找cnake文件
    cmake ..
    # 开始编译代码,生成可执行程序exe
    cmake --build .
    # 在build\Debug生成exe,可以用cmd执行该文件
    

  4. Open the project with vs2019. The c source code file and cmake file are copied to the test directory. You can execute test.c under release|64 or debug|64. You may need to set text as a startup item.


Getting started with cmake

  1. Displays all IDEs supported by cmake. You can view all IDE versions supported by cmake under Windows 10. The * sign indicates the version supported by default.
    cmake -G
    
  2. To generate a project for a specified Visual Studio version, you need to install the corresponding Visual Studio version.
    # 生成Visual Studio 10 2017版本的工程,编译生成的文件可以在任何地方
    # 默认生成与当前操作系统位数相匹配的项目文件,生成64位
    cmake -G "Visual Studio 15 2017" ..\demo
    # 生成32位,旧版本
    cmake -G "Visual Studio 15 2017 Win32" ..\demo
    # 新版本
    cmake -G "Visual Studio 15 2017 " -A Win32 ..\demo
    # 生成MinGW的项目
    cmake -G "MinGW Makefiles" ..\demo
    
  3. CMake compilation options: Debug build, CMake will configure the build for debugging purposes, enable debugging symbols and disable optimization; Release build, CMake will optimize the build of the release version, enable various optimization flags, increase code execution speed and reduce executable File size; RelWithDebInfo build, CMake will include both optimization and debugging information in the generated executable file, which is a mixed type; MinSizeRel build, CMake will generate the smallest executable file to cope with resource-limited environments or Application scenarios that are sensitive to executable file size.
    # CMAKE_BUILD_TYPE:用于指定编译类型
    # 生成vs工程
    cmake -D CMAKE_CONFIGURATION_TYPES="Release;Debug" ..\demo
    # 编译(Release或Debug)
    cmake --build . --config Release
    cmake --build . --config Debug
    
  4. Generate a static library, create a new header file math.h and a source file math.c as the source files being called to provide functions.
    ----------------------------------------------------------------------
    // test.c
    #include <stdio.h>
    #include "math.h"
    int main()
    {
          
          
        printf(" 5-4=%d\n", sub(5, 4));
        return 0;
    }
    ----------------------------------------------------------------------
    // math.h
    #ifndef MATH_H_
    //用于避免头文件的重复包含
    #define MATH_H_
    int sub(int m, int n);
    #endif // MATH_H_
    ----------------------------------------------------------------------
    // math.c
    #include "math.h"
    int sub(int m, int n)
    {
          
          
        return m - n;
    }
    ----------------------------------------------------------------------
    
    # 在CMakeLists.txt加入指令
    # math.c生成静态库
    add_library(math STATIC math.c)
    # 使用静态库
    target_link_libraries(hello_world math)
    
    cmake ..
    cmake --build .
    
    Generate the lib library first, and then generate the exe.
  5. Generate dynamic library
    # 在CMakeLists.txt加入指令
    # math.c生成动态库,注意在dll同目录下必须要有lib
    add_library(math SHARED math.c)
    # 链接动态库
    target_link_libraries(hello_world math)
    
    cmake ..
    cmake --build .
    
    The linker cannot find the file named '.lib', an error occurred.
    Place the generated static library file in the same directory as the dll and successfully generate the dynamic library.

Summarize

It uses simple examples to explain the most basic use of cmake. In the future, based on the blogger's learning and usage, the cmake learning tutorial will be gradually enriched and shared with everyone to discuss and make progress together.

Guess you like

Origin blog.csdn.net/yangyu0515/article/details/134050955
Recommended