I am defining some C++ class methods within a .cu file. This is basically a container class, but it does allocate some device memory and holds onto the pointers to that memory. I am using cuda-8.0 and Nsight in Eclipse. The essential code is below:
#include <string>
#include <fstream>
#include "Filter.h" // basic Filter class definitions
using namespace std;
//...
// After the constructor, the following method is defined:
int Filter::ReadFilter(string fileName)
{
int retVal = 0;
ifstream fileStream(fileName.c_str(), ifstream::binary | ifstream::beg | ifstream::in);
// do some other stuff here
return(retVal)
}
When I compile this, nvcc complains about the ifstream instantiation:
../Filter.cu(43): error: no instance of constructor "std::basic_ifstream<_CharT, _Traits>::basic_ifstream [with _CharT=char, _Traits=std::char_traits<char>]" matches the argument list
argument types are: (const char*, int)
If I put this code into a regular .cpp file, it compiles just fine. Am I doing something inherently wrong here by putting a class into a .cu file? As I understand it, nvcc should be calling the normal gcc or g++ under the covers here. Thanks for your help.
I get errors with that code when I compile with g++
$ cat t87.cu
#include <string>
#include <fstream>
using namespace std;
int test(string fileName)
{
int retVal = 0;
ifstream fileStream(fileName.c_str(), ifstream::binary | ifstream::beg | ifstream::in);
// do some other stuff here
return(retVal);
}
$ diff t87.cu t87.cpp
$ g++ -c t87.cpp
t87.cpp: In function ‘int test(std::string)’:
t87.cpp:11:91: error: invalid conversion from ‘int’ to ‘std::ios_base::openmode {aka std::_Ios_Openmode}’ [-fpermissive]
ifstream fileStream(fileName.c_str(), ifstream::binary | ifstream::beg | ifstream::in);
^
In file included from t87.cpp:2:0:
/usr/include/c++/4.8/fstream:467:7: error: initializing argument 2 of ‘std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]’ [-fpermissive]
basic_ifstream(const char* __s, ios_base::openmode __mode = ios_base::in)
^
$ nvcc -c t87.cu
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
t87.cu(11): error: no instance of constructor "std::basic_ifstream<_CharT, _Traits>::basic_ifstream [with _CharT=char, _Traits=std::char_traits<char>]" matches the argument list
argument types are: (const char *, int)
1 error detected in the compilation of "/tmp/tmpxft_00007e76_00000000-7_t87.cpp1.ii".
$ g++ --version
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -c t87.cpp -fpermissive
t87.cpp: In function ‘int test(std::string)’:
t87.cpp:11:91: warning: invalid conversion from ‘int’ to ‘std::ios_base::openmode {aka std::_Ios_Openmode}’ [-fpermissive]
ifstream fileStream(fileName.c_str(), ifstream::binary | ifstream::beg | ifstream::in);
^
$
I think that it is choking on ios::beg or ifstream::beg. If I take that out it works. I suppose it’s redundant anyway, but I have other code that works with ios::ate.