CMake Visual Studio解决方案设置

时间:2017-07-14 14:59:03

标签: c++ visual-studio compilation cmake visual-studio-2017

首先,我是CMake的新手。

该项目适用于Windows和Linux平台。

文件夹结构

root_project
  |─── CMakeLists.txt
  |─── Project 1
  |      |─── build
  |      |      |─── Debug
  |      |      └─── Release
  |      |─── source
  |      |      |─── CMakeLists.txt
  |      |      └─── include
  |      |─── resource
  |      └─── header
  └─── Project 2
         |─── build
         |      |─── Debug
         |      └─── Release
         |─── source
         |      |─── CMakeLists.txt
         |      └─── include
         |─── resource
         └─── header

CMake文件

这些文件中的第一个用于根项目,第二个用于“项目1”和“项目2”,因为第二个文件中只有一行不同。

# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.8.2)

# Project's name
project("root_project")

set_property(GLOBAL PROPERTY USE_FOLDERS ON)

IF(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
    message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt.")
ENDIF()

OPTION(BUILD_TESTS "Decides whether the unit tests will be built." ON)

# C/C++ languages required.
enable_language(C)
enable_language(CXX)

# Set the C++ Version
message("!REQUIRED! -- Supported features = ${cxx_std_14}")
message("Supported features = ${cxx_std_17}")

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Only allow 64bit architecture
IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
    # 64bit
    message(STATUS "Running on x86-64 platform. Proceeding...")
ELSEIF(CMAKE_SIZEOF_VOID_P EQUAL 4)
    # 32bit
    message(FATAL_ERROR "Running on x86 platform. This is not supported. Aborting...")
ELSE()
    # unidentified architecture
    message(FATAL_ERROR "Running on unidentified architecture. This is not supported. Aborting...")
ENDIF()

# Abort when OpenGL is not found
IF(NOT OPENGL_FOUND)
    message(WARNING "Could not find OpenGL library.")
ENDIF()

IF(NOT VULKAN_FOUND)
    message(WARNING "Could not find Vulkan library.")
ENDIF()

message(STATUS "----------------------------------------")
message(STATUS "CMake Binary Dir:" ${CMAKE_BINARY_DIR})
message(STATUS "CMake Source Dir:" ${CMAKE_SOURCE_DIR})
message(STATUS "CMake CFG Dir:" ${CMAKE_CFG_INTDIR})
message(STATUS "CMake exe Dir:" ${EXECUTABLE_OUTPUT_PATH})
message(STATUS "CMake lib Dir:" ${LIBRARY_OUTPUT_PATH})

# Add the modules
add_subdirectory("Project 1")
add subdirectory("Project 2")

“Project 1”和“Project 2”的CMakeLists.txt:

# Specify the minimum version for CMake
cmake_minimum_required(VERSION 3.8.2)

project(Project 1)

# Set the version number of the project here
set(VERSION_MAJOR "0")
set(VERSION_MINOR "1")
set(VERSION_PATCH "0")
set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH})

set(HEADERS

)

set(SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
)

set(HEADERS_WIN32

)

set(SOURCES_WIN32

)

set(HEADERS_LINUX

)

set(SOURCES_LINUX

)

source_group(headers FILES ${HEADERS} ${HEADERS_WIN32} ${HEADERS_LINUX})
source_group(sources FILES ${SOURCES} ${SOURCES_WIN32} ${HEADERS_LINUX})

if(WIN32)
add_library(DarkEngine
    ${HEADERS}
    ${SOURCES}
    ${HEADERS_WIN32}
    ${SOURCES_WIN32}
)
ELSEIF(UNIX AND NOT APPLE)
add_library(DarkEngine
    ${HEADERS}
    ${SOURCES}
    ${HEADERS_LINUX}
    ${HEADERS_LINUX}
)
ELSE()
# The system is not supported
message(FATAL_ERROR "System not supported.")
ENDIF()

注意:“项目1”是一个库,而“项目2”是可执行文件,“项目2”将基于项目1.将其视为“项目1”是一个引擎而“Project 2”就是游戏。

问题

  • 进行此设置后,我必须从哪个文件夹调用CMake才能在包含两个项目“Project 1”和“Project 2”的Visual Studio 2017中打开解决方案“root_project”。 cmake .. -G“Visual Studio 15 2017 Win64”
  • 如何在Debug模式下编译时将二进制文件放入Debug文件夹,以及在Release模式下编译时如何将它们放入Release文件夹? Release和Debug文件夹用于区分Release Studio和Release Studio之类的版本。

1 个答案:

答案 0 :(得分:1)

我会尝试直接从Developer Command Prompt打开root_project文件夹,按照此处的说明操作: https://blogs.msdn.microsoft.com/vcblog/2016/10/05/cmake-support-in-visual-studio/

cd root_project
devenv .

此内置支持将您的二进制文件转储到%APPDATA%中临时目录下的某个位置,因此我在顶级CMakeLists.txt文件中添加了以下内容以将二进制文件转储到特定文件夹:

# My understanding of what the output types mean. Check CMAKE documentation
# to confirm:
#   ARCHIVE: Static & Import Library directory
#   LIBRARY: DLL directory (MODULE)
#   RUNTIME: DLL directory (SHARED) 

# If you use these lines, VS will automatically create Debug and Release dirs
# under the directories you provide.

set (CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../lib")
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../dll")

# In my project I want Debug and Release to clobber each other because
# I am building a DLL that I want to deploy next to an existing exe that
# loads it, so I explicitly point both scenarios to the same place
# (note the "_DEBUG" and "_RELEASE" suffixes to CMAKE_xxx_OUTPUT_DIRECTORY)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_LIST_DIR}/../lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_LIST_DIR}/../lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_LIST_DIR}/../dll")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_LIST_DIR}/../lib")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_LIST_DIR}/../lib")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_LIST_DIR}/../dll")