0

I'm trying to build two projects using cmake at same-time. My folder structure is as follows:



project
├── CMakeLists.txt
├── build
├── out
├── lib
├── yanthra_engine
│   ├── CMakeLists.txt
│   └── ...
└───sandbox
    ├── CMakeLists.txt
    └── ...

  

main CmakeLists.txt

cmake_minimum_required(VERSION 3.4.1)
project(yanthra_console VERSION 0.1 DESCRIPTION "A 3d Game Engine.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fexceptions")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo;Release;Debug" CACHE STRING "Build type selections" FORCE)

add_subdirectory(yanthra_engine)
add_subdirectory(sandbox)


yanthra_engine/CMakeLists.txt



set(THIRD_PARTY_DIR "../../../third-party")
set(MAIN_SOURCE_DIR "../../main/src")
include_directories(${THIRD_PARTY_DIR}/SDL/include)



set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib )

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../lib)



file(GLOB_RECURSE CPP_HEADERS ${MAIN_SOURCE_DIR}/*.hpp)
file(GLOB_RECURSE CPP_SOURCES ${MAIN_SOURCE_DIR}/*.cpp)


add_library(
   yanthra
   SHARED
   ${CPP_HEADERS}
   ${CPP_SOURCES}
)


set (CMAKE_SHARED_LINKER_FLAGS "-F../yanthra_engine/Frameworks -framework SDL2 -framework OpenGL")

sandbox/CMakeLists.txt


set(THIRD_PARTY_DIR "../../main")
set(MAIN_SOURCE_DIR "./src")
include_directories(${THIRD_PARTY_DIR}/include)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../out)

file(GLOB_RECURSE CPP_HEADERS ${MAIN_SOURCE_DIR}/includes/*.h)
file(GLOB_RECURSE CPP_SOURCES ${MAIN_SOURCE_DIR}/*.cpp)
file(GLOB_RECURSE LIB ${MAIN_SOURCE_DIR}/../lib/*.dylib)

add_executable(
    yanthra_sandbox
    ${CPP_HEADERS}
    ${CPP_SOURCES}
    )

set_target_properties(
    yanthra_sandbox
    PROPERTIES
    LINK_FLAGS
    "-F../yanthra_engine/Frameworks -framework SDL2 -framework OpenGL"
    )

target_link_libraries(yanthra_sandbox PRIVATE ${LIB})

I would like to know if I'm generating library file in each build mode, how will I link it with it's corresponding executable, given the fact that each mode builds its output to to its own folder i.e for library its lib/Debug (for debug mode) and for executable its out/Debug.

2
  • Btw, are there other libraries you need to link other than yanthra (and possibly its dependencies)? Commented Nov 14, 2020 at 8:52
  • @fabian yes I also want to link other libraries like SDL and OpenGL etc. Thanks for replying. Commented Nov 14, 2020 at 9:01

1 Answer 1

1

You don't seem to link the link the yanthra target. You should do this though, since this will automatically choose the library compiled with the current configuration.

sandbox/CMakeLists.txt

...
target_link_libraries(yanthra_sandbox PRIVATE yanthra ${LIB})
...

As for importing the other libs: It would be preferrable to use find_library. This should automatically set the link options and make adding them for yanthra_sandbox unnecessary.

yanthra_engine/CMakeLists.txt

...

list(APPEND CMAKE_FRAMEWORK_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Frameworks")

find_library(SDL2_LIB SDL2 REQUIRED)
find_library(OPEN_GL_LIB OpenGL REQUIRED)

target_link_library(yanthra PUBLIC "${SDL2_LIB}" "${OPEN_GL_LIB}")

This should allow you to remove the compiler flags from both targets. If there are no dependencies in the lib directory you could also remove the search for the libraries on the file system/

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the Wonderfull answer. I do have another doubt should I install the library or should I just link it. Just by linking it works. Is that desired behaviour?
This entirely depends on what you want to do with the program/libs. If all of the requirements are fulfilled without installing the lib, it seems unnecessary to install the lib. If you want to publish the lib you'll probably want to test the lib in the installed version and check, if all the files end up in the correct places. I'm not familiar enough with macOS to know about all the consequences of a lib (not) being installed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.