彻底根治opencv4中cblas没有定义的错误
最近在看人民邮电出版社的冯振的《OpenCV4快速入门》,在从源码一路编译完成OpenCV4.5的安装后,cmake问题不大,但是make时,总会出现:
1 make 2 [ 50%] Linking CXX executable videoCapture 3 /usr/bin/ld: /usr/local/lib/libopencv_core.so.4.5.0: undefined reference to `cblas_dgemm' 4 /usr/bin/ld: /usr/local/lib/libopencv_core.so.4.5.0: undefined reference to `cblas_cgemm' 5 /usr/bin/ld: /usr/local/lib/libopencv_core.so.4.5.0: undefined reference to `cblas_zgemm' 6 /usr/bin/ld: /usr/local/lib/libopencv_core.so.4.5.0: undefined reference to `cblas_sgemm' 7 collect2: 错误:ld 返回 1 8 make[2]: *** [CMakeFiles/videoCapture.dir/build.make:118:videoCapture] 错误 1 9 make[1]: *** [CMakeFiles/Makefile2:95:CMakeFiles/videoCapture.dir/all] 错误 2 10 make: *** [Makefile:103:all] 错误 2
看下代码videoCapture.cpp,来自书本上的代码,吐槽下,该书中的\和/不分,希望看同本书的同志注意下,反正我在linux下没有一次运行成功:
1 #include <opencv2/opencv.hpp> //#include <opencv2\opencv.hpp>原书的,实际时不对的,也许是我在linux下原因吧 2 #include <iostream> 3 4 using namespace std; 5 using namespace cv; 6 7 int main() 8 { 9 system("color F0"); //¸ 10 VideoCapture video("cup.mp4"); 11 if (video.isOpened()) 12 { 13 cout << "视频图形宽度=" << video.get(CAP_PROP_FRAME_WIDTH) << endl; 14 cout << "视频图形高度=" << video.get(CAP_PROP_FRAME_HEIGHT) << endl; 15 cout << "视频帧率=" << video.get(CAP_PROP_FPS) << endl; 16 cout << "视频的总帧数=" << video.get(CAP_PROP_FRAME_COUNT); 17 } 18 else 19 { 20 cout << "请确认视频文件名称是否正确·" << endl; 21 return -1; 22 } 23 while (1) 24 { 25 Mat frame; 26 video >> frame; 27 if (frame.empty()) 28 { 29 break; 30 } 31 imshow("video", frame); 32 waitKey(1000 / video.get(CAP_PROP_FPS)); 33 } 34 waitKey(); 35 return 0; 36 }
CMakeLists.txt
1 cmake_minimum_required(VERSION 2.6) 2 #创建工程 3 project(videoCapture) 4 #C++版本为 11 5 set(CMAKE_CXX_FLAGS "-std=c++11") 6 #set(CMAKE_EXE_LINKER_FLAGS "-lcblas") 7 8 find_package(OpenCV 4.1.0 REQUIRED) #找 OpenCV 4.0 安装路径 9 include_directories(${OpenCV_INCLUDE_DIRS}) #加载 OpenCV 4.0 的头文件 10 11 12 add_executable(videoCapture VideoCapture.cpp) #将程序生成可执行文件 13 target_link_libraries(videoCapture ${OpenCV_LIBS}) #链接 lib 文件到可执行文件中
当没有第6行时,按照常规方法:
mkdir build && cd build
cmake ..
make时总会出现文章开始的错误,原来的解决方法是在make时加上参数: -DCMAKE_EXE_LINKER_FLAGS=-lcblas,
后来觉得每次都要加上参数很不方便,就在CMakeLists.txt中加上第6行的内容后,编译的步骤终于成为了:
mkdir build
cd build
cmake ..
make
好了就可以了,大公告成
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。