简体   繁体   English

使用 CLion 在 Mac M1 上的 Cmake 项目中添加 assimp

[英]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.一周以来,我尝试将 assimp 库包含到我的 OpenGL 项目中,但没有成功。 I use the Mac M1 and I get the following error我使用 Mac M1 并收到以下错误

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:这是我的 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.为了包含 assimp 库,我在 github 页面(版本 5.0.1)上下载了 zip 文件,将其解压缩到我的项目的 libs/thirdparty 文件夹中,启动cmake.命令和make一个。 I'm not really familiar with cmake, so maybe I make a mistake during the compilation.我对 cmake 不是很熟悉,所以可能是我在编译过程中出错了。

I saw that there is a version of this library available via Homebrew, but I was not able to make it work.我看到这个库的一个版本可以通过 Homebrew 获得,但我无法让它工作。

Did anyone have an idea of how to fix this issue?有没有人知道如何解决这个问题?

Thanks in advance Lyxas提前致谢

If you include Assimp project into your CMakeLists.txt via add_subdirectory , then it creates assimp target, which incorporates all knowledge about assimp.如果您通过add_subdirectory将 Assimp 项目包含到您的CMakeLists.txt中,那么它会创建assimp目标,其中包含有关 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 :或者,您可以使用 ALIAS target assimp::assimp代替“普通”assimp 目标:

target_link_libraries(opengl_tuto assimp::assimp)

Using ALIAS target has following advantages:使用 ALIAS 目标具有以下优点:

  1. CMake automatically checks existence of the target assimp::assimp and emits an error if it is not exist. CMake 自动检查目标assimp::assimp是否存在,如果不存在则发出错误。 (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.) (如果您链接assimp目标并且给定的目标不存在,那么 CMake 只会将-lassimp选项传递给 linker。此选项很可能导致 linker 失败。)
  2. Would you transform your project to use find_package(assimp) instead of add_subdirectory approach, the linkage remains the same.您是否会将项目转换为使用find_package(assimp)而不是add_subdirectory方法,链接保持不变。 (With find_package approach assimp target is not created). (使用find_package方法未创建assimp目标)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM