【问题标题】:OpenCV - only working with g++. Not gcc or nvccOpenCV - 仅使用 g++。不是 gcc 或 nvcc
【发布时间】:2011-11-25 07:02:52
【问题描述】:

我已经安装了 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 编译器提供相同的参数,但随后又出现错误。这是一个问题,因为我想在 CUDA 项目中使用 OpenCV,所以我必须使用 nvcc 编译器。

我对 C 和 C++ 的经验很少,所以有可能这是显而易见的 :)

【问题讨论】:

  • OpenCV 是 C++,你的示例程序肯定是 C++,不是 C。用 C 编译器编译它没有意义。可以使用 gcc 完成,但它会无缘无故地为您带来大量额外的工作。
  • 谢谢!但我需要使用 nvcc 编译器,因为将来我可以在该文件中添加一些 CUDA 代码。
  • 使用nvcc编译,然后g++链接。
  • 好的。我想我可以将 CUDA 代码添加到 CUDA 文件,将 C/C++ 代码添加到 C/C++ 文件,然后分别编译和链接它们。

标签: c++ c macos opencv cuda


【解决方案1】:

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

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

虽然gcc 通常可以正确猜出编译文件的语言,但它通常无法链接到所需的库。

【讨论】:

  • 谢谢。这很明显。但是 CUDA 支持 C++ 那么为什么 nvcc 不检查这个。以及如何解决这个问题? ;)
  • @bobjink:我只能猜测,但我认为 nvcc 在这里的工作方式与 gcc 相同:它可以正确编译,但不能链接。所以你应该使用 nvcc 来编译 (cc -c foo.cxx -o foo.o) 然后使用 g++ 链接 (g++ -o app foo.o bar.o xxx.o)
  • 谢谢。现在我更好地理解它了。我试过: 1:nvcc -c Test.cpp -o Test.o 2:g++ -o app Test.o pkg-config --cflags --libs opencv -lopencv_gpu 但得到错误:ld:警告:忽略文件 Test.o,文件是为 i386 构建的这不是被链接的架构(x86_64)所以我猜它不起作用。
  • 听起来你要么在 g++ 调用上需要 -m32,要么在 nvcc 行上需要 -m64。
【解决方案2】:

我认为这些命令会起作用,因为它在我的电脑上运行(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

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    相关资源
    最近更新 更多