I had this problem too, trying to use OpenCV. I was able to work around it by rebuilding OpenCV with -stdlib=libstdc++, then adding a separate OpenCV wrapper class to my project that’s compiled separately with the ‘-x c++’ switch. Everything then links at the end without error. Here’s my (very basic) class, as an example.
MatWrapper.h:
#ifndef OPENCVWRAPPER_H_
#define OPENCVWRAPPER_H_
#include <string>
class MatWrapper {
public:
MatWrapper();
MatWrapper(unsigned char *,
unsigned int, unsigned int);
virtual ~MatWrapper();
void imread(std::string);
void imshow();
unsigned char *getData();
unsigned int getRows();
unsigned int getCols();
private:
unsigned char *data;
unsigned int nRows;
unsigned int nCols;
};
#endif /* OPENCVWRAPPER_H_ */
MatWrapper.cpp
#include "MatWrapper.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
/**
* Default constructor
*/
MatWrapper::MatWrapper() {
this->data = NULL;
this->nRows = 0;
this->nCols = 0;
}
/**
* Constructor that assigns private member variables
*/
MatWrapper::MatWrapper(unsigned char * _data,
unsigned int _nRows,
unsigned int _nCols) {
this->nRows = _nRows;
this->nCols = _nCols;
this->data = new unsigned char[nRows*nCols];
memcpy(this->data, _data, nRows*nCols);
}
/**
* Default destructor
*/
MatWrapper::~MatWrapper() {
delete [] this->data;
}
/**
* Wrapper for openCVs cv::imread method
*/
void MatWrapper::imread(std::string filename) {
cv::Mat image = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
this->nRows = image.rows;
this->nCols = image.cols;
this->data = new unsigned char[nRows*nCols];
memcpy(this->data, image.data, nRows*nCols);
}
/**
* Wrapper for openCVs cv::imshow method,
* with some added steps rolled in
*/
void MatWrapper::imshow() {
cv::Mat image(nRows, nCols, CV_8UC1, data);
cv::namedWindow("Display window", cv::WINDOW_AUTOSIZE);
cv::imshow("Display window", image);
cv::waitKey(0);
}
/**
* Getter for raw data contained in a cv::Mat object
*/
unsigned char *MatWrapper::getData() {
return this->data;
}
/**
* Getter for number of rows in a cv::Mat object
*/
unsigned int MatWrapper::getRows() {
return this->nRows;
}
/**
* Getter for number of columns in a cv::Mat object
*/
unsigned int MatWrapper::getCols() {
return this->nCols;
}
Here’s the output when I build my project in Nsight:
Building file: ../src/MatWrapper.cpp
Invoking: NVCC Compiler
/Developer/NVIDIA/CUDA-6.0/bin/nvcc -G -g -O0 -ccbin /usr/bin/clang -gencode arch=compute_30,code=sm_30 -odir "src" -M -o "src/MatWrapper.d" "../src/MatWrapper.cpp"
/Developer/NVIDIA/CUDA-6.0/bin/nvcc -G -g -O0 -ccbin /usr/bin/clang --compile -x c++ -o "src/MatWrapper.o" "../src/MatWrapper.cpp"
nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
Finished building: ../src/MatWrapper.cpp
Building file: ../src/OpenCVTest.cu
Invoking: NVCC Compiler
/Developer/NVIDIA/CUDA-6.0/bin/nvcc -G -g -O0 -ccbin /usr/bin/clang -gencode arch=compute_30,code=sm_30 -odir "src" -M -o "src/OpenCVTest.d" "../src/OpenCVTest.cu"
/Developer/NVIDIA/CUDA-6.0/bin/nvcc --compile -G -O0 -ccbin /usr/bin/clang -g -gencode arch=compute_30,code=compute_30 -gencode arch=compute_30,code=sm_30 -x cu -o "src/OpenCVTest.o" "../src/OpenCVTest.cu"
Finished building: ../src/OpenCVTest.cu
Building target: OpenCVTest
Invoking: NVCC Linker
/Developer/NVIDIA/CUDA-6.0/bin/nvcc --cudart static -L/usr/local/lib -ccbin /usr/bin/clang -link -o "OpenCVTest" ./src/MatWrapper.o ./src/OpenCVTest.o -lopencv_highgui -lopencv_core
nvcc warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
Finished building target: OpenCVTest
It’s not a perfect solution, but better than nothing. Hope this helps.