Search code examples
c++cmakeapple-m1assimp

Add assimp in Cmake project on Mac M1 with CLion


Since a week, I tried to include the assimp library into my OpenGL project without success. I use the Mac M1 and I get the following error

Undefined symbols for architecture arm64:
  "Assimp::Importer::ReadFile(char const*, unsigned int)", referenced from:
      Assimp::Importer::ReadFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned int) in Model.cpp.o
  "Assimp::Importer::Importer()", referenced from:
      Model::loadModel(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in Model.cpp.o
  "Assimp::Importer::~Importer()", referenced from:
      Model::loadModel(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in Model.cpp.o
  "Assimp::Importer::GetErrorString() const", referenced from:
      Model::loadModel(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in Model.cpp.o
  "_aiGetMaterialTexture", referenced from:
      aiMaterial::GetTexture(aiTextureType, unsigned int, aiString*, aiTextureMapping*, unsigned int*, float*, aiTextureOp*, aiTextureMapMode*) const in Model.cpp.o
  "_aiGetMaterialTextureCount", referenced from:
      aiMaterial::GetTextureCount(aiTextureType) const in Model.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [opengl_tuto] Error 1
make[2]: *** [CMakeFiles/opengl_tuto.dir/all] Error 2
make[1]: *** [CMakeFiles/opengl_tuto.dir/rule] Error 2
make: *** [opengl_tuto] Error 2

Here is my CmakeLists.txt :

cmake_minimum_required(VERSION 3.19)
project(opengl_tuto)

set(CMAKE_CXX_STANDARD 14)

add_executable(opengl_tuto src/main.cpp
        src/Shader.cpp src/Shader.h
        src/CubeTexture.cpp src/CubeTexture.h
        src/Camera.cpp src/Camera.h
        src/entity/Cube.cpp src/entity/Cube.h
        src/Mesh.cpp src/Mesh.h src/Model.cpp src/Model.h)

if(APPLE)
    set(OPENGL_MAC_DIR /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework/Versions/A/Headers/)
    target_include_directories(${PROJECT_NAME} PRIVATE ${OPENGL_MAC_DIR})
else()
    find_package(OpenGL REQUIRED)
    set(LIBS OpenGL)
    # Define the link libraries
    target_include_directories(${PROJECT_NAME} PUBLIC ${LIBS})
    target_link_libraries(${PROJECT_NAME} ${LIBS})
endif()
set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/libs/thirdparty")

#GLFW
set(GLFW_DIR ${LIB_DIR}/glfw)

set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")

add_subdirectory(${GLFW_DIR})
target_link_libraries(${PROJECT_NAME} glfw ${GLFW_LIBRARIES})
target_include_directories(${PROJECT_NAME} PRIVATE ${GLFW_DIR}/include)
include_directories(${GLFW_DIR}/include)


set(ASSIMP_DIR ${LIB_DIR}/assimp/)
add_subdirectory(${ASSIMP_DIR})
include_directories(${ASSIMP_DIR}/include)

#glad
set(GLAD_DIR ${LIB_DIR}/glad)
add_library("glad" ${GLAD_DIR}/src/glad.c src/entity/Cube.cpp src/entity/Cube.h)
target_include_directories(glad PRIVATE ${GLAD_DIR}/include)
target_include_directories(${PROJECT_NAME} PRIVATE ${GLAD_DIR}/include)
target_link_libraries(${PROJECT_NAME} "glad" "${CMAKE_DL_LIBS}")

set(STB_DIR ${LIB_DIR}/stb_image)
add_library(stb_image ${STB_DIR}/stb_image.cpp)
target_include_directories(stb_image PRIVATE ${STB_DIR})
target_include_directories(${PROJECT_NAME} PRIVATE ${STB_DIR})
include_directories(${STB_DIR})
target_link_libraries(${PROJECT_NAME} "stb_image" "${CMAKE_DL_LIBS}")

set(GLM_DIR ${LIB_DIR}/glm)
#add_package(glm ${GLM_DIR})
include_directories(${GLM_DIR})

To include the assimp library, I have downloaded the zip file on the github page (version 5.0.1), extract it on the libs/thirdparty folder of my project, launch the cmake . command and the make one. I'm not really familiar with cmake, so maybe I make a mistake during the compilation.

I saw that there is a version of this library available via Homebrew, but I was not able to make it work.

Did anyone have an idea of how to fix this issue ?

Thanks in advance Lyxas


Solution

  • If you include Assimp project into your CMakeLists.txt via add_subdirectory, then it creates assimp target, which incorporates all knowledge about assimp.

    So, its usage is simple:

    # That line handles both library linking and assimp include directories.
    target_link_libraries(opengl_tuto assimp)
    

    Alternatively, instead of "normal" assimp target you may use ALIAS target assimp::assimp:

    target_link_libraries(opengl_tuto assimp::assimp)
    

    Using ALIAS target has following advantages:

    1. CMake automatically checks existence of the target assimp::assimp and emits an error if it is not exist. (If you link with assimp target and given target doesn't exist, then CMake just passes -lassimp option to the linker. Most likely this option causes linker to fail.)
    2. Would you transform your project to use find_package(assimp) instead of add_subdirectory approach, the linkage remains the same. (With find_package approach assimp target is not created).