CMake:设置目标源的目录 [英] CMake: set directory for target sources

查看:135
本文介绍了CMake:设置目标源的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++项目,其中所有实现源文件(* .cpp)都位于项目目录内的 src 目录中。其中一些文件位于其他子目录中。假设 src / foo / 中有50个文件。我需要将这些文件作为 add_library 和/或 target_sources 函数的一部分列出。

I have a C++ project where all implementation source files (*.cpp) reside in a src directory within the project directory. Some of the files are in further subdirectories. Let's say there are 50 files in src/foo/. I need to list these files as part of the add_library and/or the target_sources function.

现在,不建议在任何地方使用,自动添加目录中的所有文件,这对我来说很好。因此,我将手动列出所有文件;但是将通用前缀 src / foo / 重复50次似乎很愚蠢而且令人讨厌。

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.

说明target_sources的文档


相对源文件路径被解释为相对于当前源目录(即CMAKE_CURRENT_SOURCE_DIR)。

Relative source file paths are interpreted as being relative to the current source directory (i.e. CMAKE_CURRENT_SOURCE_DIR).

所以我添加了 set(CMAKE_CURRENT_SOURCE_DIR quot ; src / foo /)调用 target_source 之前,但是没有用。 (我收到找不到源文件错误。)

So I've added set(CMAKE_CURRENT_SOURCE_DIR "src/foo/") before the call to target_source but it didn't work. (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:项目的(公共)头文件(* .hpp)位于 include 目录中(在 src 之外)。可以使用 target_include_directories 函数很好地配置(不必列出单个文件)。

N.B.: The (public) header files (*.hpp) of the project are in an include directory (outside of src). This is nicely configured (without having to list the individual files) with the target_include_directories function.

推荐答案


,但重复通用前缀src / foo / 50次

but repeating the common prefix src/foo/ 50 times

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

这篇关于CMake:设置目标源的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