c++ - Visual Studio下CMake构建问题

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

首先,我是 CMake 新手。

文件夹结构

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

CMake 文件

root_project 的 CMakeLists.txt:

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

# Project's name
project("RootProject X")

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(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 "Compiling for x86-64 platform. Proceeding...")
ELSEIF(CMAKE_SIZEOF_VOID_P EQUAL 4)
    # 32bit
    message(FATAL_ERROR "Compiling for 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
FIND_PACKAGE(OpenGL 4.5 REQUIRED)

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

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

项目 1 的 CMakeLists:

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

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})

find_package(OpenGL 4.5 REQUIRED)

include_directories(${OPENGL_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/sources/third-party-include/)
link_libraries(${OPENGL_gl_LIBRARY})
add_definitions(${OpenGL_DEFINITIONS})

#include_directories(${CMAKE_CURRENT_SOURCE_DIR}/sources/include/)

# ToDo: Testing
set(HEADERS
    ${CMAKE_CURRENT_SOURCE_DIR}/sources/include/Engine.h
)

set(SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/source/Engine.cpp
)

source_group(headers FILES ${HEADERS})
source_group(sources FILES ${SOURCES})

if(WIN32)
# currently add_executable for testing purpose
add_executable("Project 1" WIN32
    #${HEADERS}
    ${SOURCES}
)
ELSEIF(UNIX AND NOT APPLE)
add_library("Project 1"
    ${HEADERS}
    ${SOURCES}
)
ELSE()
# The system is not supported
message(FATAL_ERROR "System not supported.")
ENDIF()

target_link_libraries(DarkEngine ${OPENGL_gl_LIBRARY})

项目 2 的 CMakeLists:

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

project("Project 2")

# 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

)

source_group(headers FILES ${HEADERS})
source_group(sources FILES ${SOURCES})

add_executable("Project 2" WIN32
    ${HEADERS}
    ${SOURCES}
)

target_link_libraries("Project 2" "Project 1")

问题/问题

注意:我正在尝试实现源外构建,其中项目 1 是项目 2 使用的引擎。

我在 root_project 的构建下运行的命令:cmake .. -G "Visual Studio 15 2017 Win64"

  • 项目 2 未链接到项目 1
  • 项目 2 未显示在 VS 2017 中
  • 我自己在项目 1 的/include 下的头文件将显示在 VS 2017 的外部依赖项中而不是源代码中

最佳答案

将我的评论变成答案

您只需为您的项目 2 目标指定源。

我已经给了你一个尝试的例子,CMake 给你一个配置错误(如果你添加第二个项目,这可能是它不会显示/不重写解决方案/项目文件的原因):

CMake Error at CMakeLists.txt:23 (add_executable):
   add_executable called with incorrect number of arguments, no sources provided

是的,如果您添加例如一个还不存在的 src/main.cppSOURCES 你会得到:

CMake Error at CMakeLists.txt:23 (add_executable):
  Cannot find source file:

    src/main.cpp

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx

因此 CMake 中的工作流程始终是先创建源文件,然后添加到您的 CMakeLists.txt。使用像 VS 这样的 IDE,您只需将一个新项目添加到您的项目中(就像您对非生成项目所做的那样),然后将其添加到您的 CMakeLists.txt 文件中。

对于我的一些项目,我创建了一个 check_and_create_source() 函数,在被覆盖的 add_executable() 调用中调用,但我现在可以说它已经使用了几年这种方法没有真正的好处。您仍然需要运行 ZERO_CHECK 构建以创建/开始之前的空文件(当您错误地输入现有源文件的包含时,您会感到困惑)。

我个人不喜欢 globbing 替代方案(参见 Why is cmake file GLOB evil?CMake/Ninja attempting to compile deleted `.cpp` file)

关于c++ - Visual Studio下CMake构建问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45389000/

相关文章:

使用自定义函数的 C++ std::sort

c++ - 在 C++ 中使用带有共享变量的 pthread 进行多线程

c++ - 如何使用 CUDA 生成随机排列

c++ - iOS 和 OpenCV 错误 : Assertion Failed in PhaseCorrelateRes

asp.net - Ajax ConfirmButtonExtender 在 OnClientClick javascript 代码之前触发

c++ - 向 Microsoft 提供有关 MFC 的反馈

ubuntu - 在 64 位平台上构建 32 位 OpenCV

c++ - 用于集成 Visual Studio 单元测试的 CMake 文件

linux - linux下如何通过编译源码安装devels

linux - 编译自定义 FFMPEG 以将 mp3/wav 转换为 flac