gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:10:19 25 4
gpt4 key购买 nike

由于我需要尽可能优化视觉系统,对于机器人项目,我需要在将要运行的机器上编译 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

最佳答案

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)

关于c++ - 在构建时从源代码编译 OpenCV 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45511161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com