[cmake命令笔记]target_compile_options

编译basalt时碰到这个命令,basalt使用的第三方库pangolin老是报错:

CMake Error at thirdparty/CMakeLists.txt:67 (target_compile_options):
  Cannot specify compile options for target "pangolin" which is not built by
  this project.


CMake Error at thirdparty/CMakeLists.txt:118 (get_target_property):
  get_target_property() called with non-existent target "pangolin".


CMake Error at thirdparty/CMakeLists.txt:134 (get_target_property):
  get_target_property() called with non-existent target "pangolin".


CMake Error at thirdparty/CMakeLists.txt:142 (set_target_properties):
  set_target_properties Can not find target to add properties to: pangolin

但是这和这个命令并没有什么关系,这个错误貌似是因为我在下载第三方库的时候,不是使用的git clone --recursive,而是手动下载的第三方库到thirdparty目录下,因此可能没有感知到pangolin的存在,然后我在basalt项目根目录下使用git submodule init和git submodule update,问题愉快地解决了。

这里记录target_compile_options命令的用法

cmake原始文档地址
这个命令是为某个需要编译的目标增加编译选项,比如在前面添加的add_executable()或者add_library(),这两个命令表示你要编译一个可执行文件或者一个库,这就是编译目标(target)。
命令的参数选项如下:

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

target_compile_options会增加选项(options)到COMPILE_OPTIONS或者INTERFACE_COMPILE_OPOTIONS目标属性(也就是增加编译选项的意思)。这些选项会在编译给定目标(<target>)时使用,给定目标必须已经通过add_executable()或者add_library()这种命令添加到项目中,并且不能是别名目标。

下面看可选参数

如果指定了BEFORE,这个命令中的选项将放到所有属性之前(prepended)而不是之后(appended)。
INTERFACE, PUBLIC和PRIVATE关键词用于指定随后的参数的作用域。
PRIVATE和PUBLIC项将填充<target>COMPILE_OPTIONS属性。
PUBLIC和INTERFACE项将填充<target>INTERFACE_COMPILE_OPTIONS属性。
随后的参数就是指定的编译选项。
编译选项就是CMAKE调用编译器时的编译参数,每个编译器都有一些编译参数,有的是共通的,有的是编译器相关的,其实我认为所有的构建工具,编译工具,最终都是为了得到一个编译器命令行,这个命令行的执行命令是编译器的编译程序,命令参数就是这些编译参数。使用VisualStudio的同学可以查看属性配置-C/C+±命令行,里面就是VS的编译参数。

猜你喜欢

转载自blog.csdn.net/u013238941/article/details/124966326