ShadowDragon
ShadowDragon

Reputation: 2398

CMake Visual Studio Solution Setup

To begin with, I'm new to CMake.

The project is for the Windows and Linux platform.

Folder Structure

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 files

The first of these files is for the root project and the second one is for "Project 1" and "Project 2" as just one line is different in the second file.

# 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")

The CMakeLists.txt for "Project 1" and "Project 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()

Note: "Project 1" is a library while "Project 2" is an executable and "Project 2" will be based on Project 1. Think of it as "Project 1" is an Engine and "Project 2" is the Game.

Question

Upvotes: 1

Views: 1393

Answers (1)

BareMetalCoder
BareMetalCoder

Reputation: 629

I would try opening the root_project folder directly from the Developer Command Prompt, as per the instructions here: https://blogs.msdn.microsoft.com/vcblog/2016/10/05/cmake-support-in-visual-studio/

cd root_project
devenv .

This built-in support dumps your binaries somewhere under a temp directory in %APPDATA%, so I added the following in my top-level CMakeLists.txt file to dump the binaries out to a specific folder:

# 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")

Upvotes: 1

Related Questions