--- dlib/cmake_utils/add_global_compiler_switch.cmake
+++ dlib/cmake_utils/add_global_compiler_switch.cmake
@@ -7,18 +7,18 @@
 macro ( add_global_compiler_switch switch_name )
    # If removing the switch would change the flags then it's already present
    # and we don't need to do anything.
-   string(REPLACE "${switch_name}" "" tempstr "${CMAKE_CXX_FLAGS}")
-   if ("${CMAKE_CXX_FLAGS}" STREQUAL "${tempstr}" )
-      set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${switch_name}" 
+   string(REPLACE "${switch_name}" "" tempstr "${CMAKE_CXX_FLAGS_RELEASE}")
+   if ("${CMAKE_CXX_FLAGS_RELEASE}" STREQUAL "${tempstr}" )
+      set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${switch_name}" 
          CACHE STRING "Flags used by the compiler during all C++ builds." 
          FORCE)
    endif ()
 endmacro()
 
 macro ( remove_global_compiler_switch switch_name )
-   string(REPLACE "${switch_name}" "" tempstr "${CMAKE_CXX_FLAGS}")
-   if (NOT "${CMAKE_CXX_FLAGS}" STREQUAL "${tempstr}" )
-      set (CMAKE_CXX_FLAGS "${tempstr}" 
+   string(REPLACE "${switch_name}" "" tempstr "${CMAKE_CXX_FLAGS_RELEASE}")
+   if (NOT "${CMAKE_CXX_FLAGS_RELEASE}" STREQUAL "${tempstr}" )
+      set (CMAKE_CXX_FLAGS_RELEASE "${tempstr}" 
          CACHE STRING "Flags used by the compiler during all C++ builds." 
          FORCE)
    endif ()

