cmake学习笔记

        在使用cmake编写项目管理脚本时,除了需要有cmake语法的基本知识外,最好是还有一本书便于你查询需要的cmake变量、命令和属性,除非你非常非常熟悉cmake的语法。cmake提供了网页版的帮助文档:cmake 2.6 documentation。不过这个我用起来不太顺手,我还是比较喜欢用ken martin(cmake的作者)写的《mastering cmake》,在网上书店有的购买。。我这里提供了带目录的mastering cmake可供下载


1.      命令Cmake_minimum_required与cmake_policy的作用于区别?

命令Cmake_minimum_required是限制当前cmake的版本的最低要求;

命令cmake_policy是在cmake 2.6版本开始添加的,主要的一个作用是用于设置向下/向上兼容的特性。

>The cmake policy mechanism is designed to help keepexisting projects building as new version of cmake introduce changes inbehavior.

Cmake policy的机制旨在帮助现有工程能够使用行为发生改变的新的版本的cmake来编译。

2.      如何使用命令行cmake,使得生成的工程相关文件指定到某个目录下而不是在CMakeLists.txt所在的目录下?

使用cd命令到达目的目录,然后在这个目录使用cmake命令即可。

3.      如何添加include路径以及如何查看现有的include路径?

使用include_directories命令可添加include路径,在生成的vs工程中,添加的include路径在inherited values中可以看到:


更直接的查看方式是查看c/c++的command line的窗口:


查看现有的include路径listinginclude_directories in cmake.

#Q: 为什么在我搭的跨平台开发环境中每次在生成vs工程的时候,在工程属性的addition include directories中都会有/local32/include这样一个目录(如上图所示)?

#A: 在我的shell配置(/local32/etc/profile.local)中设置了这几项:


#Q: CPPFLAGS, CFLAGS, CXXFLAGS和LDFLAGS与cmake有什么关联呢?或者这些与shell又有什么关联呢?

#A: Wikipedia:CFLAGS

>CFLAGS and CXXFLAGS are either the name of environmentvariables or of makefile variables that can be set to specify additionalswitches to be passed to a compiler in the process of building computersoftware.

>CFLAGS allows to add switches for the C compiler, whileCXXFLAGS is meant to be used when invoking a c++ compiler. Similarly, avariable CPPFLAGS exists with switches to be passed to the c or c++preprocessor.

Variablesused by implicit rules.

Makefile编译选项CC与CXX/CPFLAGS、CFLAGS与CXXFLAGS/LDFLAGS

上面这些都是与makefile相关的,都是与make有直接的关系。那么这些变量与cmake有什么关系呢?

[cmake]CFLAGSand CPPFLAGS

Cmake里有变量CMAKE_C_FLAGS和CMAKE_CXX_FLAGS,在shell环境中设置的CFLAGS和CXXFLAGS会被cmake附加。

那我大概清楚了,在linux环境下设置环境变量CFLAGS,CPPFLAGS和CXXFLAGS等等,可直接用于make(可在shell下输入make –p查看);而在cmake中有CMAKE_C_FLAGS和CMAKE_CXX_FLAGS,这在cmake生成各类工程文件时有用,而在linux环境下设置的CFLAGS和CXXFLAGS环境变量会被cmake分别自动附加到CMAKE_C_FLAGS和CMAKE_CXX_FLAGS中(事实上在windows环境下也可以设置环境变量CFLAGS和CXXFLAGS,这样也能被cmake识别并附加)。在windows上的mingw/msys环境下,也会自动加载windows设置的所有的环境变量。

注意:在cmake生成某工程后,产生了cmakecache.txt,如果这时候改变环境变量CFLAGS或CXXFLAGS,cmake不会改变CMAKE_C_FLAGS/CMAKE_CXX_FLAGS(除非在cmakelists.txt文件中修改了这个变量),此时只有删除cmakecache.txt,cmake才能应用这些更新。

4.      如何制定目标输出路径?

设置属性ARCHIVE_OUTPUT_DIRECTORY, LIBRARY_OUTPUT_DIRECTORY和RUNTIME_OUTPUT_DIRECTORY。

可使用命令Set_target_properties或者set_properties(target)。

但是有几个问题:

1)      如果目标是add_library(targetname SHARED …),也即生成的是dynamic dll,设置LIBRARY_OUTPUT_DIRECTORY无效,而RUNTIME_OUTPUT_DIRECTORY是决定dll的输出的,ARCHIVE_OUTPUT_DIRECTORY决定lib的输出。

2)      采用上面参数设置后,在各个配置类型下仍然会在设置的目录下加上debug, release等目录。要想为各个配置类型设置对应的目录的话就需要设置属性ARCHIVE_OUTPUT_DIRECTORY_<CONFIG>

遍历配置类型的方法:

Foreach(config${CMAKE_CONFIGURATION_TYPES})
Command…
Endforeach()

>Executablesare always treated as runtime targets (example: *.exe). Static libraries arealways treated as archive targets (example: *.lib). Module libraries are alwaystreated as library targets (example: *.dll). For non-DLL platforms sharedlibraries are treated as libraries targets . For DLL platforms the DLL part ofa shared library is treated as a runtime target and the corresponding importlibrary is treated as an archive target. All Windows-based systems includingCygwin are DLL platforms.

在目标为shared library的情况下,在non-DLL平台下,起作用的是ARCHIVE_OUTPUT_DIRECTORY和LIBRARY_OUTPUT_DIRECTORY;在dll平台下,起作用的是ARCHIVE_OUTPUT_DRECTORY和RUNTIME_OUTPUT_DIRECTORY

5.      如何为不同的编译配置类型设置单独的属性,比如include路径和link路径?

a)        设置link路径

没有直接的设置方法。

Cmake:defining link directories and libraries for different build targets

Linkingdifferent libraries for debug and release builds in cmake on windows?

使用target_link_libraries。

Target_link_libraries的格式:

Target_link_libraries(<target> [item1 [item2 […]]]
[[debug|optimized|general] <item>] …)

>A “debug”, “optimized”, or “general” keyword indicatesthat the library immediately following it is to be used only for the correspondingbuild configuration. The “debug” keyword corresponds to the Debug configuration(or to configurations named in the DEBUG_CONFIGURATIONS global property if itis set). The “optimized” keyword corresponds to all other configurations. The “general”keyword corresponds to all configurations, and is purely optional (assumed ifomitted).

当设置debug,则此命令是针对debug的build configuration的;当设置optimized,则此命令是针对除debug外的build configuration的;当设置general,则此命令是针对所有的build configuration的,而这个也是默认的。

b)        设置include路径

没有直接的设置方法。

6.      如何配置安装?

使用install命令。

7.      各平台链接库的区别?

#Q: *.a与*.so的区别是什么?

#A: 目标文件分类:


Import libraries举个例子就是在windows上面使用msvc生成shared dll时生成的lib库,由cygwin或mingw生成的.dll.a就相当于这里的lib。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值