2011-09-26 70 views
0

我已經安裝了OpenCV。 我已經能夠編譯一些代碼,但有時它不起作用。下面的例子不起作用。OpenCV - 只使用g ++。不是gcc或nvcc

#include <iostream> 
#include "opencv2/opencv.hpp" 
#include "opencv2/gpu/gpu.hpp" 

int main (int argc, char* argv[]) 
{ 
    try 
    { 
     cv::Mat src_host = cv::imread("building.jpg", CV_LOAD_IMAGE_GRAYSCALE); 
     cv::gpu::GpuMat dst, src; 
     src.upload(src_host); 

     cv::gpu::threshold(src, dst, 128.0, 255.0, CV_THRESH_BINARY); 

     cv::Mat result_host = dst; 
     cv::imshow("Result", result_host); 
     cv::waitKey(); 
    } 
    catch(const cv::Exception& ex) 
    { 
     std::cout << "Error: " << ex.what() << std::endl; 
    } 
    return 0; 
} 

我收到以下錯誤,當我與

GCC Test.cpp的$(pkg配置--cflags --libs OpenCV的)

gcc Test.cpp $(pkg-config --cflags --libs opencv) 
Undefined symbols for architecture x86_64: 
"std::allocator<char>::allocator()", referenced from: 
    _main in ccnzUIww.o 
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from: 
    _main in ccnzUIww.o 
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from: 
    _main in ccnzUIww.o 
"std::terminate()", referenced from: 
    _main in ccnzUIww.o 
"std::allocator<char>::~allocator()", referenced from: 
    _main in ccnzUIww.o 
"cv::gpu::GpuMat::upload(cv::Mat const&)", referenced from: 
    _main in ccnzUIww.o 
"cv::gpu::Stream::Null()", referenced from: 
    _main in ccnzUIww.o 
"cv::gpu::threshold(cv::gpu::GpuMat const&, cv::gpu::GpuMat&, double, double, int, cv::gpu::Stream&)", referenced from: 
    _main in ccnzUIww.o 
"cv::gpu::GpuMat::operator cv::Mat() const", referenced from: 
    _main in ccnzUIww.o 
"___cxa_begin_catch", referenced from: 
    _main in ccnzUIww.o 
"std::cout", referenced from: 
    _main in ccnzUIww.o 
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from: 
    _main in ccnzUIww.o 
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from: 
    _main in ccnzUIww.o 
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from: 
    _main in ccnzUIww.o 
"___cxa_end_catch", referenced from: 
    _main in ccnzUIww.o 
"std::ios_base::Init::Init()", referenced from: 
    __static_initialization_and_destruction_0(int, int)in ccnzUIww.o 
"std::ios_base::Init::~Init()", referenced from: 
    ___tcf_0 in ccnzUIww.o 
"cv::gpu::GpuMat::release()", referenced from: 
    cv::gpu::GpuMat::~GpuMat()in ccnzUIww.o 
"___gxx_personality_v0", referenced from: 
    Dwarf Exception Unwind Info (__eh_frame) in ccnzUIww.o 
ld: symbol(s) not found for architecture x86_64 
collect2: ld returned 1 exit status 

編譯我自己也嘗試:

  • GCC Test.cpp的$(pkg配置--cflags --libs OpenCV的)-m32
  • GCC Test.cpp的$(pkg配置--cflags --libs OpenCV的)-m64
  • NVCC Test.cpp的$(pkg配置--cflags --libs OpenCV的)

但是,這也給錯誤。我已經找到了stackoverflow上的答案,並發現這一點。 Compiling OpenCV CUDA program 在這個答案的解決方案是使用這個命令:

g++ Test.cpp `pkg-config --cflags --libs opencv` -lopencv_gpu 

和它的作品!我曾嘗試給gcc和nvcc編譯器提供相同的參數,但後來再次出現錯誤。這是一個問題,因爲我需要使用nvcc編譯器,因爲我想在CUDA項目中使用OpenCV。

我有C和C++的小經驗,所以有可能,這是明顯的東西:)

+6

OpenCV的是C++,你的例子程序,絕對是C++,*不* C.它編譯用C編譯器沒有意義。它可以用gcc來完成,但是它無緣無故地爲你完成額外的工作。 – Flexo

+0

謝謝!但我需要使用nvcc編譯器,因爲將來我可以在該文件中添加一些CUDA代碼。 –

+1

然後使用'nvcc'編譯並'g ++'鏈接。 – Flexo

回答

6

用於編譯和鏈接C代碼,你應該使用gcc

用於編譯和鏈接C++代碼,你應該使用g++

儘管gcc通常可以正確地猜出用什麼語言來編譯文件,但它通常不能鏈接到所需的庫。

+0

謝謝。這很明顯。但是CUDA支持C++,爲什麼nvcc不檢查這個。以及如何解決這個問題? ;) –

+0

@bobjink:我只能猜測,但我認爲nvcc和gcc一樣工作:它可以正確編譯,但不能鏈接。所以你應該使用nvcc來編譯('cc -c foo.cxx -o foo.o'),然後使用g ++來鏈接('g ++ -o app foo.o bar.o xxx.o') – PlasmaHH

+0

謝謝。現在我更瞭解它。我試過了: 1:nvcc -c Test.cpp -o Test.o 2:g ++ -o app Test.o'pkg-config --cflags --libs opencv' -lopencv_gpu但是出現錯誤:ld:warning:ignore文件Test.o,該文件是爲i386而建立的,它不是被鏈接的架構(x86_64),所以我猜它不起作用。 –

0

我認爲,這些命令將工作,因爲它在我的電腦工作(Ubuntu的+ OpenCV的+的Eclipse)

huynhngoctan @ Ubuntu的:〜/工作區$ G ++ SiftDescriptor.cpp $(pkg配置--cflags - -libs OpenCV的)-lopencv_gpu

huynhngoctan @ Ubuntu的:〜/工作區$ ./a.out box.png box_in_scene.png