Raspbian Ubuntu下安裝OpenCV2.4.9的詳細過程及碰到的問題和第一次程式的編譯除錯
sudo ldconfig
****cmake 當中遇到的問題*****************Ubuntu 上cmake編譯OpenCV,遇到一個錯誤:
usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: 沒有那個檔案或目錄
很是奇怪,百度搜索了下,得知是GCC6的一個改動造成的。
自己的gcc是6.3.0
解決辦法:cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -DENABLE_PRECOMPILED_HEADERS=OFF
****make 當中遇到的問題*****************
1、/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘void cv::ChamferMatcher::Matching::computeDistanceTransform(cv::Mat&, cv::Mat&, cv::Mat&, float, float, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:969:30:error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address]
if (&annotate_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1016:34:error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address] if (&annotate_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘cv::ChamferMatcher::Match* cv::ChamferMatcher::Matching::localChamferDistance(cv::Point, cv::Mat&, cv::Mat&, cv::ChamferMatcher::Template*, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1111:25:error: the compiler can assume that the address of ‘orientation_img’ will never be NULL [-Werror=address] if (&orientation_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1111:5:warning: nonnull argument ‘orientation_img’ compared to NULL [-Wnonnull-compare] if (&orientation_img!=NULL) { ^~/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘void cv::ChamferMatcher::Matching::computeDistanceTransform(cv::Mat&, cv::Mat&, cv::Mat&, float, float, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1016:17:warning: nonnull argument ‘annotate_img’ compared to NULL [-Wnonnull-compare] if (&annotate_img!=NULL) { ^~/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:969:13:warning: nonnull argument ‘annotate_img’ compared to NULL [-Wnonnull-compare] if (&annotate_img!=NULL) { ^~cc1plus: some warnings being treated as errorsmodules/contrib/CMakeFiles/opencv_contrib.dir/build.make:158: recipe for target 'modules/contrib/CMakeFiles/opencv_contrib.dir/src/chamfermatching.cpp.o' failedmake[2]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/src/chamfermatching.cpp.o] Error1CMakeFiles/Makefile2:3195: recipe for target 'modules/contrib/CMakeFiles/opencv_contrib.dir/all' failedmake[1]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/all] Error2
解決辦法:
release/modules/contrib/CMakeFiles/opencv_contrib.dir/flags.make檔案 , 刪掉-Werror=address
,然後重新
$make
解決完上面的問題就完成了OpenCV的交叉編譯了。
接下來編譯第一個程式:
首先:配置環境
將opencv的庫加入到路徑,從而讓系統可以找到
sudo gedit /etc/ld.so.conf.d/opencv.conf
末尾加入/usr/local/lib,儲存退出
sudo ldconfig 使配置生效sudo gedit /etc/bash.bashrc
末尾加入
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfigexport PKG_CONFIG_PATH
儲存退出
sudo source /etc/bash.bashrc #使配置生效(該步驟可能會報錯找不到命令,原因是source為root命令
sudo passwd root(設定root密碼,如果之前沒有設定過)
輸入新密碼
su(進入root許可權)
輸入密碼
source /etc/bash.bashrc
exit(退出root)
)
sudo updatedb #更新database
g++ helloworld.cpp -o helloworld `pkg-config --cflags --libs opencv`(注意ESC下面的`,不是單引號)(注意後面的依賴庫)
編譯通過後再執行
./helloworld hello.jpg
即可看到圖片顯示,安裝完成。