cmake basic

作者: SnC_ | 来源:发表于2021-07-07 19:41 被阅读0次

    参考链接

    一般我们用cmake进行build都是按照这样的操作:

    mkdir build
    cd build
    cmake ..
    make
    

    把编译生成的文件放入build中,不至于影响源文件。

    上面的做法缺乏一些灵活性,比如我们的编译工具不用make,而是其他工具如ninja,则需要修改上面的命令。
    如果把make改为cmake --build .,则可以解决这个问题。

    mkdir build
    cd build
    cmake ..
    cmake --build .
    

    Minimal version
    cmake的开头一般为

    # Sets the minimum required version of cmake for a project. Also updates the policy settings.
    cmake_minimum_required(VERSION 3.1)
    

    指令的大小写无关紧要。
    VERSION是这个指令的专用keyword,它之后跟的是其值。


    Project
    然后,会跟上

    # Sets the name of the project, and stores it in the variable [MyProject]
    # When called from the top-level `CMakeLists.txt` also stores the project name
    # in the variable [CMAKE_MyProject]
    project(MyProject VERSION 1.0
                      DESCRIPTION "Very nice project"
                      LANGUAGES CXX)
    

    这里的所有keyword都是可选项。


    Executable

    # Adds an executable target called <one> to be built from
    # the source files listed in the command invocation. 
    add_executable(one two.cpp three.h)
    

    one是生成的可执行文件名,也是cmake中的target。
    之后列出的是要用到的所有suorce files。个数不限。
    头文件一般会被忽略,把它放进来是为了在IDE中显示。


    Library

    # Add a library to the project using the specified source files.
    add_library(another STATIC another.cpp another.h)
    

    library的类型可以为STATIC、SHARED、MODULE。如果没有在指令中指定,则会使用BUILD_SHARED_LIBS中的值。
    指令会根据所处平台生成库文件,比如libanother.a或another.lib等。


    Target

    # Specifies include directories to use when compiling a given target.
    # The target must have been created by a command such
    # as add_executable() or add_library()
    target_include_directories(one PUBLIC inc)
    

    若"one"是executable,则PUBLIC在这里没有意义。
    若"one"是library,则PUBLIC表示所有link到one的target,都需要Include "inc"路径。

    我们可以将target串起来:

    add_library(another STATIC another.cpp another.h)
    target_link_libraries(another PUBLIC one)
    

    这表示another依赖于one。
    target_link_libraries是个很有用但也很复杂的指令,用法很多。

    Target是cmake中非常关键的一个概念。围绕它可以做include directories, linked libraries (or linked targets), compile options, compile definitions, compile features等等操作。
    熟悉了Target,你会知道cmake的优秀之处。


    Example
    来看一个例子,以更好地串联上述概念。

    cmake_minimum_required(VERSION 3.8)
    
    project(Calculator LANGUAGES CXX)
    
    add_library(calclib STATIC src/calclib.cpp include/calc/lib.hpp)
    target_include_directories(calclib PUBLIC include)
    target_compile_features(calclib PUBLIC cxx_std_11)
    
    add_executable(calc apps/calc.cpp)
    target_link_libraries(calc PUBLIC calclib)
    

    在执行cmake以及make的时候,为了解其详细运行过程,我们可以在CMakeLists.txt中加上
    set(CMAKE_VERBOSE_MAKEFILE ON)

    相关文章

      网友评论

        本文标题:cmake basic

        本文链接:https://www.haomeiwen.com/subject/szzoultx.html