c++ - CMake Visual Studio 解决方案设置

标签 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")

“项目 1”和“项目 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”是一个引擎“项目 2”是游戏。

问题

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

最佳答案

我会尝试按照此处的说明直接从开发人员命令提示符打开 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")

关于c++ - CMake Visual Studio 解决方案设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45106088/

相关文章:

C++ 模拟/测试 boost::asio::io_stream - 基于异步处理程序

c# - Visual Studio 2022 有时显示 "The application was unable to start correctly"

javascript - Coffeescript 解析意外错误 '.'

c++ - 折叠任意多个可变参数包

c++ - 从 catch block 重新抛出异常时丢失异常类型

c# - 如何为 Visual Studio 2008 插件创建安装程序?

java - 编译 NanoVM Java 虚拟机

c++ - 如何使用 opencv contrib 模块编译 tldTracker

c++ - 应用世界变换

c# - Visual Studio 2010 - 无法单步执行 .NET Framework 4 源 (PresentationCore.dll)