使用CMake [duplicate]同时包含ompl和assimp包时出现未定义的引用错误

wko9yo5t  于 2023-03-18  发布在  其他
关注(0)|答案(1)|浏览(159)

此问题在此处已有答案

(39个答案)
3天前关闭。
当我尝试使用CMake编译我的项目时,我得到了以下错误(我使用的是Ubuntu 22.04.2 LTS):

Consolidate compiler generated dependencies of target AssimpTest
[ 50%] Building CXX object CMakeFiles/AssimpTest.dir/testArea/AssimpTest.cpp.o
[100%] Linking CXX executable AssimpTest
/usr/bin/ld: CMakeFiles/AssimpTest.dir/testArea/AssimpTest.cpp.o: in function `main':
AssimpTest.cpp:(.text+0x24): undefined reference to `Assimp::Importer::Importer()'
/usr/bin/ld: AssimpTest.cpp:(.text+0x3f): undefined reference to `Assimp::Importer::ReadFile(char const*, unsigned int)'
/usr/bin/ld: AssimpTest.cpp:(.text+0x94): undefined reference to `Assimp::Importer::~Importer()'
/usr/bin/ld: AssimpTest.cpp:(.text+0xba): undefined reference to `Assimp::Importer::~Importer()'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/AssimpTest.dir/build.make:97: AssimpTest] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/AssimpTest.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2

这是我正在尝试编译的代码:

#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <iostream>

int main()
{
    Assimp::Importer importer;

    const aiScene* scene = importer.ReadFile("multiplate-f1-clutch_assemble.dae",
    aiProcess_Triangulate | aiProcess_FlipUVs);

    if (nullptr != scene)
    {
        // handle error
        std::cout << "ERROR: model could not be loaded.";
        return 1;
    }

    // use the loaded model data here
    std::cout << "model successfully loaded \n";

    return 0;
}

我的CMakeLists.txt看起来像这样:

cmake_minimum_required(VERSION 3.10)
project(Project_Name)

find_package(
    ompl REQUIRED
    assimp REQUIRED
)

include_directories(
    ${EIGEN_INCLUDE_DIR}
    ${OMPL_INCLUDE_DIRS}
    ${ASSIMP_INLCUDE_DIR}
)

add_executable(AssimpTest testArea/AssimpTest.cpp)
target_link_libraries(AssimpTest ${ASSIMP_LIBRARIES})

当我从find_package中删除ompl REQUIRED行时,代码编译没有问题。2但是我需要使用ompl包以及为我的项目。3我猜这与ompl使用assimp包本身有关。4虽然代码不编译时,只有包括ompl和排除assimp从find_package。5我需要在我的CMakeLists做什么改变。txt才能同时使用ompl和assimp?
谢谢你的回答。

zvokhttg

zvokhttg1#

find_package(
    ompl REQUIRED
    assimp REQUIRED 
)

我不相信你可以在一次find_package()调用中列出两个包,我从来没有见过,从我对documentation的阅读来看,我不相信它是受支持的。

find_package(<PackageName> [version] [EXACT] [QUIET]
             [REQUIRED] [[COMPONENTS] [components...]]
             [OPTIONAL_COMPONENTS components...]
             [CONFIG|NO_MODULE]
             [GLOBAL]
             [NO_POLICY_SCOPE]
             [BYPASS_PROVIDER]
             [NAMES name1 [name2 ...]]
             [CONFIGS config1 [config2 ...]]
             [HINTS path1 [path2 ... ]]
             [PATHS path1 [path2 ... ]]
             [REGISTRY_VIEW  (64|32|64_32|32_64|HOST|TARGET|BOTH)]
             [PATH_SUFFIXES suffix1 [suffix2 ...]]
             [NO_DEFAULT_PATH]
             [NO_PACKAGE_ROOT_PATH]
             [NO_CMAKE_PATH]
             [NO_CMAKE_ENVIRONMENT_PATH]
             [NO_SYSTEM_ENVIRONMENT_PATH]
             [NO_CMAKE_PACKAGE_REGISTRY]
             [NO_CMAKE_BUILDS_PATH] # Deprecated; does nothing.
             [NO_CMAKE_SYSTEM_PATH]
             [NO_CMAKE_INSTALL_PREFIX]
             [NO_CMAKE_SYSTEM_PACKAGE_REGISTRY]
             [CMAKE_FIND_ROOT_PATH_BOTH |
              ONLY_CMAKE_FIND_ROOT_PATH |
              NO_CMAKE_FIND_ROOT_PATH])

从上面我没有看到允许在参数中超过1次。因此,我相信你的问题是${ASSIMP_LIBRARIES}是一个空变量,导致没有任何东西被链接到assimp。
相反,您应该有两个单独的行:

find_package(ompl REQUIRED)
find_package(assimp REQUIRED)

相关问题