I have found a strange behavior (probably an issue) while trying to compile a simple application with MS VC 15.3.1 (after applying VC 2017 Upgrade 3):
#include <iostream>
#include <algorithm>
#include <string>
// compiles if commenting out the line below
#include <vector>
#include <list>
class A
{
std::string s;
public:
A(const std::string& str) : s(str)
{
}
A(A&& other)
{
// compiles if changing std::swap<std::string>() to std::swap()
std::swap<std::string>(s, other.s);
}
};
int main(int argc, char *argv[])
{
return 0;
}
the compiler emits error :
1>d:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\vector(2131):
error C2039: '_Alloc': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
This code compiles without any issues with VC 15.2, VS2015 and VS2013 toolsets.
Related
I'm testing an antidebug solution with ptrace method; and i compile the program by using ndk21e cross-compile.
The problem is that it compiles successfully with gcc, but fails with ndk cross-compile.
ndk cross-compile compiles all other programs without problems
#include <stdlib.h>
#include <stdio.h>
#include<sys/ptrace.h>
#include <dlfcn.h>
#include <string.h>
int main(int argc, char **argv) {
void *handle;
long (*go)(enum __ptrace_request request, pid_t pid);
// get a handle to the library that contains 'ptrace'
handle = dlopen ("/lib/x86_64-linux-gnu/libc.so.6", RTLD_LAZY);
// reference to the dynamically-resolved function 'ptrace'
go = dlsym(handle, "ptrace");
if (go(PTRACE_TRACEME, 0) < 0) {
puts("being traced");
exit(1);
}
puts("not being traced");
// cleanup
dlclose(handle);
return 0;
}
And it shows the error like the picture as follow:
gcc compileresult and cross-compile error result
How can i solve this problem. Thanks.
I am using vs2012 and having the same problem. below is my sample source code.
#include "stdafx.h"
#include "iostream"
#include "list"
#include "algorithm"
#include "xutility"
using namespace std;
typedef struct Student{
string name;
int age;
int operator==(list<Student>::iterator itr)
{
return (!strcmp(itr->name.c_str(),this->name.c_str()));
}
}SStudent;
int _tmain(int argc, _TCHAR* argv[])
{
list<SStudent> myList;
SStudent temp1;
temp1.age = 10;
temp1.name = "aaaa";
myList.push_back(temp1);
list<SStudent>::iterator itr;
itr = std::find(myList.begin(), myList.end(), "aaaa");
return 0;
}
here is the error:
Error 1 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Student' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility 3186
Can I use URLOpenBlockingStream on win32 console application ?
I have a simple code:
#include "stdafx.h"
#include <urlmon.h>
#include <iostream>
using namespace std;
bool url(){
LPCWSTR pfile(NULL);
HRESULT hRez = URLDownloadToFile( NULL, _T("www.google.com"), (pfile), 0, NULL );
if( hRez == 0 ){
return 1;
// download ok
}
else{
return 0;// download failed
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<< url();
return 0;
}
and it returns compiler error
All outputs are up-to-date.
1>vvconsole.obj : error LNK2019: unresolved external symbol _URLDownloadToFileW#20 referenced in function "bool __cdecl url(void)" (?url##YA_NXZ)
1>C:\Users\Prem kumar Sekaran\Documents\Visual Studio 2010\Projects\vvconsole\Debug\vvconsole.exe : fatal error LNK1120: 1 unresolved externals
1>Build FAILED.
I am using built in libraries, Do I still need to link the .lib file ?
URLDownloadToFile() requires the header urlmon.h and urlmon.lib library. Add below line to the top of your code to link this library.
#pragma comment(lib, "urlmon.lib")
#include "stdafx.h"
#include <urlmon.h>
#include <iostream>
#pragma comment(lib, "urlmon")
using namespace std;
bool url(){
LPCWSTR pfile(NULL);
HRESULT hRez = URLDownloadToFile( NULL, _T("www.google.com"), (pfile), 0, NULL );
if( hRez == 0 ){
return 1;
// download ok
}
else{
return 0;// download failed
}
}
int _tmain(int argc, _TCHAR* argv[])
{
cout<< url();
return 0;
}
I am new to CUDA and I am getting a strange error. I want to print a string from a passed object and I get the error "calling host function from global function is not allowed" and I don't know why. But if I want to print an integer (changing get method to return sk1), everything works fine. Here is the code:
class Duomenys {
private:
string simb;
int sk1;
double sk2;
public:
__device__ __host__ Duomenys(void): simb(""), sk1(0), sk2(0.0) {}
__device__ __host__~Duomenys() {}
__device__ __host__ Duomenys::Duomenys(string simb1, int sk11, double sk21)
: simb(simb1), sk1(sk11), sk2(sk21) {}
__device__ __host__ string Duomenys::get(){
return simb;
}
};
And here I am calling Duomenys::get from __global__ function:
__global__ void Vec_add(Duomenys a) {
printf(" %s \n",a.get());
}
EDIT: I am trying to read data from a file and print it in a global function. In this code I am trying read all data and print just one object to see if everything works. This is the error I'm getting:
calling a __host__ function("std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string") from a __global__ function("Vec_add") is not allowed
Code:
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;
class Duomenys {
private:
string simb;
int sk1;
double sk2;
public:
__device__ __host__ Duomenys(void): simb(""), sk1(0), sk2(0.0) {}
__device__ __host__~Duomenys() {}
__device__ __host__ Duomenys::Duomenys(string simb1, int sk11, double sk21)
: simb(simb1), sk1(sk11), sk2(sk21) {}
__device__ __host__ string Duomenys::print()
{
stringstream ss;
ss << left << setw(10) << simb << setw(10) << sk1 << setw(10) << sk2;
return ss.str();
}
};
__global__ void Vec_add(Duomenys a) {
printf(" %s \n",a.print());
}
/* Host code */
int main(int argc, char* argv[]) {
setlocale (LC_ALL,"");
vector<Duomenys> vienas;
vector<vector<Duomenys>> visi;
//data reading to vector "vienas" (it works without any errors)
Duomenys *darr;
const size_t sz = size_t(2) * sizeof(Duomenys);
cudaMalloc((void**)&darr, sz);
Vec_add<<<1, 1>>>(visi[0].at(0));
cudaDeviceSynchronize();
cudaMemcpy(darr, &visi[0].at(0), sz, cudaMemcpyHostToDevice);
return 0;
}
Your problem is not with printf function, but with string data type. You cannot use the C++ string type in a kernel. See related question here: Can we use the string data type in C++ within kernels
Why would you pass a string object to printf when the %s format specifier is expecting something else? When I try to do that in ordinary host code, I get warnings about "passing non-POD types through ellipsis (call will abort at runtime)". Note that this problem has nothing to do with CUDA.
But beyond that issue, presumably you're getting string from the C++ standard library. (It's better if you show a complete reproducer code, then I don't have to guess at where you're getting things or what you are including.)
If I get string as follows:
#include <string>
using namespace std;
Then I am using a function defined in the C++ Standard Library. CUDA supports the C++ language (mostly) but does not necessarily support usage of C++ libraries (or C libraries, for that matter) in device code. Libraries are (usually) composed of (at least some) compiled code (such as allocators, in this case), and this code has been compiled for CPUs, not for the GPU. When you try to use such a CPU compiled routine (e.g. an allocator associated with the string class) in device code, the compiler will bark at you. If you include the complete error message in the question, it will be more obvious specifically what (compiled-for-the-host) function is actually the issue.
Use a standard C style string instead (i.e. char[] and you will be able to use it directly in printf.
EDIT: In response to a question in the comments, here is a modified version of the code posted that demonstrates how to use an ordinary C-style string (i.e. char[]) and print from it in device code.
#include <stdio.h>
#include <stdlib.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#define STRSZ 32
using namespace std;
class Duomenys {
private:
char simb[STRSZ];
int sk1;
double sk2;
public:
__device__ __host__ Duomenys(void): sk1(0), sk2(0.0) {}
__device__ __host__~Duomenys() {}
__device__ __host__ Duomenys(char *simb1, int sk11, double sk21)
: sk1(sk11), sk2(sk21) {}
__device__ __host__ char * print()
{
return simb;
}
__device__ __host__ void store_str(const char *str)
{
for (int i=0; i< STRSZ; i++)
simb[i] = str[i];
}
};
__global__ void Vec_add(Duomenys a) {
printf(" %s \n",a.print());
}
/* Host code */
int main(int argc, char* argv[]) {
string host_string("hello\n");
setlocale (LC_ALL,"");
vector<Duomenys> vienas(3);
vienas[0].store_str(host_string.c_str());
vector<vector<Duomenys> > visi(3);
visi[0] = vienas;
//data reading to vector "vienas" (it works without any errors)
Duomenys *darr;
const size_t sz = size_t(2) * sizeof(Duomenys);
cudaMalloc((void**)&darr, sz);
Vec_add<<<1, 1>>>(visi[0].at(0));
cudaDeviceSynchronize();
cudaMemcpy(darr, &(visi[0].at(0)), sz, cudaMemcpyHostToDevice);
return 0;
}
Note that I didn't try to understand your code or fix everything that looked strange to me. However this should demonstrate one possible approach.
I'm trying to compile some old codes in Visual C++ 6.0. The DSW file was missing, so I'm adding all the codes into a new workspace.
I have a.cpp as follows
#include "vld.h"
#include "afx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "b.h"
...
void function_1(char* string1)
{
char buf[2000];
strcpy_s(&buf[0], 2000, string1);
strcat_s(&buf[0], 2000, "_append");
...
}
There are other functions used such as fopen_s, strncpy_s, strncat_s in a.cpp as well.
And b.h is
#include <stdio.h>
#include <string.h>
char function_2(unsigned char * string2, int abc, int def)
{
char buf2[200];
sprintf_s(buf2, 200, "abcdef");
strcat_s(buf2, 200, "ghijkl");
}
Despite already having stdio.h and string.h, I still get the errors
'sprintf_s': undeclared identifier
'strcat_s': undeclared identifier
'strcpy_s': undeclared identifier
'fopen_s': undeclared identifier
'strncpy_s': undeclared identifier
'strncat_s': undeclared identifier
for both a.cpp and b.h.
Is there any missing settings that I've left out? Why am I getting these errors?
The _s version function were added to Visual Studio 2005. So you need use more modern Visual Studio version.