Using OpenCV on linux with debug assertions enabled

I noticed CV_DbgAssert on linux is NO-OPd even when I build my program in debug mode. This is because _DEBUG is not defined in my build with GCC on linux. Who is expected to define _DEBUG? My program links to OpenCV using CMake and CMake or OpenCVConfig.cmake does not seem to define _DEBUG either.

MSVC seems to define _DEBUG

I appreciate if someone can point me in the right direction.


#if defined _DEBUG || defined CV_STATIC_ANALYSIS
#  define CV_DbgAssert(expr) CV_Assert(expr)
/** replaced with CV_Assert(expr) in Debug configuration */
#  define CV_DbgAssert(expr)

cmake should have added -D _DEBUG to the cxx compiler/linker cmdline, when building the opencv libs (it’s too late in your pogram)

can you check the cmake output, or the result from cv::getBuildInformation() ?

if it really is so, it might be a bug worth reporting
(but i have doubts, that you are right, see e.g. here, at bottom )

I will check my build flags once again, thank you

Yup, that is the problem. I am using the Release mode library wrongly in debug mode through CMake.

    Timestamp:                   2020-09-30T09:55:12Z
    Host:                        Linux 5.8.12-arch1-1 x86_64
    CMake:                       3.5.1
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

Can you help me find which CMake template defines _DEBUG on linux? I looked through opencv/cmake/templates at master · opencv/opencv · GitHub but did not find a match

CMAKE_BUILD_TYPE=DEBUG is the resp. cmake flag.
and cmake should set the c/c++ compiler flags

maybe use a seperate build/install folder, else you overwrite your release libs !