I have workspace structure as shown below. It involves PROJ
for project, TPL
for third party library and DEP-PROJ
for dependency project (and ws
for workspace). I have also specified their dependencies inside brackets, next to them.
ws
├── opencv34
│ ├── opencv
│ └── opencv_contrib
├── opencv44
| ├── opencv
| └── opencv_contrib
├── DEP-PROJ (opencv 3.4)
└── PROJ (DEP-PROJ, opencv 4.4)
└── TPL (opencv 4.4)
I have built both versions of opencv with cmake ..
and make -j6
commands. But did not run make install
.
Next, DEP-PROJ
has following lines in its CMakeLists.txt (notice OPENCV3_DIR
):
set(OpenCV_DIR $ENV{OPENCV3_DIR})
find_package(OpenCV 3.4 REQUIRED)
Before doing cmake
in DEP-PROJ/build
directory, I run export OPENCV3_DIR=/ws/opencv34/opencv/build
, so that cmake
succeeds.
Next, PROJ
has following lines in its CMakeLists.txt (notice OPENCV4_DIR
):
set(OpenCV_DIR $ENV{OPENCV4_DIR})
find_package(OpenCV 4.4 REQUIRED)
Next, before doing cmake
in PROJ/build
directory, I run export OPENCV4_DIR=/ws/opencv34/opencv/build
, so that cmake
succeeds.
Before doing cmake
in PROJ/build
directory, I run export OPENCV4_DIR=/ws/opencv44/opencv/build
, so that cmake
succeeds.
This cause cmake to succeed, but make fails with error:
fatal error: opencv/cv.h: No such file or directory
It seems that doing make inside PROJ
rebuilds DEP-PROJ
. However, CMakeLists.txt of PROJ
sets OpenCV_DIR
to opencv44
directory. This makes DEP-PROJ
build fail giving above error.
To avoid this, I run make install
inside /ws/opencv34/opencv/build
. This lets DEP-PROJ
find opencv 3.4 from /usr
directory and PROJ
references opencv 4.4 from /ws/opencv44/opencv/build
directory leading to successfully building PROJ
.
However, I feel its a bit hacky way to resolve this (doing make install
in opencv 3.4 and not doing it in opencv 4.4). What is correct way to do this? How can I avoid rebuilding DEP-PROJ
while building PROJ
? or build it against /ws/opencv34/opencv/build
(instead of opencv in /usr
) along with building PROJ
against /ws/opencv44/opencv/build
?
PS: I am noob in cmake.
TL;DR: mixing multiple versions of a single library in the same process on UNIX doesn't work, just give up!
You appear to be building for UNIX.
Unlike on Windows, a UNIX shared library is not self-contained -- it is subject to symbol interposition and symbol collisions.
Also, OpenCV versions 3.4 and 4.4 define the same symbols, and are not ABI-compatible.
Taken together the two statements above mean that IF you manage to link your project dynamically with versions 3.4 and 4.4 of OpenCV, you are almost guaranteed to have inexplicable crashes, memory corruption, and more.
(If you try to link your project with archive versions instead, you will most likely fail with multiple symbol definition instead.)