《CMake 进阶之路》第三章 Usage Requirements for Library

  • 更好的操作一个库或可执行的链接文件

target_compile_definitions()

target_compile_definitions(<target>
  <INTERFACE|PUBLIC|PRIVATE> [items1...]
  [<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])

Specifies compile definitions to use when compiling a given <target>. The named <target> must have been created by a command such as add_executable() or add_library() and must not be an ALIAS target.

名字要和上面两个命令里面指定的一致!

target_compile_options()

target_include_directories()

target_link_libraries

让我们用现代的CMake,对第二章节的代码进行重构

Remember INTERFACE means things that consumers require but the producer doesn't.

这句话有点设计模式的味道,不翻译。下面是cmake.org的源代码

Add the following lines to the end of MathFunctions/CMakeLists.txt:

修改MathFunctions目录下的CMakeLists.txt

target_include_directories(MathFunctions
          INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
          )

Lesson03实际工程代码长这样

同样修改TopLevel的CMakeLists.txt

# Now that we've specified usage requirements for MathFunctions we can safely remove our uses of the EXTRA_INCLUDES variable from the top-level CMakeLists.txt, here:

if(USE_MYMATH)
  add_subdirectory(MathFunctions)
  list(APPEND EXTRA_LIBS MathFunctions)
endif()


target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           )

下面是我实际工程的代码

全部再次编译

再用tree .查看

代码又演进了一步。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值