Assimp with cmake

Szczepan picture Szczepan · Jan 5, 2015 · Viewed 6.9k times · Source

I want to include assimp to my project using CMake. I have Ubuntu 14.04 LTE and QTCreator. Project contain of main.cpp and linked libraries stored in libs directory.

Main CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(test)  

find_package(OpenGL REQUIRED)

# libs contain external libaries
add_subdirectory (libs)

include_directories(
    libs/glfw-3.0.4/include
    libs/assimp-3.1.1/include/
)

set(allLibs
    ${GLFW_LIBRARIES}
    ${OPENGL_LIBRARY}
    GLEW_LIB
    assimp
)

add_executable(test
    main/main.cpp 
)

target_link_libraries(Manipulator glfw assimp ${allLibs} )

CMakeList.txt inside libs directory

### GLFW ###

add_subdirectory (glfw-3.0.4)

include_directories(
    glfw-3.0.4/include/GLFW/
    glew-1.11.0/include/
    assimp-3.1.1/include/
)


set(OPENGL_LIBRARY
    -lGL -lGLU -lXrandr -lXext -lX11 -lrt
    ${CMAKE_DL_LIBS}
    ${GLFW_LIBRARIES}
)

### GLEW ###

set(GLEW_SOURCE
    glew-1.11.0/src/glew.c
)

add_library( GLEW_LIB STATIC
    ${GLEW_SOURCE}
)

target_link_libraries(GLEW_LIB
    ${OPENGL_LIBRARY}
)


### ASSIMP ###

# Zlib
add_subdirectory( assimp-3.1.1/contrib/zlib )

# Boost workaround
include_directories( assimp-3.1.1/code/BoostWorkaround )
add_definitions( -DASSIMP_BUILD_BOOST_WORKAROUND )

# Compile AssImp
add_subdirectory( assimp-3.1.1/code )

And I receive following error.

CMake Error at libs/assimp-3.1.1/code/CMakeLists.txt:725 (INSTALL): install TARGETS given no ARCHIVE DESTINATION for static library target "assimp".

This point's me to this

INSTALL( TARGETS assimp # 725 line
         LIBRARY DESTINATION ${ASSIMP_LIB_INSTALL_DIR}
         ARCHIVE DESTINATION ${ASSIMP_LIB_INSTALL_DIR}
         RUNTIME DESTINATION ${ASSIMP_BIN_INSTALL_DIR}
         COMPONENT ${LIBASSIMP_COMPONENT})

How to correctly link this library?

Answer

Szczepan picture Szczepan · Aug 27, 2015

This is a CMakeList.txt that I use to build my project. All external libraries were stored in libs directory. Main file was stored in main directory.

cmake_minimum_required (VERSION 2.6)
project (Project name)

find_package(OpenGL REQUIRED)

#Adding external CMakeList.txt files
add_subdirectory(libs/glfw-3.1)

#add GLEW as STATIC library
add_library(GLEW STATIC libs/glew-1.11.0/src/glew.c libs/glew-1.11.0/include/GL/glew.h)

#add include directories
include_directories(
                    ${CMAKE_CURRENT_SOURCE_DIR}
                    common/
                    libs/glfw-3.1/include/
                    libs/glew-1.11.0/include/
                    libs/glm/
                    libs/assimp-3.1.1/include/  
                    libs/CImg_162/CImg.h                
                    )
# set variables that are needed
set(ZLIB_LIBRARIES zlibstatic)
set(ENABLE_BOOST_WORKAROUND ON)
set(BUILD_STATIC_LIB ON)
set(BUILD_ASSIMP_TOOLS  ON)
set(ASSIMP_BUILD_STATIC_LIB ON)

add_subdirectory(libs/assimp-3.1.1)

#Add my own liblary
add_subdirectory(common)

set(extLibs
    ${extLibs}
    ${GLFW_LIBRARIES}
    )


add_executable (openGL main/main.cpp
                                Source/vertexShader.vs
                                Source/fragmentShader.fs
                )

target_link_libraries(openGL common assimp GLEW glfw ${GLFW_LIBRARIES})

CMakelist.txt from Common looks like this:

add_library(common model.cpp)

This should work on Ubuntu 14.04. If some libraries are missing try to follow steps from this tutorial tutorial