可以使用get_source_file_property命令获取指定源文件的属性,源文件属性默认是从当前目录属性中获取。
命令格式
get_source_file_property(<variable> <file>
[DIRECTORY <dir> | TARGET_DIRECTORY <target>]
<property>)
- <variable>:必选参数,源文件属性的存储结果
- <file>:必选参数,源文件名称
- DIRECTORY <dir>:可选参数,从指定的<dir>目录获取属性,前提是该目录是已经通过add_subdirectory()添加或者是CMake构建的顶层目录。如果是相对路径,则是针对当前目录的相对路径。
- TARGET_DIRECTORY <target>:可选参数,从构建目标<target>被创建的目录中获取属性,前提是<target>必须已经存在(通过add_executable或add_libraray创建)
- <property>:必选参数,属性名称
简单示例
下面展示获取源文件LOCATION属性的示例,该属性表示源文件的全路径。更多的源文件属性请参考这里。
假设接下来的目录结构为:
get_source_file_property/
├── CMakeLists.txt
├── main.cpp
└── test
├── CMakeLists.txt
└── test.cpp
其中get_source_file_property/test/test.cpp提供了一个test_print()接口,并被编译成test库,供get_source_file_property/main.cpp调用。
get_source_file_property/test/test.cpp文件内容:
#include <iostream>
void test_print()
{
std::cout << "In test: say hello!" << std::endl;
}
get_source_file_property/main.cpp文件内容:
extern void test_print();
int main(int argc, char** argv)
{
test_print();
return 0;
}
-
不指定目录(使用默认目录)
get_source_file_property/CMakeLists.txt文件内容:
cmake_minimum_required(VERSION 3.22.1) project(test) get_source_file_property(file_full_path test.cpp LOCATION) message("# Get source file property LOCATION: ${file_full_path}")
get_source_file_property/test/CMakeLists.txt文件内容:
get_source_file_property(file_full_path test.cpp LOCATION) message("# In subdirectory test, Get source file property LOCATION: ${file_full_path}")
运行
cmake .
,输出如下:# In subdirectory test, Get source file property LOCATION: /XXX/get_source_file_property/test/test.cpp # Get source file property LOCATION: /XXX/get_source_file_property/test.cpp
说明,即使是相同的文件,在不同的目录下,使用默认的目录就是源文件当前所在的目录。
-
指定目录
get_source_file_property/CMakeLists.txt文件内容(指定获取test目录):
cmake_minimum_required(VERSION 3.22.1) project(test) add_subdirectory(test) get_source_file_property(file_full_path main.cpp LOCATION) message("# Get source file property LOCATION: ${file_full_path}") get_source_file_property(file_full_path test/test.cpp DIRECTORY test LOCATION) message("# Get source file test/test.cpp from directory test property LOCATION: ${file_full_path}")
运行
cmake .
结果为:# In subdirectory test, Get source file property LOCATION: /XXX/get_source_file_property/test/test.cpp # Get source file from directory test property LOCATION: /XXX/get_source_file_property/test.cpp # Get source file test/test.cpp from directory test property LOCATION: /XXX/get_source_file_property/test/test.cpp
-
指定构建目标所在目录
get_source_file_property/CMakeLists.txt文件内容(指定获取test库所在的目录):
cmake_minimum_required(VERSION 3.22.1) project(test) add_subdirectory(test) get_source_file_property(file_full_path main.cpp LOCATION) message("# Get source file property LOCATION: ${file_full_path}") get_source_file_property(file_full_path test/test.cpp DIRECTORY test LOCATION) message("# Get source file test/test.cpp from directory test property LOCATION: ${file_full_path}") get_source_file_property(file_full_path test/test.cpp TARGET_DIRECTORY test LOCATION) message("# Get source file test/test.cpp from target test property LOCATION: ${file_full_path}")
get_source_file_property/test/CMakeLists.txt文件内容(指定获取test库所在的目录):
get_source_file_property(file_full_path test.cpp LOCATION) message("# In subdirectory test, Get source file property LOCATION: ${file_full_path}") add_library(test test.cpp)
运行
cmake .
结果为:# In subdirectory test, Get source file property LOCATION: /XXX/get_source_file_property/test/test.cpp # Get source file from directory test property LOCATION: /XXX/get_source_file_property/test.cpp # Get source file test/test.cpp from directory test property LOCATION: /XXX/get_source_file_property/test/test.cpp # Get source file test/test.cpp from target test property LOCATION: /XXX/get_source_file_property/test/test.cpp