cmake小知识

目录

cmake编译选项

CMakeLists.txt 常用命令

aux_source_directory

cmake编译选项

cmake command line:

cmake [<options>] (<path-to-source> | <path-to-existing-build>)
cmake [(-D <var>=<value>)...] -P <cmake-script-file>
cmake --build <dir> [<options>...] [-- <build-tool-options>...]
cmake -E <command> [<options>...]
cmake --find-package <options>...

参考: https://cmake.org/cmake/help/v3.10/manual/cmake.1.html

options:

-C <initial-cache>

Pre-load a script to populate the cache.

-D <var>:<type>=<value>, -D <var>=<value>,  -D<var>:<type>=<value> or -D<var>=<value>

     Create a cmake cache entry.

-N

View mode only.

Only load the cache. Do not actually run configure and generate steps.

-P <file>

Process script mode.

Process the given cmake file as a script written in the CMake language. No configure or generate step is performed and the cache is not modified. If variables are defined using -D, this must be done before the -P argument.

-E

CMake command mode.

For true platform independence, CMake provides a list of commands that can be used on all systems. Run with -E help for the usage information. Commands available are: chdir, compare_files, copy, copy_directory, copy_if_different, echo, echo_append, environment, make_directory, md5sum, remove, remove_directory, rename, sleep, tar, time, touch, touch_nocreate. In addition, some platform specific commands are available. On Windows: delete_regv, write_regv. On UNIX: create_symlink.

cmake -P <script_name>.camke

读取<script_name>.camke文件,执行cmake命令.

cmake 语法:

参考: https://cmake.org/cmake/help/v3.3/command/string.html

string(REGEX REPLACE <regular_expression>
       <replace_expression> <output variable>
       <input> [<input>...])

例如: STRING(REGEX REPLACE "/$""" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")

        将${CMAKE_INSTALL_PREFIX}结尾的/去掉

message([<mode>] "message to display" ...)

向用户显示信息, 其中mode可以是以下几种:

(none)         = Important information
STATUS         = Incidental information
WARNING        = CMake Warning, continue processing
AUTHOR_WARNING = CMake Warning (dev), continue processing
SEND_ERROR     = CMake Error, continue processing,
                              but skip generation
FATAL_ERROR    = CMake Error, stop processing and generation
DEPRECATION    = CMake Deprecation Error or Warning if variable
                 CMAKE_ERROR_DEPRECATED or CMAKE_WARN_DEPRECATED
                 is enabled, respectively, else no message.

例如: MESSAGE(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"")

RPATH使用

动态库查找路径的顺序: RPATH   --   LD_LIBRARY_PATH -- RUNPATH(出现会屏蔽RPATH) -- /etc/ld.so.conf -- builtin dircetories(/lib, /usr/lib)

CMake里面维护了3个比较重要的RPATH变量:

CMAKE_SKIP_RPATH 强制CMake不在构建期间和安装install期间给你加上它所认为的RPATH;

CMAKE_SKIP_BUILD_RPATH 在构建期间不允许CMake给你加入相关RPATH;

CMKAE_SKIP_INSTALL_RPATH 在安装期间不允许CMake给你加入相关RPATH.

cmake .. -DCMAKE_SKIP_RPATH=TRUE
 
 
cmake .. -DCMAKE_SKIP_BUILD_RPATH=TRUE cmake .. -DCMAKE_SKIP_INSTALL_RPATH=TRUE

CMake为了方便用户的安装,默认在make install之后会自动remove掉相关的RPATH,这个时候你再用readelf -d myexe或ldd -r myexe的方法去查看exe的RPATH,已经发现没有这个字段了。因此,当每次make install之后,我们进入到安装路径下执行相关exe的时候,就会发现此时的exe已经找不到相关的库路径了。但我们可以使用下面的方法保留住rpath信息.

#所有动态库
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
#特定动态库
 
set_target_properties(myexe PROPERTIES INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

CMakeLists.txt 常用命令

aux_source_directory

查找在某个路径下的所有源文件。

aux_source_directory(< dir > < variable >)
搜集所有在指定路径下的源文件的文件名,将输出结果列表储存在指定的变量中。

参考:https://blog.csdn.net/u012564117/article/details/95085360

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值