2014-12-07 106 views
0

我試圖編譯和鏈接,我發現下面的OpenCV程序在線:在Ubuntu 14.04上從源代碼編譯時如何鏈接到opencv 3.0?

main.cpp中:

#include <opencv2/highgui.hpp> 
#include <opencv2/imgproc.hpp> 
#include <opencv2/videoio.hpp> 

#include <iostream> 
#include <stdio.h> 

using namespace std; 
using namespace cv; 
int main(int argc, const char** argv) 
{ 
     VideoCapture capture(0); 

     while(capture.isOpened()) 
     { 
       Mat frame; 
       if (! capture.read(frame)) 
         continue; 

       imshow("lalala", frame); 
       if(waitKey(10) == 27) 
         break; 
     } 



     return 0; 
} 

我filestructure如下:

〜的/ dev/opencv- 1

〜的/ dev/OpenCV的-示例

OpenCV的-1是一個git克隆:https://github.com/Itseez/opencv

我跟着位於中的說明:http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_install/linux_install.html

我的目標是要調試到OpenCV的代碼,所以我用了CMAKE_BUILD_TYPE =調試

我能得到OpenCV的代碼編譯sucessfuly。我寫了下面的makefile編譯這個樣本:

INCPATH =-I../opencv-1/modules/objdetect/include \ 
    -I../opencv-1/modules/highgui/include \ 
    -I../opencv-1/modules/imgproc/include \ 
    -I../opencv-1/modules/core/include \ 
    -I../opencv-1/modules/imgcodecs/include \ 
    -I../opencv-1/modules/videoio/include \ 

LIBPATH =-L../opencv-1/debug/lib 

LIBS =-lopencv_core -lopencv_videoio -lopencv_highgui -lopencv_imgproc -lopencv_video -lopencv_objdetect 

%:%.cpp 
     g++ $(INCPATH) $(LIBS) $(LIBPATH) $^ -o [email protected] 

編譯我跑:使主

發生這種情況時,我得到以下鏈接錯誤:

g++ -I../opencv-1/modules/objdetect/include -I../opencv-1/modules/highgui/include -I../opencv-1/module                s/imgproc/include -I../opencv-1/modules/core/include -I../opencv-1/modules/imgcodecs/include -I../open                cv-1/modules/videoio/include -lopencv_core -lopencv_videoio -lopencv_highgui -lopencv_imgproc -lopenc                v_video -lopencv_objdetect -L../opencv-1/debug/lib main.cpp -o main 
/tmp/cc0PMDdW.o: In function `main': 
main.cpp:(.text+0x29): undefined reference to `cv::VideoCapture::VideoCapture(int)' 
main.cpp:(.text+0x69): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)' 
main.cpp:(.text+0xce): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)' 
main.cpp:(.text+0xf6): undefined reference to `cv::waitKey(int)' 
main.cpp:(.text+0x131): undefined reference to `cv::VideoCapture::isOpened() const' 
main.cpp:(.text+0x14d): undefined reference to `cv::VideoCapture::~VideoCapture()' 
main.cpp:(.text+0x1b0): undefined reference to `cv::VideoCapture::~VideoCapture()' 
/tmp/cc0PMDdW.o: In function `cv::String::String(char const*)': 
main.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4f): undefined reference to `cv::String::al                locate(unsigned long)' 
/tmp/cc0PMDdW.o: In function `cv::String::~String()': 
main.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallo                cate()' 
/tmp/cc0PMDdW.o: In function `cv::_InputArray::_InputArray()': 
main.cpp:(.text._ZN2cv11_InputArrayC2Ev[_ZN2cv11_InputArrayC5Ev]+0x13): undefined reference to `vtable                for cv::_InputArray' 
/tmp/cc0PMDdW.o: In function `cv::_InputArray::_InputArray(cv::Mat const&)': 
main.cpp:(.text._ZN2cv11_InputArrayC2ERKNS_3MatE[_ZN2cv11_InputArrayC5ERKNS_3MatE]+0x17): undefined re                ference to `vtable for cv::_InputArray' 
/tmp/cc0PMDdW.o: In function `cv::_InputArray::~_InputArray()': 
main.cpp:(.text._ZN2cv11_InputArrayD2Ev[_ZN2cv11_InputArrayD5Ev]+0x13): undefined reference to `vtable                for cv::_InputArray' 
/tmp/cc0PMDdW.o: In function `cv::_OutputArray::_OutputArray(cv::Mat&)': 
main.cpp:(.text._ZN2cv12_OutputArrayC2ERNS_3MatE[_ZN2cv12_OutputArrayC5ERNS_3MatE]+0x23): undefined re                ference to `vtable for cv::_OutputArray' 
/tmp/cc0PMDdW.o: In function `cv::_OutputArray::~_OutputArray()': 
main.cpp:(.text._ZN2cv12_OutputArrayD2Ev[_ZN2cv12_OutputArrayD5Ev]+0x13): undefined reference to `vtab                le for cv::_OutputArray' 
/tmp/cc0PMDdW.o: In function `cv::Mat::~Mat()': 
main.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)' 
/tmp/cc0PMDdW.o: In function `cv::Mat::release()': 
main.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::dea                llocate()' 
collect2: error: ld returned 1 exit status 
make: *** [main] Error 1 

我不是確定我錯過了什麼。我已經包含了這個簡單程序需要編譯的所有庫。我在網上搜索了類似的錯誤,並且我看到的所有答案都建議添加已經存在於我的makefile中的庫。

+0

還將'g ++ $(INCPATH)$(LIBS)$(LIBPATH)$^-o $ @'重寫爲$(CXX)$(INCPATH)$(LIBPATH)$^$(LIBS)-o $ @ '。 – Chnossos 2014-12-07 21:57:02

+0

@berak,我很困惑。我以爲我用我的LIB_PATH變量來做這件事。 – lordoku 2014-12-08 01:05:33

+0

@Chnossos非常感謝。這似乎已經成功了。我要回答你的答案併爲這個問題創建一個答案。 – lordoku 2014-12-08 17:06:13

回答

1

你的問題是,這條線:

g++ $(INCPATH) $(LIBS) $(LIBPATH) $^ -o [email protected] 

應重新寫成這樣:

$(CXX) $(INCPATH) $(LIBPATH) $^ $(LIBS) -o [email protected] 

-l標誌的變量應該總是去.cpp.o文件命令行。


但是你可以做得更好。你的Makefile應該看起來像這樣:

CPPFLAGS := -I../opencv-1/modules/objdetect/include \ 
       -I../opencv-1/modules/highgui/include \ 
       -I../opencv-1/modules/imgproc/include \ 
       -I../opencv-1/modules/core/include \ 
       -I../opencv-1/modules/imgcodecs/include \ 
       -I../opencv-1/modules/videoio/include 

LDFLAGS  := -L../opencv-1/debug/lib 

LDLIBS  := -lopencv_core -lopencv_videoio -lopencv_highgui \ 
       -lopencv_imgproc -lopencv_video -lopencv_objdetect 

沒有更多,沒什麼。 GNU Make已經有一個隱含的規則來編譯一個.cpp文件到一個可執行文件中,只需通過使用內置變量來依賴它。

注意CPPFLAGS表示預處理器標誌,而不是C++標誌。使用CXXFLAGS來提供標誌,例如-std-g標誌。