undefined reference error when including both ompl and assimp packages with CMake

218 views Asked by At

I get the following error when trying to compile my project using CMake (I am on 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

This is the code I am trying to compile:

#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;
}

My CMakeLists.txt looks like this:

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})

When I remove the ompl REQUIRED line from find_package, the code compiles without issues. However I need to use the ompl package as well for my project. I am guessing this has something to do with ompl using the assimp package itself. Though the code does not compile when only including ompl and excluding assimp from find_package. What change do I need to make in my CMakeLists.txt to be able to both use ompl and assimp ?

Thank you for your answers.

1

There are 1 answers

0
drescherjm On BEST ANSWER
find_package(
    ompl REQUIRED
    assimp REQUIRED 
) 

I don't believe you can list both packages on a single call to find_package() I have never seen that and from my reading of the documentation I don't believe it is supported.

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])

From the above I don't see allowed to be in the arguments more than 1 time. As a result I believe your issue is ${ASSIMP_LIBRARIES} is an empty variable causing nothing to be linked for assimp.

Instead you should have two separate lines:

find_package(ompl REQUIRED)
find_package(assimp REQUIRED)