0

I have two projects PROJ in my workspace ws with corresponding third party libraries TPL as shown below. All depend on opencv 4.4.

ws   
├── PROJ1 (opencv 4.4)
|    └── TPL1 (opencv 4.4)  
└── PROJ2 (opencv 4.4)
    └── TPL2 (opencv 4.4)

PROJ1, PROJ2 and TPL1 have following lines in their CMakeLists.txt:

set(OpenCV_DIR $ENV{OPENCV4_DIR})
find_package(OpenCV 4.4 REQUIRED) 

Whereas TPL2 only have:

find_package(OpenCV 4.4 REQUIRED) 

but does not have

set(OpenCV_DIR $ENV{OPENCV4_DIR})

When I run cmake and make inside PROJ1/build and PROJ2/build they succeessbully get built. However, notice that PROJ2 gets built without TPL2 having set(OpenCV_DIR $ENV{OPENCV4_DIR}) specified in its CMakeLists.txt, but TPL1 needs to have this line for PROJ1 to build successfully. Otherwise it gives following error:

fatal error: opencv/cv.h: No such file or directory
26 | #include <opencv/cv.h>
   |          ^~~~~~~~~~~~~

Why is it so? Is there some CMakeLists.txt config that lets third party apps to inherit paths from CMakeLists.txt in parent directories or something similar?

PS: I do not do make install in any of these PROJs and TPLs.

3
  • "Is there some CMakeLists.txt config that lets third party apps to inherit paths from CMakeLists.txt in parent directories or something similar?" - What "paths" you are talking about? Variables like OpenCV_DIR are inherited from the parent's CMakeLists.txt automatically. But those variables should be used by the inner project. E.g. by find_package (and following target_link_libraries), or by target_include_directories, or by some other means. Please, provide more code of your projects (preferably in a form of minimal reproducible example). Currently provided code and error message are vague. Commented Oct 14, 2023 at 16:38
  • Ok I edited question to make it more clear. Both TPL1 and TPL2 CMakeLists.txt have find_package(OpenCV 4.4 REQUIRED) . But, TPL1 needs to specify set(OpenCV_DIR $ENV{OPENCV4_DIR}) in its CMakeLists.txt for it to build successfully, whereas TPL2 gets build successfully without needing to have set(OpenCV_DIR $ENV{OPENCV4_DIR}) in its CMakeLists.txt. Somehow TPL1 is unable to inherit set(OpenCV_DIR $ENV{OPENCV4_DIR}) from its parent's CMakeLists.txt but TPL2 is able to inherit it from its parent's CMakeLists.txt. Why this might be happening? Commented Oct 15, 2023 at 14:52
  • "Why this might be happening?" - Without viewing the code, we could only speculate about possible reasons. It could be that variable OpenCV_DIR is modified by some other code in your project (you could use variable_watch command for debug such situation). Or it could be that TPL2 project is included into PROJ2 via add_subdirectory command, and that command is issued before setting the variable (set(OpenCV_DIR $ENV{OPENCV4_DIR})). Commented Oct 15, 2023 at 15:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.