How do I include the assimp library in the project via CMake? - c++

I followed the asssimp library's installation guide, which used Visual Studio and did all the necessary steps to include it in the project through the GUI when I did everything through pure CMake and MinGW.
I need to include headers and two library files in my project: assimp-[...].dll and assimp-[...].lib
My assimp proj hierarchy
I started to figure out how to write my own CMakeLists.txt and came up with this:
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
project(Assimp)
add_library(assimp SHARED IMPORTED GLOBAL)
file(GLOB_RECURSE ROOT_SOURCE "./assimp/*.cpp")
file(GLOB_RECURSE ROOT_INLINE "./assimp/*.inl")
file(GLOB_RECURSE ROOT_HPP "./assimp/*.hpp")
file(GLOB_RECURSE ROOT_HEADER "./assimp/*.h")
file(GLOB_RECURSE COMPILER_HEADER "./assimp/Compiler/*.h")
file(GLOB_RECURSE PORT_HEADER "./assimp/port/AndroidJNI/*.h")
source_group("Compiler Files" FILES ${COMPILER_HEADER})
source_group("Port Files" FILES ${PORT_HEADER})
include(GNUInstallDirs)
if(BUILD_STATIC_LIBS)
add_library(assimp_static STATIC
${ROOT_SOURCE} ${ROOT_INLINE} ${ROOT_HPP}
${ROOT_HEADER} ${COMPILER_HEADER} ${PORT_HEADER}
)
target_link_libraries(assimp_static PUBLIC assimp)
add_library(assimp::assimp_static ALIAS assimp_static)
endif()
if(BUILD_SHARED_LIBS)
add_library(assimp_shared SHARED
${ROOT_SOURCE} ${ROOT_INLINE} ${ROOT_HPP}
${ROOT_HEADER} ${COMPILER_HEADER} ${PORT_HEADER}
)
target_link_libraries(assimp_shared PUBLIC assimp)
add_library(assimp::assimp_shared ALIAS assimp_shared)
endif()
set_property(TARGET assimp PROPERTY IMPORTED_LOCATION "${PROJECT_SOURCE_DIR}/external/assimp/lib/assimp-vc143-mtd.dll")
set_property(TARGET assimp PROPERTY IMPORTED_IMPLIB "${PROJECT_SOURCE_DIR}/external/assimp/lib/assimp-vc143-mtd.lib")
After that I'm linking it in the main CMakeLists.txt:
add_executable(${PROJECT_NAME} [src code files here])
add_subdirectory(external/assimp)
target_link_libraries(${PROJECT_NAME} PRIVATE assimp)
But after that, I can't see any header from this library, as well as its directory, and I'm not even sure if the dynamic libraries from the "lib" folder are included correctly.
What am I doing wrong?

Related

Require shared library with dependencies in cmake

I'm new to cmake, so I'm having trouble figuring out how to model the following:
My program requires a third party library that I only have access to in the form of headers and *.so files. It also ships many of its dependencies as *.so files. How do I make sure that everything compiles and links correctly, ideally in the "correct" cmake way?
I've tried this
cmake_minimum_required (VERSION 3.8)
project ("Test")
add_executable (MyApp "MyApp.cpp")
link_directories("/path/to/lib")
target_include_directories(MyApp PUBLIC "/path/to/headers")
This compiles but fails at the linking stage.
Then I tried
cmake_minimum_required (VERSION 3.8)
project ("Test")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_executable (MyApp "MyApp.cpp")
find_package(Library REQUIRED)
target_link_libraries(MyApp PUBLIC Library::Library)
And cmake/FindLibrary.cmake following https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/ (truncated):
...
if(Library_FOUND AND NOT TARGET Library::Library)
add_library(Library::Library SHARED IMPORTED)
set_target_properties(Library::Library PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${Library_INCLUDE_DIR}"
)
set_target_properties(Library::Library PROPERTIES
IMPORTED_LOCATION "/path/to/library.so"
)
endif()
This compiles and links, but the required other *.so files are not found at runtime. I suppose I need to add them also as libraries somehow, although I don't want them to be exported in FindLibrary.cmake. How do I do this correctly?
You can use IMPORTED libraries:
add_library(externalLib SHARED IMPORTED GLOBAL)
set_property(TARGET externalLib PROPERTY IMPORTED_LOCATION "/path/to/lib.so")
target_include_directories(externalLib INTERFACE "/path/to/headers")
target_link_directories(externalLib INTERFACE /path/to/libs)
target_link_libraries(externalLib INTERFACE -ldep1 -ldep2)
Then you can just depend on it:
target_link_libraries(MyApp PRIVATE externalLib)

How To Include External Libraries in CMake Project

