OpenCV -仅与g++一起工作.不是gcc或nvcc
OpenCV - only working with g++. Not gcc or nvcc
我已经安装了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上寻找答案,发现了这个。编译OpenCV CUDA程序在这个答案中,解决方案是使用以下命令:
g++ Test.cpp `pkg-config --cflags --libs opencv` -lopencv_gpu
它有效!我试过给gcc和nvcc编译器同样的参数,但后来我又得到错误。这是一个问题,因为我必须使用nvcc编译器,因为我想在CUDA项目中使用OpenCV。
我几乎没有C和c++的经验,所以有可能这是显而易见的:)
要编译和链接C
代码,应该使用gcc
编译和链接C++
代码应该使用g++
虽然gcc
通常可以正确地猜出编译文件的语言,但它通常不能链接到所需的库。
我认为这些命令将工作,因为它在我的电脑(Ubuntu+OpenCV +Eclipse)工作
huynhngoctan@ubuntu:~/workspace$ g++ SiftDescriptor.cpp $(pkg-config——cflags——libs opencv) -lopencv_gpu
huynhngoctan@ubuntu: ~/工作区美元。/。Out box.png box_in_scene.png
相关文章:
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- 为什么对于gcc来说,"UNIX"和"unix"宏不是一回事?
- 为什么 gcc 告诉我我的模板不是模板?
- 模板模板参数导致Clang下的编译器错误,而不是GCC
- 强制 Clang 在 Linux 中使用 llvm 而不是 GCC
- C++ GCC 错误:"sqrtl"不是"std"的成员
- 是否有 GCC 警告,用于使用 C 库中的符号而不是通过命名空间 std?
- std::可选的默认构造函数不是 gcc 中的 constexpr?
- Gcc 使用 memcpy 作为隐式复制赋值运算符,而不是成员复制
- 如何强制 GCC 使用 jmp 指令而不是 ret?
- constexpr(但不是真正的)构造函数在GCC中编译,而不是在Clang中编译
- 在MacOS上使用gcc-8而不是clang编译Qt5
- 未定义的引用指针的变量模板在clang中功能,而不是GCC
- 模板中的友元函数(为什么这在Visual Studio中失败,而不是GCC和Clang)
- 为什么_largefile_source在用G 编译而不是GCC中定义了stdio.h
- "multiple definition of value"在 g++ 中编译具有未初始化全局但不是 gcc 的 C 程序时
- 在PowerPC编译器上删除功能,而不是GCC上的功能
- 'thread'不是GCC 4.8中'std'的成员
- OpenCV -仅与g++一起工作.不是gcc或nvcc
- 如何在编译时测试libstdc++的版本,而不是GCC