使用cmake将OpenCV用作外部的C ++项目

时间:2018-09-30 13:09:06

标签: c++ opencv cmake

我是cmake的新手,几天后不知道如何实现这一目标。

我正在尝试使用cmake构建依赖于OpenCV的C ++项目,但是我希望cmake像this一样克隆并安装它。我找到了一个project's CMakeFile用作参考来完成此操作。所以我有以下内容:

main.cpp:

#include <iostream>
#include <opencv2/opencv.hpp>


int main() {
    std::cout << "OpenCV Version: " + cv::getVersionString() << std::endl;
    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.12)
project(cv_playground)
set(CMAKE_CXX_STANDARD 14)

find_package(Git REQUIRED)

include(ExternalProject)

# OpenCV
set(OPENCV_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/opencv)
ExternalProject_Add(opencv
        GIT_REPOSITORY https://github.com/opencv/opencv
        GIT_TAG 68942affdbc4677aa845bc4307d4752182324a0e # 4.0.0-alpha
        SOURCE_DIR opencv
        BINARY_DIR opencv-build
        CMAKE_ARGS
          -DCMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_LOCATION}
        )
include_directories(${OPENCV_INSTALL_LOCATION}/include/opencv4)
link_directories(${OPENCV_INSTALL_LOCATION}/lib)

add_executable(cv_playground main.cpp)
add_dependencies(cv_playground opencv)
target_link_libraries(cv_playground opencv_core opencv_dnn opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs)

但是在构建项目时,我会得到很多未定义的引用(pthread,gz,dlopen / dlclose,itt_domain_create_ptr等)

我想知道如何修复那些undef引用,我已经安装了pthread,zlib等,但是我不知道如何制作cmake来使用它们。例如,我尝试将它们添加到target_link_libraries中,但仍然给我相同的错误:

CMakeLists.txt(请注意target_link_libraries中的CMAKE_DL_LIBS):

cmake_minimum_required(VERSION 3.12)
project(cv_playground)
set(CMAKE_CXX_STANDARD 14)
set(THREADS_PREFER_PTHREAD_FLAG ON)

find_package(Threads REQUIRED)
find_package(Git REQUIRED)

include(ExternalProject)

# OpenCV
set(OPENCV_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/opencv)
ExternalProject_Add(opencv
        GIT_REPOSITORY https://github.com/opencv/opencv
        GIT_TAG 68942affdbc4677aa845bc4307d4752182324a0e # 4.0.0-alpha
        SOURCE_DIR opencv
        BINARY_DIR opencv-build
        CMAKE_ARGS
          -DWITH_OPENGL=OFF
          -DCMAKE_INSTALL_PREFIX=${OPENCV_INSTALL_LOCATION}
        )
include_directories(${OPENCV_INSTALL_LOCATION}/include/opencv4)
link_directories(${OPENCV_INSTALL_LOCATION}/lib)

add_executable(cv_playground main.cpp)
add_dependencies(cv_playground opencv)
target_link_libraries(cv_playground Threads::Threads ${CMAKE_DL_LIBS} opencv_core opencv_dnn opencv_features2d opencv_flann opencv_highgui opencv_imgcodecs)

错误消息(仍然获得对dlopen,dlclose等的未定义引用)

[ 90%] Linking CXX executable cv_playground
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(system.cpp.o): In function `cv::TLSData<cv::(anonymous namespace)::ThreadID>::createDataInstance() const':
system.cpp:(.text._ZNK2cv7TLSDataINS_12_GLOBAL__N_18ThreadIDEE18createDataInstanceEv+0x37): undefined reference to `__itt_thread_set_name_ptr__3_0'
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(opencl_core.cpp.o): In function `GetHandle(char const*)':
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x7): undefined reference to `dlopen'
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x1e): undefined reference to `dlsym'
opencl_core.cpp:(.text._ZL9GetHandlePKc+0x53): undefined reference to `dlclose'
/home/syonekura/CLionProjects/cv_playground/cmake-build-debug/opencv/lib/libopencv_core.a(opencl_core.cpp.o): In function `opencl_check_fn(int)':
opencl_core.cpp:(.text._ZL15opencl_check_fni+0x3f): undefined reference to `dlsym'

1 个答案:

答案 0 :(得分:0)

所以我自己对此有很多问题。 Cmake本质上应仅用于构建OpenCV库。假设您要使用ubuntu机器,则要执行以下操作:

# Update and upgrade packages
sudo apt-get -y update
sudo apt-get -y upgrade

# Remove any existing versions of x264
sudo apt-get remove x264 libx264-dev

# Install OS Libraries

# Install Dependencies
sudo apt-get -y install build-essential checkinstall cmake pkg-config yasm gfortran git
sudo apt-get -y install libjpeg8-dev libjasper-dev libpng12-dev
# Used for Ubuntu 16.04
sudo apt-get -y install libtiff5-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get -y install libxine2-dev libv4l-dev
sudo apt-get -y install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get -y install libqt4-dev libgtk2.0-dev libtbb-dev
sudo apt-get -y install libatlas-base-dev
sudo apt-get -y install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get -y install libvorbis-dev libxvidcore-dev
sudo apt-get -y install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get -y install x264 v4l-utils

# Install Optional Dependencies
sudo apt-get -y install libprotobuf-dev protobuf-compiler
sudo apt-get -y install libgoogle-glog-dev libgflags-dev
sudo apt-get -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

# Download OpenCV from Github
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.3.0
cd ..

# Download opencv_contrib from Github
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.3.0
cd ..

# Compile and Install OpenCV with contrib
# Create build directory inside opencv directory
cd opencv
mkdir build
cd build

# Run CMake with the following options
cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -D BUILD_EXAMPLES=ON ..

# Compile and Install
make -j4
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

您可以挑选最相关的部分,但实际上这需要使OpenCV库可用于您的项目。然后,您可以使用制作文件来制作项目,也可以这样运行命令:

  

c ++ pkg-config --cflags opencv文件名。cpppkg-config --libs opencv -o文件名

这将创建一个名为filename的可执行文件