• 【CPP】CMake


    Examples

    main.cpp

    #include 
    #include "functions.h"
    using namespace std;
    
    int main()
    {
        printhello();
    
        cout << "This is main:" << endl;
        cout << "The factorial of 5 is: " << factorial(5) << endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    factorial.cpp

    #include "functions.h"
    
    int factorial(int n)
    {
        if (n == 1)
            return 1;
        else
            return n * factorial(n - 1);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    printhello.cpp

    #include 
    #include "functions.h"
    
    using namespace std;
    
    void printhello()
    {
        int i;
        cout << "Hello World!" << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    functions.h

    #ifndef _FUNCTIONS_H_
    #define _FUNCTIONS_H_
    void printhello();
    int factorial(int n);
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5

    CMake

    What is CMake

    CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice.

    CMake needs CMakeLists.txt to run properly.
    A CMakeLists.txt consists of commands , comments and spaces.

    • The commands include command name, brackets and parameters ,
      the parameters are separated by spaces. Commands are not case sensitive.
    • Comments begins with ‘#’.

    A single source file in a project

    CMakeList.txt

    cmake_minimum_required(VERSION 3.10)
    
    project(hello)
    
    add_executable(hello main.cpp factorial.cpp printhello.cpp)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    cmake .
    
    • 1

    在这里插入图片描述在这里插入图片描述
    生成了cmake_install.cmake, CMakeCache.txt, Makefile 三个文件
    以及一个文件夹 CMakeFiles

    编译:

    make
    
    • 1

    在这里插入图片描述

    生成hello可执行文件

    ./hello
    
    • 1

    在这里插入图片描述

    make clean
    
    • 1

    删掉了hello文件

    但是还有其他文件没有删掉,需手动删除。

    所以建议在CMake之前就新建一个build文件夹,在build文件夹里进行cmake

    mkdir build
    cd build
    cmake ..
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    附录

    生成的Makefile是这样的:

    # CMAKE generated file: DO NOT EDIT!
    # Generated by "Unix Makefiles" Generator, CMake Version 3.27
    
    # Default target executed when no arguments are given to make.
    default_target: all
    .PHONY : default_target
    
    # Allow only one "make -f Makefile2" at a time, but pass parallelism.
    .NOTPARALLEL:
    
    #=============================================================================
    # Special targets provided by cmake.
    
    # Disable implicit rules so canonical targets will work.
    .SUFFIXES:
    
    # Disable VCS-based implicit rules.
    % : %,v
    
    # Disable VCS-based implicit rules.
    % : RCS/%
    
    # Disable VCS-based implicit rules.
    % : RCS/%,v
    
    # Disable VCS-based implicit rules.
    % : SCCS/s.%
    
    # Disable VCS-based implicit rules.
    % : s.%
    
    .SUFFIXES: .hpux_make_needs_suffix_list
    
    # Command-line flag to silence nested $(MAKE).
    $(VERBOSE)MAKESILENT = -s
    
    #Suppress display of executed commands.
    $(VERBOSE).SILENT:
    
    # A target that is always out of date.
    cmake_force:
    .PHONY : cmake_force
    
    #=============================================================================
    # Set environment variables for the build.
    
    # The shell in which to execute make rules.
    SHELL = /bin/sh
    
    # The CMake executable.
    CMAKE_COMMAND = /opt/homebrew/Cellar/cmake/3.27.7/bin/cmake
    
    # The command to remove a file.
    RM = /opt/homebrew/Cellar/cmake/3.27.7/bin/cmake -E rm -f
    
    # Escaping for special characters.
    EQUALS = =
    
    # The top-level source directory on which CMake was run.
    CMAKE_SOURCE_DIR = /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab
    
    # The top-level build directory on which CMake was run.
    CMAKE_BINARY_DIR = /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab
    
    #=============================================================================
    # Targets provided globally by CMake.
    
    # Special rule for the target edit_cache
    edit_cache:
    	@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake cache editor..."
    	/opt/homebrew/Cellar/cmake/3.27.7/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
    .PHONY : edit_cache
    
    # Special rule for the target edit_cache
    edit_cache/fast: edit_cache
    .PHONY : edit_cache/fast
    
    # Special rule for the target rebuild_cache
    rebuild_cache:
    	@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..."
    	/opt/homebrew/Cellar/cmake/3.27.7/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
    .PHONY : rebuild_cache
    
    # Special rule for the target rebuild_cache
    rebuild_cache/fast: rebuild_cache
    .PHONY : rebuild_cache/fast
    
    # The main all target
    all: cmake_check_build_system
    	$(CMAKE_COMMAND) -E cmake_progress_start /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab/CMakeFiles /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab//CMakeFiles/progress.marks
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 all
    	$(CMAKE_COMMAND) -E cmake_progress_start /Users/jiaweiluo/luojw/Shiqiyu/CPP/week05/examples/lab/CMakeFiles 0
    .PHONY : all
    
    # The main clean target
    clean:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 clean
    .PHONY : clean
    
    # The main clean target
    clean/fast: clean
    .PHONY : clean/fast
    
    # Prepare targets for installation.
    preinstall: all
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
    .PHONY : preinstall
    
    # Prepare targets for installation.
    preinstall/fast:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 preinstall
    .PHONY : preinstall/fast
    
    # clear depends
    depend:
    	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
    .PHONY : depend
    
    #=============================================================================
    # Target rules for targets named hello
    
    # Build rule for target.
    hello: cmake_check_build_system
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 hello
    .PHONY : hello
    
    # fast build rule for target.
    hello/fast:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/build
    .PHONY : hello/fast
    
    factorial.o: factorial.cpp.o
    .PHONY : factorial.o
    
    # target to build an object file
    factorial.cpp.o:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/factorial.cpp.o
    .PHONY : factorial.cpp.o
    
    factorial.i: factorial.cpp.i
    .PHONY : factorial.i
    
    # target to preprocess a source file
    factorial.cpp.i:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/factorial.cpp.i
    .PHONY : factorial.cpp.i
    
    factorial.s: factorial.cpp.s
    .PHONY : factorial.s
    
    # target to generate assembly for a file
    factorial.cpp.s:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/factorial.cpp.s
    .PHONY : factorial.cpp.s
    
    main.o: main.cpp.o
    .PHONY : main.o
    
    # target to build an object file
    main.cpp.o:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/main.cpp.o
    .PHONY : main.cpp.o
    
    main.i: main.cpp.i
    .PHONY : main.i
    
    # target to preprocess a source file
    main.cpp.i:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/main.cpp.i
    .PHONY : main.cpp.i
    
    main.s: main.cpp.s
    .PHONY : main.s
    
    # target to generate assembly for a file
    main.cpp.s:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/main.cpp.s
    .PHONY : main.cpp.s
    
    printhello.o: printhello.cpp.o
    .PHONY : printhello.o
    
    # target to build an object file
    printhello.cpp.o:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/printhello.cpp.o
    .PHONY : printhello.cpp.o
    
    printhello.i: printhello.cpp.i
    .PHONY : printhello.i
    
    # target to preprocess a source file
    printhello.cpp.i:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/printhello.cpp.i
    .PHONY : printhello.cpp.i
    
    printhello.s: printhello.cpp.s
    .PHONY : printhello.s
    
    # target to generate assembly for a file
    printhello.cpp.s:
    	$(MAKE) $(MAKESILENT) -f CMakeFiles/hello.dir/build.make CMakeFiles/hello.dir/printhello.cpp.s
    .PHONY : printhello.cpp.s
    
    # Help Target
    help:
    	@echo "The following are some of the valid targets for this Makefile:"
    	@echo "... all (the default if no target is provided)"
    	@echo "... clean"
    	@echo "... depend"
    	@echo "... edit_cache"
    	@echo "... rebuild_cache"
    	@echo "... hello"
    	@echo "... factorial.o"
    	@echo "... factorial.i"
    	@echo "... factorial.s"
    	@echo "... main.o"
    	@echo "... main.i"
    	@echo "... main.s"
    	@echo "... printhello.o"
    	@echo "... printhello.i"
    	@echo "... printhello.s"
    .PHONY : help
    
    
    
    #=============================================================================
    # Special targets to cleanup operation of make.
    
    # Special rule to run CMake to check the build system integrity.
    # No rule that depends on this can have commands that come from listfiles
    # because they might be regenerated.
    cmake_check_build_system:
    	$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
    .PHONY : cmake_check_build_system
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
  • 相关阅读:
    2022年最新广西道路运输安全员真题题库及答案
    JWT——讲解
    HTML5+CSS3小实例:悬停放大图片的旅游画廊
    JavaScript基础
    redis集群三主三从搭建
    1553_AURIX_TC275_CCU寄存器以及模块的独立时钟生成
    53. 最大子序和 392.判断子序列 115.不同的子序列 583. 两个字符串的删除操作 72. 编辑距离 编辑距离总结篇
    python趣味编程-5分钟实现一个益智数独游戏(含源码、步骤讲解)
    基于java(ssm)个人健康管理系统
    刷题日记【第十一篇】-笔试必刷题【小易的升级之路+找出字符串中第一个只出现一次的字符+微信红包+计算字符串的编辑距离】
  • 原文地址:https://blog.csdn.net/weixin_38362786/article/details/133915780