I am confused on how to statically include the source code of SDL2. I am trying to do this to make a library I am working on more portable.
When I was setting this up as executable the library was compiled with it fine, but when I changed it to a library it wouldn't include the library.
Currently, when I try to include my library in another project it says "Cannot open include file: 'SDL2/SDL.h': No such file or directory". So it leads me to think that the cause of the error is that the include directories aren't exported with the static library.
My Filesystem:
include
--Header Files
src
--Source Files
extern
--SDL2
build
Here is an example of the file causing the error:
#include <iostream>
#include <SDL.h> //Error
using namespace std;
/* The code */
Here is an example of my main CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
project(MyProject VERSION 1.0.0)
set(SDL2_SOURCE_DIR “${CMAKE_SOURCE_DIR}/extern/SDL2”)
add_subdirectory(extern/SDL2)
add_subdirectory(src)
Here is an example of my src CMakeLists.txt:
set(PROJECT_NAME MyProject)
file(GLOB HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB SOURCES "*.cpp")
add_library(${PROJECT_NAME} ${SOURCES} ${HEADER_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/include" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2/include")
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2main SDL2-static)
set_target_properties( ${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin"
)
From sdl2 CMakeLists.txt try:
set(SDL_STATIC 1)
add_subdirectory(extern/SDL2) # And I recommend EXCLUDE_FROM_ALL
Also to be sure add a check:
foreach(i IN ITEMS SDL2main SDL2-static)
if(NOT TARGET ${i})
message(FATAL_ERROR "${i} is not a target")
endif()
endif()
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2main SDL2-static)

How To Properly Include SDL2 Source Code in Project CMake

I am confused on how to statically include the source code of SDL2. I am trying to do this to make a library I am working on more portable.
Currently, when I try to include my library in another project it says "Cannot open include file: 'SDL2/SDL.h': No such file or directory".
My Filesystem:
include
--Header Files
src
--Source Files
extern
--SDL2
build
Here is an example of the file causing the error:
#include <iostream>
#include <SDL2/SDL.h> //Error
using namespace std;
/* The code */
Here is an example of my main CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
project(MyProject VERSION 1.0.0)
set(SDL2_SOURCE_DIR “${CMAKE_SOURCE_DIR}/extern/SDL2”)
add_subdirectory(extern/SDL2)
add_subdirectory(src)
Here is an example of my src CMakeLists.txt:
set(PROJECT_NAME MyProject)
file(GLOB HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB SOURCES "*.cpp")
add_library(${PROJECT_NAME} ${SOURCES} ${HEADER_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/include")
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2main SDL2-static)
set_target_properties( ${PROJECT_NAME}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin"
)
add_subdirectory() will process the subdirectory as a cmake porject (process the provided CMakeLists.txt for SDL). If you read that CMakeLists.txt, you'd see that it add the include directories relative to SDL2_SOURCE_DIR, which is empty by default.
What I would try is to set SDL2_SOURCE_DIR to ${CMAKE_CURRENT_SOURCE_DIR}/extern/SDL2 to make the path absolute. Like that SDL include dirs will be valid everywhere in your project.
You can also manually add the correct includes by using the include_directories() directive.

Cmake links only to executable

I have a problem that in my project I have multiple libraries who all link eachother differently.
And then there is one executable that links to only one library.
Now there are two problems:
1) The linked libraries are not showing up in the librarian of the static libraries.
2) the librarires are all showing up at the executable.
So here is my project order
Game|- Engine|--Core|---Graphics|----Libpng|---Platform
so Graphic links Libpng
Core links Graphics and Platform
Engine links Core
Game links Engine
Utilities is an interface library so headers only
what happens is that all Additional dependencies are at Game
The librarian of Core, Engine and Graphic containts no additional dependencies.
This is the Root CMake
cmake_minimum_required(VERSION 2.8.9)
set(CMAKE_GENERATOR "Visual Studio 14 2015")
project(EngineOnly)
set(CMAKE_SUPPRESS_REGENERATION true)
if(WIN32)
set(PLATFORM WIN32)
else()
set(PLATFORM LINUX)
endif (WIN32)
add_subdirectory(UTILITIES)
add_subdirectory(PLATFORM)
add_subdirectory(GRAPHICS)
add_subdirectory(CORE)
add_subdirectory(Engine)
add_subdirectory(Game)
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT Trinity.Game)
This is game
#UTILITIES LIB
include_directories(../UTILITIES/INCLUDE)
include_directories(../ENGINE/INCLUDE)
#SOURCE
file(GLOB SOURCES "SOURCE/*.cpp")
file(GLOB INCLUDES "INCLUDE/*.h")
#PROJECTNAME
set(PROJECTNAME "Trinity.Game")
#LINK ALL
add_executable(${PROJECTNAME} ${SOURCES} ${INCLUDES})
set_target_properties(${PROJECTNAME} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_BINARY_DIR}/../BIN/${PLATFORM}/DEBUG"
RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_BINARY_DIR}/../BIN/${PLATFORM}/RELEASE"
)
target_link_libraries(${PROJECTNAME}
LINK_PRIVATE Trinity.Engine
)
Engine
#UTILITIES LIB
include_directories(../UTILITIES/INCLUDE)
include_directories(../CORE/INCLUDE)
#SOURCE
file(GLOB SOURCES "SOURCE/*.cpp")
file(GLOB INCLUDES "INCLUDE/*.h")
#PROJECTNAME
set(PROJECTNAME "Trinity.Engine")
#LINK ALL
add_library(${PROJECTNAME} ${SOURCES} ${INCLUDES})
set_target_properties(${PROJECTNAME} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_BINARY_DIR}/LIB/${PLATFORM}/DEBUG"
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_BINARY_DIR}/LIB/${PLATFORM}/RELEASE"
)
target_link_libraries(${PROJECTNAME}
LINK_PRIVATE Trinity.Core
)
Core
#UTILITIES LIB
include_directories(../UTILITIES/INCLUDE)
include_directories(../GRAPHICS/INCLUDE)
include_directories(../PLATFORM/INCLUDE)
#SOURCE
file(GLOB SOURCES "SOURCE/*.cpp")
file(GLOB INCLUDES "INCLUDE/*.h")
#PROJECTNAME
set(PROJECTNAME "Trinity.Core")
#LINK ALL
add_library(${PROJECTNAME} ${SOURCES} ${INCLUDES})
set_target_properties(${PROJECTNAME} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_BINARY_DIR}/LIB/${PLATFORM}/DEBUG"
ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_BINARY_DIR}/LIB/${PLATFORM}/RELEASE"
)
target_link_libraries(${PROJECTNAME}
LINK_PRIVATE Trinity.Graphics
LINK_PRIVATE Trinity.Platform
)
The other look the same.
I've tried PRIVATE instead of LINK_PRIVATE but the result is the same.
You can't link static libraries against each other and it is normal that they only showing up at the executable. Does your code not compiling?
For more information about static and dynamic libraries I suggest this article.
If you want to change the library type in cmake specify the add_lib command:
add_library(${LIB_NAME} ${LIB_TYPE} ${SOURCE})
# with ${LIB_TYPE} = STATIC or SHARED
more informations about cmake
If you have more problems or my answer answer to you is not enough please specify your question.

