简体   繁体   English

CMake:为目标源设置目录

[英]CMake: set directory for target sources

I have a C++ project where all implementation source files (*.cpp) reside in a src directory within the project directory.我有一个 C++ 项目,其中所有实现源文件 (*.cpp) 都位于项目目录中的src目录中。 Some of the files are in further subdirectories.一些文件位于更远的子目录中。 Let's say there are 50 files in src/foo/ .假设src/foo/ 中有 50 个文件。 I need to list these files as part of the add_library and/or the target_sources function.我需要将这些文件列为add_library和/或target_sources函数的一部分。

Now, everywhere one looks, adding all files from a directory automtically is discouraged, which is fine with me.现在,无论在哪里,都不鼓励从目录中自动添加所有文件,这对我来说很好。 So I am going to list all the files manually;所以我将手动列出所有文件; but repeating the common prefix src/foo/ 50 times seems really silly and is annoying.但是重复公共前缀src/foo/ 50 次似乎很愚蠢而且很烦人。

Inthe documentation for target_sources it saystarget_sources 的文档中,它说

Relative source file paths are interpreted as being relative to the current source directory (ie CMAKE_CURRENT_SOURCE_DIR).相对源文件路径被解释为相对于当前源目录(即 CMAKE_CURRENT_SOURCE_DIR)。

So I've added set(CMAKE_CURRENT_SOURCE_DIR "src/foo/") before the call to target_source but it didn't work.所以我在调用target_source之前添加了set(CMAKE_CURRENT_SOURCE_DIR "src/foo/")但它不起作用。 (I get a "Cannot find source file" error.) (我收到“找不到源文件”错误。)

So what is the correct way to achieve what I want if it is even possible?那么,如果可能的话,实现我想要的东西的正确方法是什么?

NB: The (public) header files (*.hpp) of the project are in an include directory (outside of src ).注意:项目的(公共)头文件 (*.hpp) 位于包含目录中(在src之外)。 This is nicely configured (without having to list the individual files) with the target_include_directories function.这是使用target_include_directories函数很好地配置(无需列出单个文件)。

but repeating the common prefix src/foo/ 50 times但重复公共前缀 src/foo/ 50 次

Just prepend the prefix to the sources.只需在来源前面加上前缀。

set(target_sources
    source1.c
    source2.c
)
list(TRANSFORM target_sources PREPEND "src/foo/")

Setting CMAKE_CURRENT_SOURCE_DIR as a relative variable to the old CMAKE_CURRENT_SOURCE_DIR is not going to work.CMAKE_CURRENT_SOURCE_DIR设置为旧CMAKE_CURRENT_SOURCE_DIR的相对变量是行不通的。 If it's set to a value, then it's fair to assume that removing that value and using a custom one will be wrong because you are missing what it was set to before.如果它被设置为一个值,那么可以假设删除该值并使用自定义值是错误的,因为您错过了它之前设置的值。

You might want to set it as an absolute path, or use set(CMAKE_CURRENT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/foo/") , or do as lots of other people and copy paste the prefix all the time.您可能希望将其设置为绝对路径,或使用set(CMAKE_CURRENT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/foo/") ,或者像其他人一样做并始终复制粘贴前缀。 That's the standard practice (which is also one of the reason I still use GLOB, because I'm lazy), following up on explicit is better than implicit (which is the point of not using GLOB ).这是标准做法(这也是我仍然使用 GLOB 的原因之一,因为我很懒),跟进显式优于隐式(这是不使用GLOB )。

You can also use add_subdirectory(src/foo) and a CMakeLists.txt in src/foo in which you only have to put您还可以在src/foo中使用add_subdirectory(src/foo)CMakeLists.txt ,您只需将

target_sources( <your target> PRIVATE|PUBLIC|INTERFACE
  your
  list
  of
  sources
)

I think that would be rather elegant.我认为那会相当优雅。

I would have suggested (if you have bash or some similar shell)我会建议(如果你有 bash 或一些类似的 shell)

function prepend() { while read line; do echo "${1}${line}"; done; }
ls *.cc *hh | sort | prepend  "\${CMAKE_CURRENT_SOURCE_DIR}/"

then copy and paste :)然后复制粘贴:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CMake:使用 target_sources() 添加当前目录和子目录中的所有文件 - CMake: add all files on current directory and subdirectories with target_sources() CMake 错误 - 目标 foo INTERFACE_SOURCES 属性包含以源目录为前缀的路径 - CMake error - Target foo INTERFACE_SOURCES property contains path which is prefixed in the source directory 当不包括带有“target_sources”的源时,CMake 没有警告 - No warning from CMake when not including sources with `target_sources` CMake 错误:请设置它们或确保它们在 CMake 文件中正确设置和测试:QUICKFIX_LIBRARY 由目录中的目标“运行”链接 - CMake Error: Please set them or make sure they are set and tested correctly in the CMake files: QUICKFIX_LIBRARY linked by target "run" in directory 将 GTest 与现有的 CMake 项目集成:共享相同的 target_sources - Integrating GTest with existing CMake Project: share the same target_sources CMake 将 CMakeLists.txt 中的 target_sources 拆分为不同的目标 - CMake split target_sources in CMakeLists.txt into different targets 在 CMake 中使用 target_sources() 添加源文件后删除它 - Remove a source file after adding it with target_sources() in CMake CMake 项目结构:何时使用 add_library/target_link_library 而不是 target_sources? - CMake Project structure: When to use add_library/target_link_library over target_sources? 如何在cmake中动态设置目标库? - How to set target library dynamically in cmake? 如何使用CMake设置MSVC目标平台版本? - How to set MSVC Target Platform Version with CMake?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM