Better_Software_Header_MobileBetter_Software_Header_Web

Find what you need - explore our website and developer resources

Setting C++ Defines with CMake

Why you should use configure_file instead of add_definitions

project(MyApp VERSION 1.5)
const QString aboutString = QStringLiteral("My App version: %1").arg(MYAPP_VERSION_STRING);
  QMessageBox::information(this, "My App", aboutString);
option(START_MAXIMIZED "Show the mainwindow maximized" OFF)
#ifdef START_MAXIMIZED
      w.showMaximized();
  #else
      w.show();
  #endif
add_definitions(-DMYAPP_VERSION_STRING="${PROJECT_VERSION}")
  if (START_MAXIMIZED)
     add_definitions(-DSTART_MAXIMIZED)
  endif()
target_compile_definitions(myapp PRIVATE MYAPP_VERSION_STRING="${PROJECT_VERSION}")
  if(START_MAXIMIZED)
     target_compile_definitions(myapp PRIVATE START_MAXIMIZED)
  endif()
configure_file(myapp_config.h.in myapp_config.h)
#define MYAPP_VERSION_STRING "${PROJECT_VERSION}"
  #cmakedefine01 START_MAXIMIZED
#define MYAPP_VERSION_STRING "1.5"
  #define START_MAXIMIZED 1
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    target_compile_options(myapp PRIVATE -Werror=undef)
  endif()

About KDAB


9 Comments

13 - Nov - 2024

Renaud G.

namespace config
{
constexpr int major{@PROJECT_VERSION_MAJOR@};
constexpr int minor{@PROJECT_VERSION_MINOR@};
constexpr int patch{@PROJECT_VERSION_PATCH@};

constexpr auto version{"@PROJECT_VERSION@"};
}

13 - Nov - 2024

David

14 - Nov - 2024

David Faure

15 - Nov - 2024

David

14 - Nov - 2024

Giuseppe D'Angelo

14 - Nov - 2024

Tobias

16 - Nov - 2024

Jens Alfke

17 - Nov - 2024

David Faure

4 - Dec - 2024

David Faure

DavidFaure

David Faure

Senior Software Engineer

Learn Modern C++

Learn more