kfmfe04
kfmfe04

Reputation: 15327

How do I set GLIBCXX_FORCE_NEW=1 in cmake for DEBUG builds only?

I need to set the environment variable GLIBCXX_FORCE_NEW=1 for DEBUG builds only, in cmake.

In the cmake documentation, I could only find:

Upvotes: 2

Views: 4349

Answers (3)

sakra
sakra

Reputation: 65791

The environment variable GLIBCXX_FORCE_NEW only affects the behavior of a compiled program at runtime (see the gcc documentation). Adding it as a preprocessor define during compile time of a program (e.g., by setting the CMAKE_CXX_FLAGS) will not have an effect.

With CMake you can set an environment variable that affects the runtime of a built target only for CMake tests. The following commands add a test valid for DEBUG builds, which will run an executable with the GLIBCXX_FORCE_NEW variable set:

add_test(NAME MyTest CONFIGURATIONS Debug COMMAND MyExecutable)
set_tests_properties(MyTest PROPERTIES ENVIRONMENT "GLIBCXX_FORCE_NEW=1")

Upvotes: 6

Vladislav Vaintroub
Vladislav Vaintroub

Reputation: 5653

Why do you need an "environment variable" for builds? If you want to set compiler or preprocessor flag for debug builds only , use CMAKE_CXX_FLAGS_DEBUG, e.g

SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DGLIBCXX_FORCE_NEW=1")

Don't test if(CMAKE_BUILD_TYPE STREQUAL "Debug"), it won't work on all non-Makefile CMake generators (OSX with Xcode, Windows with Visual Studio)

Upvotes: 0

arrowd
arrowd

Reputation: 34401

You want CMAKE_BUILD_TYPE.

You can check for build type using if(CMAKE_BUILD_TYPE STREQUAL "Debug").

Also, be aware that STREQUAL is case-sensitive, so you might want to string(UPPERCASE ...) your variable before check.

Upvotes: 2

Related Questions