2016-11-10 66 views
0

我在Ubuntu上並且想要在自定義目錄上安裝不同版本的OpenCV(2.4.13)。我跟着這個教程在這裏:http://code.litomisky.com/2014/03/09/how-to-have-multiple-versions-of-the-same-library-side-by-side/OpenCV無法在自定義目錄中編譯

我不能得到這個簡單的main.cpp程序來編譯。我不能創建一個CV ::墊的形象,但我能獲得OpenCV的版本就好了!:

#include <iostream> 
#include <opencv2/core/version.hpp> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/imgproc/imgproc.hpp> 

int main(int argc, char ** argv) 
{ 
std::cout << "OpenCV version: " 
     << CV_MAJOR_VERSION << "." 
     << CV_MINOR_VERSION << "." 
     << CV_SUBMINOR_VERSION 
     << std::endl; 

cv::Mat image; //without this line, it works! 


return 0; 
} 

這裏是我的makefile:

CPP = g++ -std=c++0x 

# OpenCV 2.4.13 
CPPFLAGS = -L/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/lib \ 
      -I/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/include 


all: test 

test: main.cpp 
    $(CPP) $(CPPFLAGS) $^ -o [email protected] 

這是編譯器錯誤:

/tmp/ccyrdd7H.o: In function `cv::Mat::~Mat()': 
main.cpp:(.text._ZN2cv3MatD1Ev[cv::Mat::~Mat()]+0x39): undefined reference to `cv::fastFree(void*)' 
/tmp/ccyrdd7H.o: In function `cv::Mat::release()': 
main.cpp:(.text._ZN2cv3Mat7releaseEv[cv::Mat::release()]+0x47): undefined reference to `cv::Mat::deallocate()' 
collect2: ld returned 1 exit status 
make: *** [test] Error 1 

回答

1

您的Makefile是錯誤的。選項在鏈接時相關(但在編譯時爲而不是)。請參閱this & that答案和示例(與您的問題非常相似,並顯示您應該能夠適應的Makefile)。運行make -p以瞭解它們使用的內置規則和變量。

(我猜你是在Linux上)

瞭解更多關於ELFlinkers & dynamic linkersobject filesexecutables等。見Levine的Linkers and loaders書。

BTW,CV_MAJOR_VERSION etc ...可能是macro在某個標頭中定義的(由#include directives的一些提到)。但是cv::Mat可能是一些真正的C++類,帶有構造函數或鏈接時引用的vtable。也許你還應該閱讀一本關於C++的好書,例如: Programming -- Principles and Practice Using C++;但我們沒有時間和空間來解釋這裏的一切。

+0

嗨,感謝您的評論。爲什麼我可以用這個makefile獲得版本號,但是我不能用完全相同的makefile創建一個cv :: Mat? – user1431515

+0

你在說什麼'Makefile'? –

0

生成文件應該像這樣:

CPP = g++ -std=c++0x 

# OpenCV trunk 
CPPFLAGS = -L/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/lib -lopencv_core -lopencv_highgui -lopencv_imgproc \ 
      -I/home/myname/Desktop/myfolder/rewrite/opencv-2.4.13/release/installed/include 

all: test 

test: main.cpp 
    $(CPP) $(CPPFLAGS) $^ -o [email protected] 

的標誌-lopencv_core -lopencv_highgui -lopencv_imgproc人失蹤。

隨着opencv在不同目錄中的安裝,您可能會遇到無法訪問的共享庫錯誤。在這種情況下請按照這裏的說明:openCV program compile error "libopencv_core.so.2.4: cannot open shared object file: No such file or directory" in ubuntu 12.04