首先,我是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
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")
注意:我正在尝试实现 out-of-source build ,其中Project 1是从Project 2使用的Engine。
我在root_project的构建下运行的命令:cmake .. -G "Visual Studio 15 2017 Win64"
答案 0 :(得分:1)
将我的评论转化为答案
您只需指定Project 2
目标的来源。
我试过给你一个例子,CMake给你一个配置错误(这可能就是为什么它不会显示/没有重写解决方案/项目文件,如果你添加了第二个项目):
CMake Error at CMakeLists.txt:23 (add_executable):
add_executable called with incorrect number of arguments, no sources provided
是的,如果你添加例如您将获得的src/main.cpp
到SOURCES
尚不存在:
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
构建来获取创建的空文件/在开始之前(当您错误输入现有源文件的包含时,您会感到困惑。)
我个人不喜欢这种全球化的选择(见Why is cmake file GLOB evil?和CMake/Ninja attempting to compile deleted `.cpp` file)