最近在编写 cmake 现在把if 命令遇到的问题总到这里
1、if(NOT TARGET XX)
官方文档
if(TARGET target-name)
True if the given name is an existing logical target name such as those created by the add_executable(), add_library(), or add_custom_target() commands.
判断 target-name 是否存在。 target-name 主要是通过 add_executable(), add_library(), or add_custom_target() 这3个命令执行输出的目标。
2、if(COMMAND command-name)
if(COMMAND command-name)
True if the given name is a command, macro or function that can be invoked.
判断 宏或者函数等可以调用
3、get_filename_component
官方文档:
get_filename_component
Get a specific component of a full filename.
get_filename_component(<VAR> <FileName> <COMP> [CACHE])
Set <VAR> to a component of <FileName>, where <COMP> is one of:
DIRECTORY = Directory without file name
NAME = File name without directory
EXT = File name longest extension (.b.c from d/a.b.c)
NAME_WE = File name without directory or longest extension
ABSOLUTE = Full path to file
REALPATH = Full path to existing file with symlinks resolved
PATH = Legacy alias for DIRECTORY (use for CMake <= 2.8.11)
Paths are returned with forward slashes and have no trailing slahes. The longest file extension is always considered. If the optional CACHE argument is specified, the result variable is added to the cache.
get_filename_component(<VAR> FileName
PROGRAM [PROGRAM_ARGS <ARG_VAR>]
[CACHE])
The program in FileName will be found in the system search path or left as a full path. If PROGRAM_ARGS is present with PROGRAM, then any command-line arguments present in the FileName string are split from the program name and stored in <ARG_VAR>. This is used to separate a program name from its arguments in a command line string.
获取组件 的信息存到
信息类型包含一下:
DIRECTORY = 不包含文件名的目录
NAME = 不包含目录的文件名
ABSOLUTE = 绝对路径,包含文件名
REALPATH = 软连接的文件的绝对路径
PATH = DIRECTORY 的旧名字。结果等同 DIRECTORY。在 cmake <=2.8.11 版本.
4、在find_package 中给目标添加属性
1、添加目标
add_library(obj STATIC IMPORTED)
- 首先这个 add_library 不会构建obj。
- 这个操作的目的就是导出 obj
2、给目标设置属性
使用set_target_properties 设置obj属性值。
官方文档
set_target_properties(target1 target2 ...
PROPERTIES prop1 value1
prop2 value2 ...)
target1 target2 … 是目标
PROPERTIES 设置属性关键字
prop1 存储属性的变量
value1 属性值。
例子
```bash
set_target_properties(obj PROPERTIES test "test___")
3、获取属性数据
1、get_property
官方文档
```bash
get_property(<variable>
<GLOBAL |
DIRECTORY [<dir>] |
TARGET <target> |
SOURCE <source>
[DIRECTORY <dir> | TARGET_DIRECTORY <target>] |
INSTALL <file> |
TEST <test> |
CACHE <entry> |
VARIABLE >
PROPERTY <name>
[SET | DEFINED | BRIEF_DOCS | FULL_DOCS])
需要指定参数
例子:
get_property(var TARGET obj PROPERTY test)
# 将会将 obj 的 test 属性值 存到变量 var 中
2、get_target_property
官方文档
get_target_property(<VAR> target property)
不需要指定参数,因为这个是针对的。
例子:
get_target_property(var obj test)
# 效果和 get_property(var TARGET obj PROPERTY test) 一样