cmake常规使用


官方文档网址: CMake Documentation and Community
官方教程网址: CMake Tutorial — CMake 3.29.3 Documentation 
Download CMake,可以下载cmake,解压后有教程,如:\cmake-3.29.3\Help\guide\tutorial文件夹
命令解释 https://cmake.org/cmake/help/v2.8.12/cmake.html#section_Commands
中文的简单教程 https://www.jianshu.com/p/3078a4a195df

经常涉及到的3步:
1.为项目编写CMakeLists.txt文件(后面介绍常用的),会自动生成CMakeCache.txt。
2.生成项目的BuildSystem  https://cmake.org/cmake/help/latest/manual/cmake.1.html#generate-a-project-buildsystem
  cmake [<options>] -B <path-to-build> [-S <path-to-source>]
  cmake [<options>] <path-to-source>
  运行上面命令将创建CMakeFiles文件夹,并在该文件夹生成项目的BuildSystem
3.构建项目  https://cmake.org/cmake/help/latest/manual/cmake.1.html#build-a-project
  cmake --build <dir>  [<options>] [-- <build-tool-options>]
  在上面生成的BuildSystem的文件夹下 make 或 cmake --build .

为软件设置版本号范例


顶层CMakeLists.txt文件,基本的语句

# set cmake minimum version  (必须项)
cmake_minimum_required(VERSION 3.10)
# set the project name and version  (必须项)
project(Tutorial VERSION 1.0)

# configure a header file to pass some of the CMake settings
# to the source code    (可选项)
configure_file(TutorialConfig.h.in TutorialConfig.h)

# Use add_subdirectory() to add SubDirectoryName to this project    (可选项,当存在子目录时必选项)
add_subdirectory(SubDirectoryName)

# add the executable    (必须项)
add_executable(Tutorial tutorial.cxx)

# Use target_link_libraries to link the library to our executable    (可选项,当使用其他库或子目录生成的库时必选项)
target_link_libraries(Tutorial PUBLIC SubDirectoryLibName)

# Add SubDirectoryName to Tutorial's target_include_directories()    (可选项,当存在子目录时必选项)
# Hint: ${PROJECT_SOURCE_DIR} is a path to the project source. AKA This folder!
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
                           "${PROJECT_BINARY_DIR}"
                           "${PROJECT_SOURCE_DIR}/SubDirectoryName"
                           )

子目录的CMakeLists.txt文件,基本的语句

add_library(MathFunctions MathFunctions.cxx mysqrt.cxx) MathFunctions为生成的库,MathFunctions.cxx mysqrt.cxx为依赖的文件

或者 写成 下面3层 先生成库,再链接生成高层库
add_library(MathFunctions MathFunctions.cxx)
add_library(SqrtLibrary STATIC mysqrt.cxx)
target_link_libraries(MathFunctions PRIVATE SqrtLibrary)

用变量控制部分代码块使用和编译

CMakeLists.txt文件 创建变量
# Create a variable USE_MYMATH using option and set default to ON
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()




c++代码中  根据变量的值 确定是否使用代码
namespace mathfunctions {
double sqrt(double x)
{
  // If USE_MYMATH is defined, use detail::mysqrt.
  // Otherwise, use std::sqrt.
#ifdef USE_MYMATH
  return detail::mysqrt(x);
#else
  return std::sqrt(x);
#endif
  return detail::mysqrt(x);
}
}

生成静态库、动态库
add_library(archive SHARED archive.cpp zip.cpp lzma.cpp)
add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)默认生成静态库,所以STATIC可以省略

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值