我已經安裝了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++的小經驗,所以有可能,這是明顯的東西:)
OpenCV的是C++,你的例子程序,絕對是C++,*不* C.它編譯用C編譯器沒有意義。它可以用gcc來完成,但是它無緣無故地爲你完成額外的工作。 – Flexo
謝謝!但我需要使用nvcc編譯器,因爲將來我可以在該文件中添加一些CUDA代碼。 –
然後使用'nvcc'編譯並'g ++'鏈接。 – Flexo