OpenCV - 仅使用g ++。不是gcc或者nvcc

时间:2011-09-26 14:45:30

标签: c++ c macos opencv cuda

我已经安装了OpenCV。 我已经能够编译一些代码,但有时它不起作用。以下示例不起作用。

#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-config --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-config --cflags --libs opencv)-m32
  • gcc Test.cpp $(pkg-config --cflags --libs opencv)-m64
  • nvcc Test.cpp $(pkg-config --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 ++没什么经验,所以有可能是显而易见的事情:)

2 个答案:

答案 0 :(得分:6)

要编译和链接C代码,您应该使用gcc

要编译和链接C++代码,您应该使用g++

尽管gcc通常可以正确地猜测编译文件的语言,但它通常无法链接到所需的库。

答案 1 :(得分:0)

我认为这些命令会起作用,因为它在我的电脑上工作(Ubuntu + OpenCV + Eclipse)

huynhngoctan @ ubuntu:〜/ workspace $ g ++ SiftDescriptor.cpp $(pkg-config --cflags --libs opencv)-lopencv_gpu

huynhngoctan @ ubuntu:〜/ workspace $ ./a.out box.png box_in_scene.png