1
votes

First I want to say this is not a duplicate to Make References like Visual Studio in CMake

I look over the internet and i cant see what maps to Visual Studio project refereces in CMake. I found this page CMake and Visual Studio but references is not there. So this is my folder config:

- CMakeLists.txt
- demo/
----CMakeLists.txt
- engine/
----CMakeLists.txt
- bin/
----[config]/
------[target_name]

The demo project is a dll and the engine project is an exe and i want to add the engine as a reference in the dll so when i compile the dll it compiles the exe if changes were made.

Main CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

cmake_policy(SET "CMP0079"  NEW)

# set configuration types and make them advanced option on cmake.
mark_as_advanced(CMAKE_INSTALL_PREFIX)
set(CMAKE_CONFIGURATION_TYPES Release Debug Release_Asserts)
set(CMAKE_CXX_FLAGS_RELEASE_ASSERTS ${CMAKE_CXX_FLAGS_RELEASE})
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE_ASSERTS ${CMAKE_SHARED_LINKER_FLAGS_RELEASE})

# set c++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# set the project/solution name
project("Llamathrust Engine"
        VERSION 1.0
        DESCRIPTION "Game engine Win32"
        LANGUAGES C CXX)
# use folders for ZERO_CHECK and BUILD_ALL
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# add glad library
add_subdirectory("${CMAKE_SOURCE_DIR}/external/glad")

# add glm library
add_subdirectory("${CMAKE_SOURCE_DIR}/external/glm")

# add the engine to the solution
add_subdirectory("${CMAKE_SOURCE_DIR}/llamathrust/")
target_include_directories("llamathrust" PRIVATE "${CMAKE_SOURCE_DIR}/external/opengl/include")
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT llamathrust)

# link llamathrust to GLAD
target_link_libraries("llamathrust" "glad")
# link llamathrust to GLM
target_link_libraries("llamathrust" "glm")
target_include_directories("llamathrust" PUBLIC "${CMAKE_SOURCE_DIR}/external/glm")

# add the demo code to the solution
add_subdirectory("${CMAKE_SOURCE_DIR}/demo/")

# link demo and llamathrust
# link llamathrust to GLM
target_link_libraries("demo" "glm")
target_include_directories("demo" PUBLIC "${CMAKE_SOURCE_DIR}/external/glm")
# remove ...
remove_definitions(/CMAKE_INTDIR)

# adding options

# option to include a blank screen game
#if (BLANK_TARGET)
#endif()

# option to include the demo game
#if (DEMO_TARGET)
#add_subdirectory("${CMAKE_SOURCE_DIR}/demo/")
#endif()

Demo CMakeLists

cmake_minimum_required(VERSION 3.10)

# set the target/project name
set(TARGET_NAME "demo")

# set c++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# set output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${TARGET_NAME}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${TARGET_NAME}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${TARGET_NAME}")

# add preprocessor definitions
add_definitions(-DLT_DYNAMIC_LIB)

# set sources
set(Demo_SRC
main/main_local.hpp
main/main_local.cpp
main/local_imports.hpp
platform/main.cpp
games/game_demo.hpp
games/game_demo.cpp
)

# executable name
add_library(${TARGET_NAME} SHARED ${Demo_SRC})

# set filters
foreach(_source IN ITEMS ${Demo_SRC})
# Get the directory of the source file
get_filename_component(_source_path "${_source}" PATH)

# Make sure we are using windows slashes
string(REPLACE "/" "\\" _group_path "${_source_path}")

source_group("${_group_path}" FILES "${_source}")
endforeach()

set_target_properties(${TARGET_NAME} PROPERTIES PROJECT_LABEL ${TARGET_NAME})
set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)

# set include directory to itself
target_include_directories(${TARGET_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(${TARGET_NAME} PUBLIC  "${CMAKE_SOURCE_DIR}/llamathrust/include")

# make a reference to the engine

Engine CMakeLists

cmake_minimum_required(VERSION 3.1)

set(TARGET_NAME "llamathrust")

# set output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${TARGET_NAME}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${TARGET_NAME}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../bin/${TARGET_NAME}")

# set sources
set(Llamathrust_SRC
...
)
find_package(OpenGL REQUIRED)

# executable name
add_executable(${TARGET_NAME} ${Llamathrust_SRC})

# set filters
foreach(_source IN ITEMS ${Llamathrust_SRC})
# Get the directory of the source file
get_filename_component(_source_path "${_source}" PATH)

# Make sure we are using windows slashes
string(REPLACE "/" "\\" _group_path "${_source_path}")

source_group("${_group_path}" FILES "${_source}")
endforeach()

# find and link to opengl
target_link_libraries(${TARGET_NAME} ${OPENGL_LIBRARY})
target_include_directories(${TARGET_NAME} PRIVATE "${OPENGL_INCLUDE_DIR}")

set(LIBS XInput)
target_link_libraries(${TARGET_NAME} ${LIBS})

# set include directory to itself
target_include_directories(${TARGET_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")

# target name label
set_target_properties(${TARGET_NAME} PROPERTIES PROJECT_LABEL ${TARGET_NAME})



#preprocessor definitions
# DEBUG
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY
  COMPILE_DEFINITIONS $<$<CONFIG:Debug>:LT_DEBUG>)
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY
  COMPILE_DEFINITIONS $<$<CONFIG:Debug>:LT_ENABLE_ASSERTS>)
#RELEASE_ASSERT
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY
  COMPILE_DEFINITIONS $<$<CONFIG:Release_Asserts>:LT_RELEASE>)
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY
  COMPILE_DEFINITIONS $<$<CONFIG:Release_Asserts>:LT_ENABLE_ASSERTS>)
#RELEASE
set_property(TARGET ${TARGET_NAME} APPEND PROPERTY
  COMPILE_DEFINITIONS $<$<CONFIG:Release>:LT_RELEASE>)

Thanks in advance!

1

1 Answers

2
votes

Well this is embarrassing, I just found out the answer on my own looking similar words to reference like dependency. The answer is add_dependencies("demo" "llamathrust") I'll keep this question since is a lot clear for visual studio developers to find the answer but final moderators have the final word.