Until today I have been building a C++ application in debug mode. I have now built it using release mode, and I want to assess what difference this makes to the application performance.
Having done an initial test, I see no change to the application performance.
This might be expected, since the application does a lot of work interacting with network sockets as well as parsing JSON data.
Is there a way to inject some information such that I can std::println some information when my application starts up which indicates whether cmake has been ran in debug or release mode?
I'm thinking that perhaps there might be a way to inject the value of CMAKE_CXX_FLAGS_DEBUG or CMAKE_CXX_FLAGS_RELEASE into a C++ char*, and then log one or the other depending on the build mode? But this is just an initial idea. I have no clue how to actually do it, or if this is the right approach.
I think @Dominik Kasewski's answer is more appropriate for what you want to do but there is also the configure_file mechanism
configure_file(myfile.h.in myfile.h)
CMake will take myfile.h.in and expand cmake variables in it:
static constexpr const char * build_type = "${CMAKE_BUILD_TYPE}";
to create a file that
static constexpr const char * build_type = "Debug";
for example.
${...} constructs in the file and dont want those expanded you can also use @CMAKE_BUILD_TYPE@ and add the @ONLY argument to configure_filetarget_compile_definitions.Since this is more complicated than what @Dominik Kaszewski suggests, I would go with their answer but if you have more information that you want to pass from CMake to the source code, then this is a convenient way to do it.
What you want can be achieved with #ifdef. Since debug mode commonly sets -DDEBUG=1 (or -DDBG=1, you can easily control it either way), you can check for it to determine the mode you are in:
#ifdef DEBUG
static constexpr const char* mode = "debug";
#else
static constexpr const char* mode = "release";
#endif
printf("You are in %s mode\n", mode);
Other config options can be passed as preprocessor flags in the same way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With