问题
SHORT VERSION
I want to bundle the (uncompiled) source files for OpenCV and librealsense2 with CMake so I can cross compile it on RPi (or at all), but I am new to CMake and don't know how.
add_executable (CMakeRealSense "CMakeRealSense.cpp" "CMakeRealSense.h")
include_directories(opencv libs/opencv-master/include)
include_directories(realsense2 libs/librealsense-master/include)
This gives a bunch of errors related to RS files itself (X not declared on scope).
LONG VERSION
I am currently attempting to write a program for the Intel RealSense camera d435. There are two libraries I am using. OpenCV and librealsense2 (actual library for the camera). I first used VCPKG to load the packages in, and that works for my local machine, but I can't properly compile it on the RPi 3. I decided that bundling source files with the project would be good idea, but I can't get it to work. I'm getting one error after another.
EDIT 1
When I compile on my local windows machine I get error Cannot open include file: 'opencv2/opencv_modules.hpp': No such file or directory
. And, on my pi it gives a bunch of errors related to RealSense files themselves (didn't check them all, there where 780 of them)
EDIT 3
As pointed out by @squareskittles. I fixed the issue relating to my local windows version, but for the pi I still have issues with new errors.
From what I can tell the error states that it can't find the opengl_config.cmake
folder in folders tools
, src
and examples
(examples
will be removed down the line so not much of a issue). Last error is
CMake Error at libs/librealsense-master/CMake/install_config.cmake:4 (add_custom_target):
add_custom_target cannot create target "uninstall" because another target
with the same name already exists. The existing target is a custom target
created in source directory
"/home/pi/.vs/CMakeRealSense/d6128cea-ef93-4c7b-83bb-89ee6315963f/src/libs/opencv-master".
See documentation for policy CMP0002 for more details.
EDIT 4
So I firstly just added the cmake folder containing the install config and that works how, the 2nd error how ever seems to be that both librealsense
and opencv
have the same name in the uninstall config. Can the add_custom_target
for uninstall be safely changed without effecting the rest of the cmake building process?
EDIT 5
It succesfully compiles the realsense2 libraries (as far as I can tell) but it cant open the opencv2/core.hpp
source file. Not sure why it cant find it.
EDIT 6
So I did ALOT more digging and aperently opencv uses a header file to check which modules your using, problem is it doesnt properly generate one. I am not sure how to do this but ill do some more digging to find out
回答1:
If you want to include the OpenCV and RealSense2 source in your CMake project directly, you should use the add_subdirectory() command to tell CMake to look in each of the directories. Be sure to provide the path to the top-level CMakeLists.txt file in each of the other source repositories, or they will not be configured completely:
add_subdirectory(libs/opencv-master)
add_subdirectory(libs/librealsense-master)
Later on in your CMakeLists.txt file, you need to make use of the CMake variables and targets defined in each of these sub-repositories in order to include each of these libraries in your executable. For example:
# Define your CMake executable target.
add_executable(CMakeRealSense CMakeRealSense.cpp CMakeRealSense.h)
# Link the OpenCV components and RealSense targets to your executable target.
target_link_libraries(CMakeRealSense PRIVATE opencv_core realsense2)
You may also have to link additional OpenCV components, such as opencv_imgproc
, opencv_dnn
, opencv_imgcodecs
, opencv_videoio
, opencv_highgui
, etc, depending on what OpenCV functionality you are using.
来源:https://stackoverflow.com/questions/61389563/how-do-i-include-realsense2-opencv-source-into-compilation-with-cmake