[英]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 says在target_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.