在构建时从源代码编译OpenCV 3

时间:2017-08-04 16:01:05

标签: c++ opencv cmake

由于我需要尽可能优化Vision系统,对于机器人项目,我需要在它将运行的机器上编译OpenCV 3。但我做错了什么,我似乎无法弄清楚为什么。 这是我的main.cpp(示例代码):

#include <opencv2/opencv.hpp>

int main( int argc, char** argv ) {

  cv::namedWindow( "Example 2-10", cv::WINDOW_AUTOSIZE );
  cv::VideoCapture cap;

  if (argc==1) {
    cap.open(0); // open the first camera
  } else {
    cap.open(argv[1]);
  }

  if( !cap.isOpened() ) { // check if we succeeded
    std::cerr << "Couldn't open capture." << std::endl;
    return -1;
  }

  cv::Mat frame;

  for(;;) {

    cap >> frame;

    if( frame.empty() ) break; // Ran out of film

    cv::imshow( "Example 2-10", frame );

    if( (char) cv::waitKey(33) >= 0 ) break;

  }

  return 0;

}

然后我的CMakesLists.txt:

cmake_minimum_required(VERSION 3.8)
project(Vision)

set(CMAKE_CXX_STANDARD 17)

include(ExternalProject)

set(EXTERNAL_INSTALL_LOCATION ${CMAKE_BINARY_DIR}/opencv)

ExternalProject_Add(opencv
        GIT_REPOSITORY https://github.com/opencv/opencv
        CMAKE_ARGS -DCMAKE_BUILD_TYPE=Debug -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION}
        )

include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)

set(SOURCE_FILES main.cpp)
add_executable(erl_vision ${SOURCE_FILES})
add_dependencies(erl_vision opencv)
target_link_libraries( erl_vision )

然后我面临的错误:

/bin/cmake --build /home/gpereira/Documents/Projects/Vision/cmake-build-debug --target erl_vision -- -j 4
[ 50%] Linking CXX executable erl_vision
CMakeFiles/erl_vision.dir/main.cpp.o: In function `main':
/home/gpereira/Documents/Projects/Vision/main.cpp:7: undefined reference to `cv::namedWindow(cv::String const&, int)'
/home/gpereira/Documents/Projects/Vision/main.cpp:8: undefined reference to `cv::VideoCapture::VideoCapture()'
/home/gpereira/Documents/Projects/Vision/main.cpp:11: undefined reference to `cv::VideoCapture::open(int)'
/home/gpereira/Documents/Projects/Vision/main.cpp:13: undefined reference to `cv::VideoCapture::open(cv::String const&)'
/home/gpereira/Documents/Projects/Vision/main.cpp:16: undefined reference to `cv::VideoCapture::isOpened() const'
/home/gpereira/Documents/Projects/Vision/main.cpp:25: undefined reference to `cv::VideoCapture::operator>>(cv::Mat&)'
/home/gpereira/Documents/Projects/Vision/main.cpp:29: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
/home/gpereira/Documents/Projects/Vision/main.cpp:31: undefined reference to `cv::waitKey(int)'
/home/gpereira/Documents/Projects/Vision/main.cpp:8: undefined reference to `cv::VideoCapture::~VideoCapture()'
/home/gpereira/Documents/Projects/Vision/main.cpp:8: undefined reference to `cv::VideoCapture::~VideoCapture()'
CMakeFiles/erl_vision.dir/main.cpp.o: In function `cv::String::String(char const*)':
/home/gpereira/Documents/Projects/Vision/cmake-build-debug/opencv/include/opencv2/core/cvstd.hpp:601: undefined reference to `cv::String::allocate(unsigned long)'
CMakeFiles/erl_vision.dir/main.cpp.o: In function `cv::String::~String()':
/home/gpereira/Documents/Projects/Vision/cmake-build-debug/opencv/include/opencv2/core/cvstd.hpp:647: undefined reference to `cv::String::deallocate()'
CMakeFiles/erl_vision.dir/main.cpp.o: In function `cv::String::operator=(cv::String const&)':
/home/gpereira/Documents/Projects/Vision/cmake-build-debug/opencv/include/opencv2/core/cvstd.hpp:655: undefined reference to `cv::String::deallocate()'
CMakeFiles/erl_vision.dir/main.cpp.o: In function `cv::Mat::~Mat()':
/home/gpereira/Documents/Projects/Vision/cmake-build-debug/opencv/include/opencv2/core/mat.inl.hpp:692: undefined reference to `cv::fastFree(void*)'
CMakeFiles/erl_vision.dir/main.cpp.o: In function `cv::Mat::release()':
/home/gpereira/Documents/Projects/Vision/cmake-build-debug/opencv/include/opencv2/core/mat.inl.hpp:804: undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/erl_vision.dir/build.make:95: erl_vision] Error 1
make[2]: *** [CMakeFiles/Makefile2:105: CMakeFiles/erl_vision.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:117: CMakeFiles/erl_vision.dir/rule] Error 2
make: *** [Makefile:131: erl_vision] Error 2

2 个答案:

答案 0 :(得分:2)

https://github.com/InsightSoftwareConsortium/ITKExamples/blob/master/Superbuild/External-OpenCV.cmake

#---------------------------------------------------------------------------
# Get and build OpenCV

if( NOT OpenCV_TAG )
  set( OpenCV_TAG "3.1.0" )
endif()

ExternalProject_Add(OpenCV
  GIT_REPOSITORY "https://github.com/opencv/opencv.git"
  GIT_TAG "${OpenCV_TAG}"
  SOURCE_DIR opencv
  BINARY_DIR opencv-build
  CMAKE_GENERATOR ${gen}
  CMAKE_ARGS
    ${ep_common_args}
    -DBUILD_DOCS:BOOL=FALSE
    -DBUILD_EXAMPLES:BOOL=FALSE
    -DBUILD_TESTS:BOOL=FALSE
    -DBUILD_SHARED_LIBS:BOOL=TRUE
    -DWITH_CUDA:BOOL=FALSE
    -DWITH_FFMPEG:BOOL=FALSE
    -DBUILD_PERF_TESTS:BOOL=FALSE
  INSTALL_DIR "${OPENCV_INSTALL_PATH}"
)

set(OpenCV_DIR ${CMAKE_BINARY_DIR}/opencv-build)

答案 1 :(得分:0)

1)您的输出不是编译OpenCV3的输出。您正在编译erl_vision.dir中的代码,而系统上没有OpenCV,并且您没有正确安装您尝试下载的代码(您只有标题,但代码丢失导致链接错误)。 我建议你先构建并安装OpenCV。

2)不,如果您从源代码构建它,您的OpenCV将不会是最优化的。它可能只是更新,但它不会让你在性能方面有所作为。 由于您使用的是Linux,我建议您通过软件包管理器安装OpenCV(apt-get install opencv *或yum install opencv *)。然后,您需要做的就是使用-lopencv-链接所需的模块,或者使用pkg-config。