CMake 学习入门

1 篇文章 0 订阅
1 篇文章 0 订阅

CMake 学习入门

配置

  • CMakeList.txt 配置
    • 配置cmake版本要求
    • 配置工程
    • 添加可执行文件
    • note: cmake文件不区分大小写
cmake_minimum_required(VERSION 2.6)
project(Tutorial)
add_executable(Tutorial tutorial.cxx)
  • 版本号设置,使用set()函数
# The version number
set(Tutorial_VERSION_MAJOR 1)
set(Tutorial_VERSION_MINOR 0)
  • 配置头文件
# configure a header file to pass some of the CMake settings to the source code
configure_file(
	"${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
	"${PROJECT_BINARY_DIR}/TuturialConfig.h"
)
# add the binary tree to the search path for include files so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
  • 增加.in文件,联系工程文件与cmakelist的文件
//the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

note: 当CMake配置这个头文件时,”@Tutorial_VERSION_MAJOR@”和”@Tutorial_VERSION_MINOR@”的值将会被CMakeLists.txt文件中的值替换

添加一个库

  • 通过add_library()函数实现

    • MathFunctions的文件下,存在mysqrt.cxx
    add_library(MathFunctions mysqrt.cxx)
    
  • 在顶层目录的cmakelist.txt中添加链接目录

include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions) 
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)
  • 添加依赖可选

# should we use our own math functions?
option (USE_MYMATH  
        "Use tutorial provided math implementation" ON) 
  • 调整链接目录
# add the MathFunctions library?
#
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial  ${EXTRA_LIBS})
  • 为了在工程中使用USE_MYMATH,需要在.in文件中添加如下指令
#cmakedefine USE_MYMATH
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值