How to integrate Third party libraries into a Portable Custom Library

Viewed 133

I have been trying to figure out how to link in CMake and it is making me go crazy. So far I have successfully been able to run my library as an executable(add_executable), but when changing it to a library(add_library) and trying to include it in another project it falls short. The issue is including the third party libraries in my library. The third party libraries are not found and therefore are failed to be included in executable project which results missing libraries. I have tried doing it by statically compiling, but I got lost and fell back to shared libraries.

Basically I am trying to figure out how to compile a library so that its dependencies are portable with the library.

Example Header

#ifndef APP_H
#define APP_H

#include <iostream>
#include <vector>
#include <cstdint>

#include <SDL.h>
#include <SDL_ttf.h>
#include <Box2D/Box2D.h>

#include "Class.h"

#include "Vec2.h"
#include "Vec4.h"

using namespace std;

class App{
     /*contents*/
};

#endif

Here is my main CMakeList.txt

add_subdirectory(extern)

cmake_minimum_required(VERSION 3.7)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

set(PROJECT_NAME Project)

project(${PROJECT_NAME} VERSION 0.1.0)

add_subdirectory(src)

Here is my source CMakeList.txt

file(GLOB HEADER_FILES "${CMAKE_SOURCE_DIR}/include/*.h")
file(GLOB SOURCES "*.cpp")

file(GLOB EXTERN_SOURCES "${CMAKE_SOURCE_DIR}/extern/stb/include/*.h" "${CMAKE_SOURCE_DIR}/extern/stb/src/*.cpp" )

include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_SOURCE_DIR}/include/Chassis2D" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2/include/" PUBLIC "${CMAKE_SOURCE_DIR}/extern/SDL2_ttf" PUBLIC "${CMAKE_SOURCE_DIR}/extern/stb/include/")

add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADER_FILES} ${EXTERN_SOURCES})

target_link_libraries(${PROJECT_NAME} PUBLIC SDL2 PUBLIC SDL2_ttf PUBLIC box2d)

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"
)

Thanks in advance!

0 Answers
Related