Search code examples
c++cmakeassimp

What is the best way to add assimp to a CMake project?


I'm trying to build assimp as a static library to link it with my executable in a CMakeFile.txt file. The executable builds just fine but problem with this is that it takes a long time to link every time I try to build the project.

Here is my CMakeFile.txt:

cmake_minimum_required(VERSION 3.22)
project(3d_renderer)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ./out)

# glfw
set(GLFW_BUILD_DOCS OFF CACHE BOOL "GLFW build documentation" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "GLFW build documentation" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "GLFW build documentation" FORCE)

add_subdirectory(libs/glfw)

# assimp
set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries" FORCE)
set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "Assimp build tests" FORCE)
set(ASSIMP_INSTALL OFF CACHE BOOL "Assimp install" FORCE)
set(ASSIMP_INSTALL_PDB OFF CACHE BOOL "Assimp install PDB" FORCE)

add_subdirectory(libs/assimp)

include_directories(include/
                    libs/glad/include
                    libs/glfw/include
                    libs/glm
                    libs/imgui
                    libs/imgui/backends
                    C:/dev/libs/assimp/include
                    C:/dev/libs/assimp/build/include)

set(SOURCE_FILES src/main.cpp
                 src/shader.cpp
                 src/texture2d.cpp
                 src/resource_manager.cpp
                 src/orbit_camera.cpp
                 libs/glad/src/glad.c
                 libs/imgui/imgui.cpp
                 libs/imgui/imgui_demo.cpp
                 libs/imgui/imgui_draw.cpp
                 libs/imgui/imgui_tables.cpp
                 libs/imgui/imgui_widgets.cpp
                 libs/imgui/backends/imgui_impl_glfw.cpp
                 libs/imgui/backends/imgui_impl_opengl3.cpp)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

target_link_libraries(${PROJECT_NAME} glfw ${GLFW_LIBRARIES})
target_link_libraries(${PROJECT_NAME} assimp)

add_custom_target(copy_resources ALL
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${PROJECT_SOURCE_DIR}/resources
        ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources
        COMMENT "Copying resources into build directory")

add_dependencies(${PROJECT_NAME} copy_resources)

My question is this: Is there a way to reduce the time it takes to link this static library, or would it better to compile assimp as a DLL?


Solution

  • I'm not sure whether linking a DLL or static library is faster - but as long as you don't make any changes to the external library, it won't need to rebuild and linking should be fast.

    Personally, the way I add assimp to my project is like this (using FetchContent, which will download the files from the repository when you run cmake, and then add the project). I build the project statically just fine.

    include(FetchContent)
    set(FETCHCONTENT_BASE_DIR ${PROJECT_SOURCE_DIR}/libs CACHE PATH "Missing description." FORCE)
    FetchContent_Declare(assimp
    GIT_REPOSITORY https://github.com/assimp/assimp.git
    GIT_TAG master)
    set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
    set(ASSIMP_BUILD_TESTS OFF CACHE BOOL "" FORCE)
    set(ASSIMP_INJECT_DEBUG_POSTFIX OFF CACHE BOOL "" FORCE)
    set(ASSIMP_INSTALL OFF CACHE BOOL "" FORCE)
    FetchContent_MakeAvailable(assimp)
    target_include_directories(3d_renderer PRIVATE libs/assimp-src/include)
    target_link_libraries(3d_renderer assimp)
    

    You can check what those ASSIMP settings do in the CMakeLists.txt file of that project. FetchContent will download the files from the repository and build the project when you run your cmake command.

    I would recommend to not add external / library code to your project as project source, but rather to add them via target_include_directories, so your code would be:

    target_include_directories(3d_renderer
                    include/
                    libs/glad/include
                    libs/glfw/include
                    libs/glm
                    libs/imgui
                    libs/assimp/include)
    set(SOURCE_FILES src/main.cpp
                 src/shader.cpp
                 src/texture2d.cpp
                 src/resource_manager.cpp
                 src/orbit_camera.cpp)
    

    Then in your code you just #include backends/imgui_impl_glfw.cpp