Skip to content

Instantly share code, notes, and snippets.

@zznewclear13
Last active November 14, 2023 08:05
Show Gist options
  • Save zznewclear13/9240df6fabe7966dc0b8e0cfbdcd8546 to your computer and use it in GitHub Desktop.
Save zznewclear13/9240df6fabe7966dc0b8e0cfbdcd8546 to your computer and use it in GitHub Desktop.
CMakeLists.txt template for visual studio, Windows
#[[
# CMakeLists.txt template for visual studio, Windows
## Features
1. Configure project with just a double click.
2. Minimized configuration: Debug and Release.
3. Customized binary output directory.
4. Automatically set startup project.
5. Automatically find all source files.
6. Automatically generate visual studio filters.
7. Hide console in Release build.
## Folder Structure
```
Base Folder
|
+---- binary
| |
| +---- *binary files*
|
+---- build
| |
| +---- MyApp.sln
| +---- *cmake files*
|
+---- include
|
+---- library
|
+---- source
| |
| +---- *source files*
| +---- CMakeLists.txt
|
+---- cmake.bat
```
## cmake.bat
```
@echo off
cd %~dp0
if not exist "%~dp0build" (mkdir "%~dp0build")
cd %~dp0build
cmake ../source
cd %~dp0
pause
```
## Usage
Double click `cmake.bat`.
## Limitations
Currently only supports configuring visual studio projects on Windows.
## The MIT License (MIT)
Copyright © 2023 zznewclear@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
cmake_minimum_required(VERSION 3.20.0 FATAL_ERROR)
set(MY_APP_NAME "MyApp")
message("[${MY_APP_NAME}] CMake configuration begins...")
# Set configuration types
set(CMAKE_CONFIGURATION_TYPES Debug Release)
project(
"My App"
DESCRIPTION "My app's description."
LANGUAGES CXX
)
if(NOT MSVC)
message( FATAL_ERROR "This app currently only supports MSVC for building." )
endif()
# Set c++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message("[${MY_APP_NAME}] Making paths...")
# Set path variables
set(MY_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
cmake_path(GET MY_SOURCE_DIR PARENT_PATH MY_PROJECT_DIR)
set(MY_INCLUDE_DIR ${MY_SOURCE_DIR} ${MY_PROJECT_DIR}/include)
set(MY_LIBRARY_DIR ${MY_PROJECT_DIR}/library)
set(MY_BINARY_DIR ${MY_PROJECT_DIR}/binary)
message("[${MY_APP_NAME}] PROJECT_DIR: ${MY_PROJECT_DIR}")
# Set My App as visual studio startup project
set_property(DIRECTORY ${MY_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${MY_APP_NAME})
message("[${MY_APP_NAME}] Gathering all source files...")
# Find all source files, auto re-configure if any file changes
file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS *.cpp* *.h* *.hpp*)
message("\n[${MY_APP_NAME}] Building visual studio filters...")
# Set visual studio filters
foreach(_source IN ITEMS ${SOURCE_FILES})
get_filename_component(_source_path "${_source}" PATH)
string(REPLACE "${CMAKE_SOURCE_DIR}" "" _group_path "${_source_path}")
string(REPLACE "/" "\\" _group_path "${_group_path}")
message(${_source})
source_group("${_group_path}" FILES "${_source}")
endforeach()
message("\n[${MY_APP_NAME}] Setting build directories...")
# Set custom build directories
foreach(config IN ITEMS ${CMAKE_CONFIGURATION_TYPES})
string(TOUPPER ${config} CONFIG)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${CONFIG} ${MY_BINARY_DIR}/${config}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${CONFIG} ${MY_BINARY_DIR}/${config}/dll)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${CONFIG} ${MY_BINARY_DIR}/${config}/lib)
endforeach()
message("[${MY_APP_NAME}] Adding executables...")
# Add executable
add_executable(${MY_APP_NAME} ${SOURCE_FILES})
# Set include directories
target_include_directories(${MY_APP_NAME} PUBLIC ${MY_INCLUDE_DIR})
# Set link directories
target_link_directories(${MY_APP_NAME} PUBLIC ${MY_SOURCE_DIR})
# Set custom compile definitions
target_compile_definitions(${MY_APP_NAME} PUBLIC _UNICODE UNICODE WIN32_LEAN_AND_MEAN NOMINMAX _USE_MATH_DEFINES)
target_compile_definitions(${MY_APP_NAME} PUBLIC $<UPPER_CASE:${MY_APP_NAME}_$<CONFIG>>)
# Link to libraries
set(MY_LINK_LIBRARIES "d3d12.lib" "dxgi.lib" "dxguid.lib")
target_link_libraries(${MY_APP_NAME} PUBLIC ${MY_LINK_LIBRARIES})
# Disable console in release build
set(MY_DISABLE_CONSOLE "/SUBSYSTEM:WINDOWS" "/ENTRY:mainCRTStartup")
target_link_options(${MY_APP_NAME} PRIVATE $<$<CONFIG:Release>:${MY_DISABLE_CONSOLE}>)
message("[${MY_APP_NAME}] CMake configuration ends.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment