Cmake“无法指定该项目未构建的目标project_name的来源。”

发布于 2025-02-10 13:14:55 字数 2033 浏览 2 评论 0原文

我正在学习cmake,但不幸的是,大多数示例要么太简单,太复杂,要么文件夹结构与我的设计不同。

我遇到了一个错误,但首先我会解释文件夹结构(请进行批判):

MyProject
    bin
    build
    src
        ComponentA
            ObjectA.cpp
            CMakeLists.txt
        ComponentB
            ObjectB.cpp
            CMakeLists.txt
        CMakeLists.txt
        main.cpp
    CMakeLists.txt

我希望能够使用其绝对路径包含一些文件,例如main.cpp可能看起来像这样:

#include <ComponentB/ObjectB.h>

int main()
{
    ComponentB cb(1, 2, 3);
}

但是,在一个源文件中,我想使用相对路径包含其标题:(

#include <ComponentB.h>

ComponentB::ComponentB(int a, int b, int c) : _ca(a, b){}

如果这导致问题,我可以使用绝对路径)

我的cmakelists文件看起来像:

myproject/cmakelists.txt

cmake_minimum_required(VERSION 3.9.1)
project(MyProject)
add_subdirectory(src)

myProject/src/cmakelists.txt

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_ROOT_DIR}/bin)
add_executable(MyProject main.cpp)
add_subdirectory(ComponentA)
add_subdirectory(ComponentB)

myproject/src/componenta/cmakelists.txt

target_sources(MyProject PUBLIC ComponentA.cpp)

myproject> myproject/src/componentsb/cmakelists.txt.txt

target_sources(MyProject PUBLIC ComponentB.cpp)
target_include_directories(MyProject PUBLIC ComponentA) 

但是当我这样做时:

cd build
cmake ..

我会得到这个错误:

CMake Error at src/ComponentA/CMakeLists.txt:2 (target_sources):
  Cannot specify sources for target "MyProject" which is not built by
  this project.

CMake Error at src/ComponentB/CMakeLists.txt:2 (target_sources):
  Cannot specify sources for target "MyProject" which is not built by
  this project.

CMake Error at src/ComponentB/CMakeLists.txt:3 (target_include_directories):
  Cannot specify include directories for target "MyProject" which is not
  built by this project.

I am learning CMake but unfortunately most examples are either too simple, too complicated or the folder structure is different to what I'm designing.

I am getting an error but first I will explain the folder structure (please do critique):

MyProject
    bin
    build
    src
        ComponentA
            ObjectA.cpp
            CMakeLists.txt
        ComponentB
            ObjectB.cpp
            CMakeLists.txt
        CMakeLists.txt
        main.cpp
    CMakeLists.txt

I would like to be able to include some files using their absolute path, for example main.cpp might look like this:

#include <ComponentB/ObjectB.h>

int main()
{
    ComponentB cb(1, 2, 3);
}

but within a source file I'd like to include it's header using the relative path:

#include <ComponentB.h>

ComponentB::ComponentB(int a, int b, int c) : _ca(a, b){}

(if this causes problems I can include using absolute paths)

My CMakeLists files look like:

MyProject/CMakeLists.txt:

cmake_minimum_required(VERSION 3.9.1)
project(MyProject)
add_subdirectory(src)

MyProject/src/CMakeLists.txt:

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_ROOT_DIR}/bin)
add_executable(MyProject main.cpp)
add_subdirectory(ComponentA)
add_subdirectory(ComponentB)

MyProject/src/ComponentA/CMakeLists.txt:

target_sources(MyProject PUBLIC ComponentA.cpp)

MyProject/src/ComponentB/CMakeLists.txt:

target_sources(MyProject PUBLIC ComponentB.cpp)
target_include_directories(MyProject PUBLIC ComponentA) 

However when I do:

cd build
cmake ..

I get this this error:

CMake Error at src/ComponentA/CMakeLists.txt:2 (target_sources):
  Cannot specify sources for target "MyProject" which is not built by
  this project.

CMake Error at src/ComponentB/CMakeLists.txt:2 (target_sources):
  Cannot specify sources for target "MyProject" which is not built by
  this project.

CMake Error at src/ComponentB/CMakeLists.txt:3 (target_include_directories):
  Cannot specify include directories for target "MyProject" which is not
  built by this project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2025-02-17 13:14:55

target_sources通过在CMAKE 3.13中添加了不同目录中目标的相对路径添加源文件。由于您指定了3.9.1的最低版本,因此这根本不起作用。

有问题的策略是CMP0076,,如下所述,如下所述。

版本3.13中的新事物。

target_sources()命令将相对路径转换为绝对。

在cmake 3.13及以上,target_sources()命令现在将相对源文件路径转换为以下情况:

  • 源文件被添加到目标的interface_sources属性。
  • 目标的source_dir属性与cmake_current_source_dir
  • 不同

这是此处适用的第二个项目符号点。

您真的应该使用较新版本的Cmake。 除了最新版本(3.23)之后的几个版本(3.23)之后的任何东西都是普通的受虐狂。

The ability for target_sources to add source files via relative path to a target in a different directory was added in CMake 3.13. Since you have specified a minimum version of 3.9.1, this will simply not work.

The policy in question is CMP0076, which is documented as follows:

New in version 3.13.

The target_sources() command converts relative paths to absolute.

In CMake 3.13 and above, the target_sources() command now converts relative source file paths to absolute paths in the following cases:

  • Source files are added to the target's INTERFACE_SOURCES property.
  • The target's SOURCE_DIR property differs from CMAKE_CURRENT_SOURCE_DIR.

It is the second bullet point that applies here.

You really ought to use a newer version of CMake. Anything more than a few versions behind the newest (3.23) is plain masochism.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文