美文网首页
CMake获取源文件属性命令get_source_file_pr

CMake获取源文件属性命令get_source_file_pr

作者: Domibaba | 来源:发表于2023-09-15 10:53 被阅读0次

可以使用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
    

相关文章

  • [原创] Cmake语法速览

    0 源文件 Cmake的源码文件,可以包含命令+注释+空格+换行。 以cmake编写的源文件以CMakeLists...

  • cmake-language 语言规范

    CMake在项目中的组织结构 CMake命令执行时的输入文件是在名为CMakeLists.txt源文件中以“CMa...

  • CMake实战一:单个源文件

    title: CMake实战一:单个源文件 categories:[实战一] tags:[CMake] date:...

  • iOS端安装包瘦身

    一、删除不再使用的资源 这里的资源文件主要指图片资源。获取的实现步骤如下: 获取资源文件(find命令) 设置资源...

  • cmake 常见命令

    cmake cmake_minimum_required命令语法:cmake_minimum_required(V...

  • CMake项目中集成gtest

    使用cmake的ExternalProject_Add相关命令,实现源码获取、编译、集成到本地项目中的动态集成,不...

  • 编译:CMAKE

    CMAKE 0、CMake CMake中,我们先输入cmake 命令对工程进行分析,生成makefile文件; 然...

  • Libde265Mac&iOS平台源码编译

    Mac平台下编译 1.获取源码 2.安装cmake命令行工具 3.cmake创建工程 -2,3两步具体步骤可参看我...

  • 反编译工具

    反编译工具使用 apktool 主要用于资源文件的获取命令:java -jar apktool_2.4.0....

  • vue多环境打包

    方法1 node属性process.argv 改属性为node执行命令获取到的命令集合 如打包命令为npm bui...

网友评论

      本文标题:CMake获取源文件属性命令get_source_file_pr

      本文链接:https://www.haomeiwen.com/subject/gwihvdtx.html