2017-07-14 208 views
1

首先,我是CMake的新手。CMake Visual Studio解决方案设置

该项目是为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") 

的的CMakeLists.txt为 “项目1”, “项目2”:

# 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“是引擎,”项目2“是游戏。

问题

  • 有了这种设置,从哪个文件夹我必须从呼叫CMake的能够打开,其中包含两个项目,“项目1”的Visual Studio 2017的解决方案“root_project”和“项目2“。 cmake .. -G“Visual Studio 15 2017 Win64”
  • 如何在调试模式下编译时将二进制文件放入Debug文件夹以及如何在Release模式下编译时将它们放入Release文件夹中? Release和Debug文件夹可以区分Visual Studio的发行版和调试版本。

回答

0

我想尝试直接从开发者命令提示符打开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")