Right way of creating a library, installing it and linking to another project using CMake

I have a set of files that I want to make into a library and then use that library in another project. This it how it looks like right now
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
SET(CMAKE_CXX_FLAGS "-std=c++11 -fopenmp -fPIC")
add_library (helperlibs lib1.cpp lib2.cpp lib3.cpp lib4.cpp )
INSTALL(TARGETS helperlibs
DESTINATION "${HOME}/lib"
)
INSTALL(FILES lib1.h lib2.h lib3.h lib4.h helperheader.h
DESTINATION "${HOME}/include/helperlibs"
)
In this code Lib4 depends on Lib1-3 and Lib3 depends on Lib1-2 and Lib2 depends on Lib1. Each of these cpp files also depend on a helperheader.h file that contains some definitions and structs.
In my project I have the following CMake file
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
SET( CMAKE_CXX_FLAGS "-std=c++11 -fopenmp -fPIC")
SET(MYINCS ${HOME}/include/helperlibs)
SET(MYLIBDIR ${HOME}/lib)
SET(MYLIBS ${MYLIBDIR}/libhelperlibs.a )
include_directories(${MYINCS})
add_executable(main main.cpp)
target_link_libraries(main ${MYLIBS})
So what I am wondering if you want to create a static library and link to from a another project using cmake is this the way you should write?
Embed the search paths into the library target as properties and create an export.
This way executables in the same build tree will find the library and its include files without you having to specify paths (they become implicit).
I needed to read the cmake documentation carefully a few times before it dawned on me how it should work.
http://www.cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html#creating-packages
http://www.cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html
excerpt from a live example:
add_library(trustportal-util ${CMAKE_CURRENT_LIST_FILE} ${_source_files} ${_disabled_source_files} )
target_link_libraries(trustportal-util ${Boost_LIBRARIES})
if(APPLE)
find_library(SECURITY_FRAMEWORK Security)
target_link_libraries(trustportal-util ${SECURITY_FRAMEWORK})
else()
find_library(LIB_SSL ssl)
find_library(LIB_CRYPTO crypto)
target_link_libraries(trustportal-util ${LIB_SSL} ${LIB_CRYPTO})
endif()
target_compile_definitions(trustportal-util PUBLIC BOOST_MOVE_USE_STANDARD_LIBRARY_MOVE)
target_include_directories(trustportal-util PUBLIC ${Boost_INCLUDE_DIRS})
target_include_directories(trustportal-util PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_include_directories(trustportal-util SYSTEM PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
install(TARGETS trustportal-util
EXPORT trustportal-utilExport
DESTINATION lib
INCLUDES DESTINATION include
)
INSTALL(EXPORT trustportal-utilExport DESTINATION lib)
One option is to do what you are currently doing where you place the includes and libs in a common location, perhaps /usr/include and /usr/lib on linux, or ${HOME} on both Windows/Linux, up to you.
Another option is available if you use git. You can include the project inside another using submodules. Then use include_directory(${submodule}) and build and link directly for every project. The advantage of this approach is that dependencies are more localised. One problem with this method is if you recursively do this, you may end up with duplicates of some projects and cmake will complain that two libraries have the same name.