NerdyKoala
NerdyKoala

Reputation: 25

How to handle not finding OpenCV in source code

I have built a C++ library that depends on OpenCV. However, sometimes I don't need to use OpenCV, and I compile the code without it.

The idea is, when the OpenCV_Found variable in CMake is true, there would be a pre-processor variable similar to the OpenCV_Found variable, but in the C++ code. In other words, I want to do something like this:

#ifdef OpenCV_Found
#include <opencv2/core/core.hpp>
#endif

My question is:

Does OpenCV provide a similar variable like that? Or do I have to define it manually, and send it to the target compilation flags?

Upvotes: 0

Views: 65

Answers (1)

Kevin
Kevin

Reputation: 18243

You can use CMake to forward the OpenCV_Found CMake definition to your C++ source code. You can use a generator expression in target_compile_definitions() like this:

add_executable(MyExe ${MY_SOURCES})
target_compile_definitions(MyExe PUBLIC $<$<BOOL:${OpenCV_Found}>:OpenCV_Found>)

This will add OpenCV_Found as a pre-processor definition to the MyExe target when the ${OpenCV_Found} CMake variable evaluates to True. When the CMake variable ${OpenCV_Found} evaluates to False (case-insensitive equal of 0, FALSE, OFF, N, NO, IGNORE, or NOTFOUND), the OpenCV_Found pre-processor definition is omitted.

Upvotes: 2

Related Questions