使用 CLion 在 Mac M1 上的 Cmake 项目中添加 assimp
Add assimp in Cmake project on Mac M1 with CLion
一周以来,我尝试将 assimp 库包含到我的 OpenGL 项目中,但没有成功。我使用 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
这是我的 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})
为了包含 assimp 库,我在 github 页面(版本 5.0.1)上下载了 zip 文件,将其解压缩到我项目的 libs/thirdparty 文件夹中,启动 cmake .
命令和 make
一个。我不是很熟悉cmake,所以可能是我在编译过程中出错了。
我看到这个库有一个可以通过 Homebrew 获得的版本,但我无法让它工作。
有没有人知道如何解决这个问题?
提前致谢
吕克萨斯
如果您通过 add_subdirectory
将 Assimp 项目包含到您的 CMakeLists.txt
中,那么它会创建 assimp
目标,其中包含有关 assimp 的所有知识。
所以,它的用法很简单:
# That line handles both library linking and assimp include directories.
target_link_libraries(opengl_tuto assimp)
或者,您可以使用 ALIAS 目标而不是“正常”assimp 目标 assimp::assimp
:
target_link_libraries(opengl_tuto assimp::assimp)
使用 ALIAS 目标有以下优点:
- CMake 自动检查 target
assimp::assimp
是否存在,如果不存在则发出错误。 (如果你 link 与 assimp
目标和给定的目标不存在,那么 CMake 只是将 -lassimp
选项传递给 linker。很可能这个选项导致 link失败。)
- 您是否会将您的项目转换为使用
find_package(assimp)
而不是 add_subdirectory
方法,linkage 保持不变。 (使用 find_package
方法 assimp
未创建目标)。
一周以来,我尝试将 assimp 库包含到我的 OpenGL 项目中,但没有成功。我使用 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
这是我的 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})
为了包含 assimp 库,我在 github 页面(版本 5.0.1)上下载了 zip 文件,将其解压缩到我项目的 libs/thirdparty 文件夹中,启动 cmake .
命令和 make
一个。我不是很熟悉cmake,所以可能是我在编译过程中出错了。
我看到这个库有一个可以通过 Homebrew 获得的版本,但我无法让它工作。
有没有人知道如何解决这个问题?
提前致谢 吕克萨斯
如果您通过 add_subdirectory
将 Assimp 项目包含到您的 CMakeLists.txt
中,那么它会创建 assimp
目标,其中包含有关 assimp 的所有知识。
所以,它的用法很简单:
# That line handles both library linking and assimp include directories.
target_link_libraries(opengl_tuto assimp)
或者,您可以使用 ALIAS 目标而不是“正常”assimp 目标 assimp::assimp
:
target_link_libraries(opengl_tuto assimp::assimp)
使用 ALIAS 目标有以下优点:
- CMake 自动检查 target
assimp::assimp
是否存在,如果不存在则发出错误。 (如果你 link 与assimp
目标和给定的目标不存在,那么 CMake 只是将-lassimp
选项传递给 linker。很可能这个选项导致 link失败。) - 您是否会将您的项目转换为使用
find_package(assimp)
而不是add_subdirectory
方法,linkage 保持不变。 (使用find_package
方法assimp
未创建目标)。