gpt4 book ai didi

CMake pybind11 无法创建目标,因为另一个同名目标已经存在,如何绕过?

转载 作者:行者123 更新时间:2023-12-04 12:59:09 30 4
gpt4 key购买 nike

我有成功运行的代码。它的 CmakeLists.txt 是:
cmake_minimum_required(VERSION 3.15)
project(rotapro3)
set(CMAKE_CXX_STANDARD 14)
add_executable(rotapro3 main.cpp)
:

我想在这个项目中使用 pybind,并按照说明添加以下几行:

add_subdirectory(pybind)
pybind11_add_module(rotapro3 main.cpp)

它成功启动,但我收到一个错误:
 add_executable cannot create target "rotapro3" because another target with
the same name already exists. The existing target is a module library
created in source directory "C:/Users/Alex/Dropbox/rotapro3".

我对 CMake 几乎一无所知。我如何重写这些行以允许我使用 add_executable ?

更新:

我还有一个更复杂的案例:
    set(SOURCE_FILES
unit_test/geometry/monomer_test.cpp
unit_test/geometry/monomer_test.hpp
unit_test/geometry/polymer_test.cpp
unit_test/geometry/polymer_test.hpp
unit_test/geometry/unit_box_test.cpp
unit_test/geometry/unit_box_test.hpp
unit_test/geometry/rect_shape_3d_test.cpp
unit_test/geometry/rect_shape_3d_test.hpp
src/general/guard.cpp
src/general/guard.hpp
src/general/fmt_enum.hpp
src/general/string_format.cpp
src/general/string_format.hpp
src/geometry/monomer.cpp
src/geometry/monomer.hpp
src/geometry/polymer.cpp
src/geometry/polymer.hpp
src/geometry/unit_box.cpp
src/geometry/unit_box.hpp
src/geometry/rect_shape_3d.cpp
src/geometry/rect_shape_3d.hpp
)
include_directories(src/general)
include_directories(src/geometry)
include_directories(unit_test/general)
include_directories(unit_test/geometry)

add_executable(
grapoli_lap ${SOURCE_FILES}
unit_test/general/string_format_test.cpp
unit_test/general/string_format_test.hpp
unit_test/geometry/monomer_test.cpp
unit_test/geometry/monomer_test.hpp
unit_test/geometry/polymer_test.cpp
unit_test/geometry/polymer_test.hpp
unit_test/geometry/unit_box_test.cpp
unit_test/geometry/unit_box_test.hpp
unit_test/geometry/rect_shape_3d_test.cpp
unit_test/geometry/rect_shape_3d_test.cpp
)

add_subdirectory(pybind11)
pybind11_add_module(grapoli_lap grapoli_lib.cpp)

target_link_libraries(grapoli_lap gtest gtest_main)

我收到同样的错误。

最佳答案

在 CMake 中,不能有两个同名的目标。因为 pybind11_add_module() 类似于 add_library() ,您应该使用此命令来创建库目标。你可以命名这个库目标 rotapro3 .然后,您可以创建可执行目标,命名为其他名称(如 rotapro3_exe ):

cmake_minimum_required(VERSION 3.15)
project(rotapro3)
set(CMAKE_CXX_STANDARD 14)

add_subdirectory(pybind)
# Create the library rotapro3 target here.
pybind11_add_module(rotapro3 example.cpp)

# Create your executable target (with a different name).
add_executable(rotapro3_exe main.cpp)

关于CMake pybind11 无法创建目标,因为另一个同名目标已经存在,如何绕过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61413576/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com