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.
CMakeLists.txtconfig that lets third party apps to inherit paths fromCMakeLists.txtin parent directories or something similar?" - What "paths" you are talking about? Variables likeOpenCV_DIRare inherited from the parent'sCMakeLists.txtautomatically. But those variables should be used by the inner project. E.g. byfind_package(and followingtarget_link_libraries), or bytarget_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.TPL1andTPL2CMakeLists.txt havefind_package(OpenCV 4.4 REQUIRED). But,TPL1needs to specifyset(OpenCV_DIR $ENV{OPENCV4_DIR})in its CMakeLists.txt for it to build successfully, whereasTPL2gets build successfully without needing to haveset(OpenCV_DIR $ENV{OPENCV4_DIR})in its CMakeLists.txt. SomehowTPL1is unable to inheritset(OpenCV_DIR $ENV{OPENCV4_DIR})from its parent'sCMakeLists.txtbutTPL2is able to inherit it from its parent'sCMakeLists.txt. Why this might be happening?OpenCV_DIRis modified by some other code in your project (you could use variable_watch command for debug such situation). Or it could be thatTPL2project is included intoPROJ2viaadd_subdirectorycommand, and that command is issued before setting the variable (set(OpenCV_DIR $ENV{OPENCV4_DIR})).