# Using OpenCV on linux with debug assertions enabled

**URL:** <https://forum.opencv.org/t/using-opencv-on-linux-with-debug-assertions-enabled/4674>\
**Category:** C++\
**Tags:** build, core\
**Created:** [August 9, 2021, 9:08am UTC](https://forum.opencv.org/t/using-opencv-on-linux-with-debug-assertions-enabled/4674 "2021-08-09T09:08:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![knz](https://avatars.discourse-cdn.com/v4/letter/k/9dc877/32.png) [@knz](https://forum.opencv.org/u/knz)\
**Post date:** [August 9, 2021, 9:08am UTC](https://forum.opencv.org/t/using-opencv-on-linux-with-debug-assertions-enabled/4674/1 "2021-08-09T09:08:06Z")

</div>

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](https://docs.microsoft.com/en-us/cpp/c-runtime-library/debug?view=msvc-160) to define `_DEBUG`

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

`opencv2/core/base.hpp`

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

```

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 9, 2021, 9:21am UTC](https://forum.opencv.org/t/using-opencv-on-linux-with-debug-assertions-enabled/4674/2 "2021-08-09T09:21:31Z")

</div>

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()` ?

> [@knz](#):
>
> This is because `_DEBUG` is not defined in my build with GCC on linux.

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](https://pullrequest.opencv.org/buildbot/builders/precommit_linux64/builds/32735/steps/cmake/logs/stdio) )

---

<div class="post-metadata">

**Author:** ![knz](https://avatars.discourse-cdn.com/v4/letter/k/9dc877/32.png) [@knz](https://forum.opencv.org/u/knz)\
**Post date:** [August 9, 2021, 9:34am UTC](https://forum.opencv.org/t/using-opencv-on-linux-with-debug-assertions-enabled/4674/3 "2021-08-09T09:34:01Z")

</div>

I will check my build flags once again, thank you

---

<div class="post-metadata">

**Author:** ![knz](https://avatars.discourse-cdn.com/v4/letter/k/9dc877/32.png) [@knz](https://forum.opencv.org/u/knz)\
**Post date:** [August 9, 2021, 9:47am UTC](https://forum.opencv.org/t/using-opencv-on-linux-with-debug-assertions-enabled/4674/4 "2021-08-09T09:47:13Z")

</div>

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

```auto
  Platform:
    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](https://github.com/opencv/opencv/tree/master/cmake/templates) but did not find a match

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [August 9, 2021, 9:53am UTC](https://forum.opencv.org/t/using-opencv-on-linux-with-debug-assertions-enabled/4674/5 "2021-08-09T09:53:27Z")

</div>

`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 !
