• CMake官方教程3--对库加入使用限制


    1. 对库加入限制

    • CMakeFile.txt
    cmake_minimum_required(VERSION 3.10)
    
    # set the project name and version
    project(Tutorial VERSION 1.0)
    
    
    # specify the C++ standard
    set(CMAKE_CXX_STANDARD 11)
    
    configure_file(TutorialConfig.h.in TutorialConfig.h)
    add_subdirectory(MathFunctions)
    
    
    add_executable(Tutorial tutorial.cxx)
    
    
    target_link_libraries(Tutorial PUBLIC MathFunctions)
    
    target_include_directories(Tutorial PUBLIC
                               "${PROJECT_BINARY_DIR}"
                               #${EXTRA_INCLUDES}
                               )
                                             
                                                      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • MathFunctions/CMakeLists
    add_library(MathFunctions MathFunctions.cxx)                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                         
    target_include_directories(MathFunctions INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")                                                                                                                                          
                                                                                                                                                                                       
    option(USE_MYMATH "Use tutorial provided math implementation" ON)                                                                                                                                                          
    if (USE_MYMATH)                                                                                                                                                                                                            
      target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")                                                                                                                                                           
                                                                                                                                                                                               
      add_library(SqrtLibrary STATIC                                                                                                                                                                                           
                  mysqrt.cxx                                                                                                                                                                                                   
                  )                                                                                                                                                                                                            
                                                                                                                                                                                                                               
     
    target_include_directories(MathFunctions INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
    
    
    option(USE_MYMATH "Use tutorial provided math implementation" ON)
    if (USE_MYMATH)
      target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
    
      add_library(SqrtLibrary STATIC
                  mysqrt.cxx
                  )
    
      target_link_libraries(MathFunctions PRIVATE SqrtLibrary)
    endif()
                                                                                                                                                                                                                                        
                                                        
    
    • 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

    对于一个库如果只用头文件,则声明为 I N T E R F A C E INTERFACE INTERFACE;
    如果只要源文件,则声明为 P R I V A T E PRIVATE PRIVATE;
    否则使用 P U B L I C PUBLIC PUBLIC

    2. 使用接口库设置C++标准

    • CMakeLists.txt
    cmake_minimum_required(VERSION 3.10)
    
    project(Tutorial VERSION 1.0)
    
    
    # specify the C++ standard
    add_library(tutorial_compiler_flags INTERFACE)
    target_compile_features(tutorial_compiler_flags INTERFACE cxx_std_11)
    
    
    configure_file(TutorialConfig.h.in TutorialConfig.h)
    
    add_subdirectory(MathFunctions)
    
    add_executable(Tutorial tutorial.cxx)
    
    target_link_libraries(Tutorial PUBLIC MathFunctions tutorial_compiler_flags)
    
    target_include_directories(Tutorial PUBLIC
                               "${PROJECT_BINARY_DIR}"
                               #${EXTRA_INCLUDES}
                               )
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    target_comiler_flags INTERFACE是一个接口库不会产生编译目标

    • Mathfunctions/CMakeLists.txt
    add_library(MathFunctions MathFunctions.cxx)
    
    
    target_include_directories(MathFunctions INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")
    
    
    option(USE_MYMATH "Use tutorial provided math implementation" ON)
    if (USE_MYMATH)
      target_compile_definitions(MathFunctions PRIVATE "USE_MYMATH")
    
      add_library(SqrtLibrary STATIC
                  mysqrt.cxx
                  )
    
      target_link_libraries(SqrtLibrary PUBLIC tutorial_compiler_flags)
      target_link_libraries(MathFunctions PUBLIC SqrtLibrary)
    endif()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
  • 相关阅读:
    06 nginx 处理转发其他域的处理 以及 proxy_redirect
    stable diffusion实践操作-LyCORIS
    艺术与科技的狂欢,阿那亚2022砂之盒沉浸艺术季
    Win11打不开组策略编辑器怎么办
    Mysql高级
    IO流高级流
    云计算安全和云原生安全的关系
    从小白到程序员的攻略(适合自学编程的网站)
    JavaSE学习文档(上)
    MySQL -- DQL
  • 原文地址:https://blog.csdn.net/bdn_nbd/article/details/136748632