OpenCV 編譯錯誤 未定義的引用

2021-07-23 09:51:23 字數 3438 閱讀 1562

opencv安裝完成也有一陣子了,我才用的是編譯安裝,環境ubuntu16.04 lts。今天有時間想自己寫一點程式,卻發現編譯不過

**如下,開啟命令列中輸入的檔名並展示在視窗中

// openpicture.cpp

#include "opencv2/highgui/highgui.hpp"

int main(int argc, char** argv)

編譯的時候報錯,顯示所有呼叫過的cv函式都未定義

/tmp/cczabtdo.o:在函式『main』中:

openpic.cpp:(.text+0x32):對『cvloadimage』未定義的引用

openpic.cpp:(.text+0x45):對『cvnamedwindow』未定義的引用

openpic.cpp:(.text+0x56):對『cvshowimage』未定義的引用

openpic.cpp:(.text+0x60):對『cvwaitkey』未定義的引用

openpic.cpp:(.text+0x6c):對『cvreleaseimage』未定義的引用

openpic.cpp:(.text+0x76):對『cvdestroywindow』未定義的引用

collect2: error: ld returned 1

exit status

google一下,大概明白了是程式鏈結的時候找不到原始碼,下面開始解決問題

opencv在ubuntu下是通過呼叫早已編譯好的動態鏈結庫.so檔案來生成程式的,通過g++ 的編譯控制選項也許可以制定編譯器的查詢路徑

下面貼幾條g++的命令幫助

-i add to

theend

ofthe main include path

-l lib-path dir

於是我進入系統的/usr/local/lib檔案,找到了一下檔名

libopencv_calib3d.so        libopencv_ml.so

.2.4

.13libopencv_calib3d.so

.2.4 libopencv_nonfree.so

libopencv_calib3d.so

.2.4

.13 libopencv_nonfree.so

.2.4

libopencv_contrib.so libopencv_nonfree.so

.2.4

.13libopencv_contrib.so

.2.4 libopencv_objdetect.so

libopencv_contrib.so

.2.4

.13 libopencv_objdetect.so

.2.4

libopencv_core.so libopencv_objdetect.so

.2.4

.13libopencv_core.so

.2.4 libopencv_ocl.so

libopencv_core.so

.2.4

.13 libopencv_ocl.so

.2.4

libopencv_features2d.so libopencv_ocl.so

.2.4

.13libopencv_features2d.so

.2.4 libopencv_photo.so

libopencv_features2d.so

.2.4

.13 libopencv_photo.so

.2.4

libopencv_flann.so libopencv_photo.so

.2.4

.13libopencv_flann.so

.2.4 libopencv_stitching.so

libopencv_flann.so

.2.4

.13 libopencv_stitching.so

.2.4

libopencv_gpu.so libopencv_stitching.so

.2.4

.13libopencv_gpu.so

.2.4 libopencv_superres.so

libopencv_gpu.so

.2.4

.13 libopencv_superres.so

.2.4

libopencv_highgui.so libopencv_superres.so

.2.4

.13libopencv_highgui.so

.2.4 libopencv_ts.a

libopencv_highgui.so

.2.4

.13 libopencv_video.so

libopencv_imgproc.so libopencv_video.so

.2.4

libopencv_imgproc.so

.2.4 libopencv_video.so

.2.4

.13libopencv_imgproc.so

.2.4

.13 libopencv_videostab.so

libopencv_legacy.so libopencv_videostab.so

.2.4

libopencv_legacy.so

.2.4 libopencv_videostab.so

.2.4

.13libopencv_legacy.so

.2.4

.13 pkgconfig

libopencv_ml.so python2.7

libopencv_ml.so

.2.4 python3.5

好~到這裡,基本能夠確定opencv鏈結庫的所在位置

返回openpicture.cpp所在目錄,執行編譯命令

g++ -o openpicture openpicture.cpp -i /usr/local/include -i /usr/local/include/opencv -i /usr/local/include/opencv2 -l /usr/local/lib /usr/local/lib/*.so
鏈結完成之後,隨便照一張**放至程式目錄下(demo.jpg)

./openpicture demo.jpg
即可看到螢幕上的**輸出框

不得不說opencv在ubuntu下其實用起來還是很麻煩的,或許使用makefile會更好管理一些

不過這個我現在還不會,下次再說吧~

sophus編譯錯誤,未定義相關函式

視覺slam14講中,在編譯完sophus庫後,使用sudo make install安裝後,經過編譯發現錯誤,如下圖 這是因為安裝後打libsophus.so 沒有被cmake鏈結到 sophus libraries,因此出現這個錯誤。在cmakelists中新增如下語句 include dire...

pthread create報出未定義錯誤

在c 中包含了通檔案pthread.h,include 但還是報錯了沒有定義的錯誤 undefined reference to pthread create undefined reference to pthread join 問題原因 pthread 庫不是 linux 系統預設的庫,連線時需...

未定義的引用錯誤

在編譯工程時出現 librestapi.a rest harbor.o 在函式 rest harbor get sysinfo 中 rest harbor.c 145 對 harbor get sysinfo 未定義的引用 collect2 error ld returned 1 exit stat...