CMake中的Visual Studio项目引用

3

首先我想说这不是一个重复的问题,与在CMake中创建类似于Visual Studio的引用不同。

我查看了互联网,但无法找到在CMake中映射到Visual Studio项目引用的内容。我找到了这个页面CMake和Visual Studio,但没有提及引用。因此,这是我的文件夹配置:

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

这个演示项目是一个动态链接库(dll),引擎项目是一个可执行文件(exe),我想在dll中添加引擎作为参考,这样当我编译dll时,如果有更改,它会同时编译exe。

主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()

演示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

引擎 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>)

事先感谢您!

1个回答

3

这有点尴尬,我刚刚自己找到了答案,查找类似于“dependency”的单词作为参考。答案是添加依赖项("demo" "llamathrust")。我会保留这个问题,因为对于Visual Studio开发人员来说,找到答案更加清晰,但最终的管理员有最后的决定权。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接