[cmake]cmake获取源文件属性命令get_source_file_property简介

可以使用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
  • 11
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FL1768317420

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值