How to include and use openCV library when installed locally?

不打扰是莪最后的温柔 提交于 2019-12-24 20:21:40

问题


I want to install an OpenCV version locally on OSX in a folder without overwriting the old installation that I need for an old executable.

I followed [these instructions][1], particularly the Building OpenCV from Source Using CMake, Using the Command Line section.

So basically I:

  1. downloaded the source code with git and now it is in the subfolder openCV
  2. Created a new directory cmake_bin_dir
  3. Entered in cmake_bin_dir and created another subdirectory instDir
  4. From the cmake_bin_dir I launched the command cmake -D CMAKE_BUILD_TYPE=DEBUG -D CMAKE_INSTALL_PREFIX=/my/home/Downloads/openCVProject/cmake_bin_dir/instDir/ ../openCV/
  5. From the same directory I launched first make and then sudo make install. This created the *.dylib files in the cmake_bin_dir/instDir directory.

To test the installation I created my project directory (at the same level of cmake_bin_dir.

I tried to compile the file named test.cpp with the command:

    g++ -std=c++11 -I../cmake_bin_dir/ -I../openCV/include -I../openCV/modules/core/include/ \
-I../openCV/modules/calib3d/include/ -I../openCV/modules/features2d/include \
-I../openCV/modules/flann/include -I../openCV/modules/dnn/include -I../openCV/modules/highgui/include/ \
-I../openCV/modules/imgcodecs/include -I../openCV/modules/videoio/include -I../openCV/modules/imgproc/include \
-I../openCV/modules/ml/include -I../openCV/modules/objdetect/include -I../openCV/modules/photo/include \
-I../openCV/modules/shape/include -I../openCV/modules/stitching/include -I../openCV/modules/superres/include \
-I../openCV/modules/video/include -I../openCV/modules/videostab/include test.cpp -o test.o

I added the include one by one because I got compilation error each time. However now I getting a linkage error:

Undefined symbols for architecture x86_64:
  "cv::String::deallocate()", referenced from:
      cv::String::~String() in test-afd12e.o
      cv::String::operator=(cv::String const&) in test-afd12e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

So my question is how can I compile my program and other projects using the local installation of the OpenCV library? [1]: https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html


回答1:


Ok I found my error. If anybody will have the same problem try to execute this on terminal:

export DYLD_LIBRARY_PATH=../your/path/lib/:$DYLD_LIBRARY_PATH

In my case the I executed:

export DYLD_LIBRARY_PATH=../cmake_bin_dir/lib/:$DYLD_LIBRARY_PATH

I also created a Makefile with the following content:

CPP = g++ -std=c++11

# OpenCV trunk
CPPFLAGS = -L../cmake_bin_dir/lib/ \
       -I../cmake_bin_dir/include \
           -I../cmake_bin_dir/ -I../openCV/include -I../openCV/modules/core/include/ \
           -I../openCV/modules/calib3d/include/ \
           -I../openCV/modules/features2d/include \
           -I../openCV/modules/flann/include -I../openCV/modules/dnn/include -I../openCV/modules/highgui/include/ \
           -I../openCV/modules/imgcodecs/include -I../openCV/modules/videoio/include -I../openCV/modules/imgproc/include \
       -I../openCV/modules/ml/include -I../openCV/modules/objdetect/include -I../openCV/modules/photo/include \
       -I../openCV/modules/shape/include -I../openCV/modules/stitching/include -I../openCV/modules/superres/include \
       -I../openCV/modules/video/include -I../openCV/modules/videostab/include \
           `pkg-config --cflags --libs ../cmake_bin_dir/instDir/lib/pkgconfig/opencv.pc`

# Opencv 2.4.8
#CPPFLAGS = -L/home/krystof/libs/opencv-2.4.8/release/installed/libs \
       -I/home/krystof/libs/opencv-2.4.8/release/installed/include

all: test

test: test.cpp
    $(CPP) $(CPPFLAGS) $^ -o $@

Now executing make should build the program with the local openCV version.



来源:https://stackoverflow.com/questions/51651362/how-to-include-and-use-opencv-library-when-installed-locally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!