Hello everyone,
im trying to setup a two different build modes for my c++ project. Im fairly new to “low”level programming in general, and especially when it comes to compiling etc. so I appreciate every bit of help
Currently MyCmakepresets.json looks like this:
{
"version": 4,
"configurePresets": [
{
"name": "DEBUG",
"displayName": "Debug Configuration",
"description": "Debug build with development features enabled",
"binaryDir": "${sourceDir}/build/debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "RELEASE",
"displayName": "Release Configuration",
"description": "Optimized release build for distribution",
"binaryDir": "${sourceDir}/build/release",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
]
}
And my CMakeLists.txt like this:
cmake_minimum_required(VERSION 4.1.1)
project(d_p VERSION 0.1 LANGUAGES CXX)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
include(FetchContent)
FetchContent_Declare(SFML
GIT_REPOSITORY https://github.com/SFML/SFML.git
GIT_TAG 3.0.1
GIT_SHALLOW ON
EXCLUDE_FROM_ALL
SYSTEM)
FetchContent_MakeAvailable(SFML)
file(GLOB_RECURSE SOURCE_FILES CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
include(CTest)
enable_testing()
if(MSVC)
set(CMAKE_CXX_FLAGS_DEBUG "/Zi /Od /MDd /RTC1 /Fd${CMAKE_BINARY_DIR}/debug.pdb")
set(CMAKE_CXX_FLAGS_RELEASE "/O2 /GL /Gy /Gw /Oi /MD /DNDEBUG")
else()
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wextra -Wpedantic -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -march=native -fomit-frame-pointer -DNDEBUG")
endif()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
add_compile_definitions(
BUILD_MODE_DEBUG=1
BUILD_MODE_RELEASE=0
BUILD_TYPE="Debgu"
)
else()
add_compile_definitions(
BUILD_MODE_DEBUG=0
BUILD_MODE_RELEASE=1
BUILD_TYPE="Debgu"
)
endif()
if(MSVC)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG")
else()
if(NOT APPLE)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -flto")
endif()
endif()
add_executable(d_p ${SOURCE_FILES})
target_compile_features(d_p PRIVATE cxx_std_17)
target_link_libraries(d_p PRIVATE SFML::Graphics)
Now this setup mostly works, I can use the “CMake Tools” extension to switch between my configurations and my macros correctly detect whether im in debug or release mode, but for some reason, the Release build keeps putting the compiled exe into /build/release/bin/Debug
, but when I click the extension it tries to launch /build/release/bin/Release
.
What is the reason for this? My macros change the project into correctly into release mode so Id assume the release compiler flags are also applied and everything else is correctly set to release mode in CMake, but the output keeps going into the debug folder.
EDIT: I just checked by renaming the Debug folder in the release directory to Release, and I can still debug the program with breakpoints and when I check the file size, the release build and debug build are exactly the same size (both 2121kb) so I think my compiler flags also dont get applied