Last active
July 2, 2023 02:33
-
-
Save fgimian/d5ebdab262d9484db33821d730849d04 to your computer and use it in GitHub Desktop.
My first attempt at a CMake script for building an application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include(FetchContent) | |
cmake_minimum_required(VERSION 3.26) | |
project( | |
totalmix-control | |
VERSION 0.1.0 | |
DESCRIPTION "Provides control over RME TotalMix via OSC." | |
HOMEPAGE_URL "https://github.com/fgimian/totalmix-control" | |
) | |
# Define the executable to be built along with its source files. | |
add_executable(${PROJECT_NAME} src/main.cpp) | |
# Configure which dependencies to download from Git (URLs are used as they're faster than cloning). | |
FetchContent_Declare( | |
tomlplusplus | |
URL https://github.com/marzer/tomlplusplus/archive/refs/tags/v3.3.0.zip | |
) | |
FetchContent_Declare( | |
tinyosc | |
URL https://github.com/mhroth/tinyosc/archive/7acc37ad4ea555c1ab8b89c4e94eac84e6af8d3a.zip | |
) | |
# Compile dependencies so they are available to the build. | |
FetchContent_MakeAvailable(tomlplusplus) | |
if(NOT tinyosc_POPULATED) | |
FetchContent_Populate(tinyosc) | |
add_library(tinyosc_tinyosc STATIC "${tinyosc_SOURCE_DIR}/tinyosc.c") | |
add_library(tinyosc::tinyosc ALIAS tinyosc_tinyosc) | |
target_link_libraries(tinyosc_tinyosc PRIVATE ws2_32) | |
target_include_directories(${PROJECT_NAME} PRIVATE ${tinyosc_SOURCE_DIR}) | |
endif() | |
# Conifgure link and compile options for the build. | |
target_link_options( | |
${PROJECT_NAME} | |
PRIVATE | |
# Ensure that the application is configured to use unicode. | |
-municode | |
) | |
# Conifgure compile options for the build. | |
target_compile_options( | |
${PROJECT_NAME} | |
PRIVATE | |
# A commonly suggested alternative is setting CMAKE_CXX_STANDARD and | |
# CMAKE_CXX_STANDARD_REQUIRED but this didn't seem to work for me. | |
-std=c++23 | |
# Ensure that the application is configured to use unicode. | |
-municode | |
# Configure warnings to be more strict. | |
# See https://github.com/cpp-best-practices/cppbestpractices/blob/master/02-Use_the_Tools_Available.md#gcc--clang | |
-pedantic | |
-Wall | |
-Wextra | |
-Wshadow | |
-Wnon-virtual-dtor | |
-Wold-style-cast | |
-Wcast-align | |
-Wunused | |
-Woverloaded-virtual | |
-Wpedantic | |
-Wconversion | |
-Wsign-conversion | |
-Wmisleading-indentation | |
-Wduplicated-cond | |
-Wduplicated-branches | |
-Wlogical-op | |
-Wnull-dereference | |
-Wuseless-cast | |
-Wdouble-promotion | |
-Wformat=2 | |
-Wzero-as-null-pointer-constant | |
) | |
# Add specific additional link options dependending on the build config. | |
# The release build is built as a GUI application while the debug build will also be built as | |
# a console app which will also provide a terminal where debugging may be performed by writing | |
# to stdout and stderr. | |
target_link_options( | |
${PROJECT_NAME} | |
PRIVATE | |
$<$<CONFIG:RELEASE>:-mwindows> | |
$<$<CONFIG:DEBUG>:-mconsole> | |
) | |
# Add the resource file as a source to be compiled into the executable. | |
target_sources(${PROJECT_NAME} PRIVATE app.rc) | |
# Link the executable against the required Windows libraries. | |
target_link_libraries(${PROJECT_NAME} PRIVATE gdiplus) | |
# Link the executable against the downloaded dependencies. | |
target_link_libraries( | |
${PROJECT_NAME} | |
PRIVATE | |
tinyosc::tinyosc | |
tomlplusplus::tomlplusplus | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment