Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 657 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
#ifdef DEBUG with CMake independent from platform

#1
I am using CMake for building my projects on Windows (Visual Studio) as well as on Linux machines(gcc). I'd like to mark some code as "debugging only", like with

#ifdef DEBUG
//some logging here
#endif

The question is: what compiler definition is available on all platforms in the CMake "Debug" build type? DEBUG seems not to exist. (I want to have the logging or whatever only when the build type is Debug.)
Reply

#2
CMake adds `-DNDEBUG` to the CMAKE_C_FLAGS_{RELEASE, MINSIZEREL} by default. So, you can use `#ifndef NDEBUG`.
Reply

#3
I would suggest that you add your own definition. The `CMake` symbol `CMAKE_C_FLAGS_DEBUG` can contain flags only used in debug mode. For example:

`C`:

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DMY_DEBUG")

`C++`:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DMY_DEBUG")

In your code you can then write the following:

#ifdef MY_DEBUG
// ...
#endif

(Maybe, you would have to use `"/DMY_DEBUG"` for visual studio.)
Reply

#4
In CMake >= 2.8, use [`target_compile_definitions`](

[To see links please register here]

):

target_compile_definitions(MyTarget PUBLIC "$<$<CONFIG:DEBUG>:DEBUG>")

When compiling in Debug mode, this will define the DEBUG symbol for use in your code. It will work even in IDEs like Visual Studio and Xcode for which cmake generates a single file for all compilation modes.

You have to do this for each target [1]. Alternatively you can use [`add_compile_options`](

[To see links please register here]

) (Cmake >= 3.0):

add_compile_options("$<$<CONFIG:DEBUG>:-DDEBUG>")

Note that recent versions of Visual C++ (at least since VS2015) allow either / or - for parameters, so it should work fine across compilers. This command is also useful for other compile options you might like to add ("/O2" in release mode for MSVC or "-O3" for release mode in G++/Clang)


[1] : Note: in CMake >= 3.12 (currently beta) there is also an `add_compile_definitions` that supports generator expressions, which affects all targets.
Reply

#5
I'm using the following in my `CMakeLists.txt`:

set(IS_DEBUG_BUILD CMAKE_BUILD_TYPE STREQUAL "Debug")

# Indication to the code that this is a debug build
if (${IS_DEBUG_BUILD})
add_compile_definitions(__DEBUG__)
endif ()

Then, in my code, I can write:

#ifdef __DEBUG__
// blablabla
#edif

My minimum `CMake` version:

cmake_minimum_required(VERSION 3.16.3)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through