C++11 function iota() not supported by nvcc? - c++

I tried to compile this code:-
#include <vector>
using namespace std;
int main() {
vector<int> v(5);
iota(v.begin(), v.end(), 0);
}
And I compiled it with this command:-
D:\workspace\test>nvcc main.cpp --std=c++11
(Because without specifying the std I was getting the "identifier iota() not found" error)
And I get this error:-
nvcc warning : The -std=c++11 flag is not supported with the configured host compiler. Flag will be ignored.
main.cpp
main.cpp(7): error C3861: 'iota': identifier not found
How do I specify the C++ standard I want nvcc to use?
Also, compiling host code separately with g++ and device code with nvcc and then linking the objects with nvcc doesn't work. I get this.

I think you need to add #include <numeric>.
enter image description here

There is no need. By default the command line tool nvcc uses Microsoft's cl.exe. And if your cl.exe is updated, the std option is not available. cl.exe automatically supports all the latest C++ standard's features.
However, in cl.exe some of the functions like iota() aren't defined in the std namespace. Instead, iota() is defined in the numeric.h header file. So to run that code you would need to include the said header file. The final code should look like this:-
#include <vector>
#include <numeric.h>
using namespace std;
int main() {
vector<int> v(5);
iota(v.begin(), v.end(), 0);
}
The code can be compiled by the command:-
nvcc main.cpp

Related

'stoi' was not declared in this scope after using -std=c++11

Most probably this is weird, but when I got this error that stoi wasn't declared in this scope, I smiled because I am familiar with this error and it's solution.
I checked this option have g++ follow the c++11 ISO c++ language standard [-std=c++11] in compiler settings of Code Blocks (16.01, with MinGW) and tried recompiling it, but surprisingly it didn't work and the same error persisted. I tried re-installing CodeBlocks but that didn't work.
Also, I tried with windows power shell and command prompt with g++ math_handler.cpp -std=c++11 but got the same error.
What am I doing wrong?
the code is here:
#include<string>
using namespace std;
int main()
{
string body="456";
int i=stoi(body);
}
Note:
I tried with -std=c++0x and g++ too.
the same problem with to_string() function.
gcc version 4.9.2 (tdm -1)
Okay, I found that it is a known bug in MinGW bundled with CodeBlocks. I found the solution here.
Download mingw-to-string-gcc47.zip which contains three patched
header files. (Original patches: wchar.h, stdio.h, os_defines.h)
Copy wchar.h and stdio.h from the include directory in the zip file
to the following directory (overwrite): C:\mingw\include (replace
C:\mingw\ with the appropriate directory)
Copy os_defines.h to the following directory (overwrite):
C:\mingw\lib\gcc\mingw32\4.7.0\include\c++\mingw32\bits (replace
C:\mingw\ with the appropriate directory) (replace 4.7.0 with the
correct version number)
Did you include the required header file?
#include <string>
stoi is also in the std namespace so:
std::stoi()
or:
using namespace std;

compiling C++ code with intel compiler on Mac error: expected an identifier

My laptop can not compile a simple c++ code since yesterday, it works perfectly fine before.
The c++ code is can be a hello-world code in main.cpp file.
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
cout<<"Hello World"<<endl;
return 0;
}
I am trying to compile the code by
icpc main.cpp
The error information is
In file included from /Library/Developer/CommandLineTools/usr/include/c++/v1/algorithm(637),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/__string(56),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/string_view(171),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/string(470),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/__locale(15),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/ios(216),
from /Library/Developer/CommandLineTools/usr/include/c++/v1/iostream(38),
from main.cpp(1):
/Library/Developer/CommandLineTools/usr/include/c++/v1/type_traits(2065): error: expected an identifier
: public decltype((_VSTD::__is_assignable_test<_Tp, _Arg>(0))) {};
compilation aborted for main.cpp (code 2)
A few information:
I am using icpc (ICC) 17.0.4 20170411, it is installed from IntelĀ® Parallel Studio XE Composer Edition for C++ macOS.
My mac is MacBook Pro (15-inch, 2017), version 10.12.6.
If I use gnu compiler, it works fine. While my code needs to use intel's compiler.
The code works before, do not know while it becomes this. I have already tried restarting the systems.
======================================================================
Update1: The problem happened after I update my "Command Line Tools for Xcode". It looks like the /Library/Developer/CommandLineTools/usr/include/c++/ is not right.
======================================================================
Update2: This is can be solved by using icpc -std=c++11 main.cpp
However when I change my main.cpp to
#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
tuple<vector<int>, vector<int>, vector<int>>
getAllBlockMeanErrorTuple(const vector<int> &vec)
{
vector<int> fact, mean, err;
fact.resize( vec.size() );
mean.resize( vec.size() );
err.resize( vec.size() );
return make_tuple(fact, mean, err);
}
int main(int argc, char** argv)
{
cout<<"Hello World"<<endl;
return 0;
}
It has error again even if I use icpc -std=c++11 main.cpp
/Library/Developer/CommandLineTools/usr/include/c++/v1/__tuple(401): error: type name is not allowed
-> __all<typename enable_if<_Trait<_LArgs, _RArgs>::value, bool>::type{true}...>;
detected during:
I encountered the same issue while upgrading command line tools to the version of September 2017
While not finding a proper solution, I reinstalled previous version ( April 2017) of command line tools and it solved the problem (https://developer.apple.com/download/more/#).
I am looking forward to having a clean solution.
EDIT (5/12/17):
I solved the issue by recompiling everything using gcc. At compilation, Intel compilers will use the compiler that responds to gcc and g++ in the path. An installation with homebrew and some symlink in /usr/local/bin pushes the newly installed gcc in front of clang and then avoids gcc to change at each system update. Hope it helps.
Try to check that you are using right settings
and GNU is working because it automatically set to C++
try to set compiler to c++
hope this works.
OR You can use xcode to write c++ Code.

Why clang on Mac automatically includes some missing headers?

I noticed that clang++ includes a missing header - <limits> on Mac, while g++ shows errors about it on Linux. Now I wonder why clang does it, and gcc not. And how I can force clang to not do it.
Here is a sample code which compiles by clang on Mac, but not by gcc on Linux:
#include <iostream>
using namespace std;
int main()
{
cout << "int max: " << numeric_limits<int>::max() << endl;
}
UPD
I looked into libraries and here is what I found.
Internally <iostream> includes <istream>, which defines >> operator for different types. <istream> wants to know limits for short, int and streamsize types.
clang++ uses libc++ standard library, which uses std::numeric_limits class template from <limits> in <istream> for this purpose. That's why this header is included automatically when <iostream> is included.
g++ uses libstdc++ standard library, which uses __gnu_cxx::__numeric_traits class template from <ext/numeric_traits.h> instead of using <limits> in <istream> (<bits/istream.tcc>). There is also a comment in that header which explains why they don't use <limits>:
<limits> is big and we avoid including it
Used compilers:
> clang++ --version
Apple LLVM version 8.0.0 (clang-800.0.42.1)
$ g++ --version
g++ (Debian 4.9.2-10) 4.9.2
In C++, unlike C, standard headers are allowed to #include other standard headers. That sometimes leads to mysterious errors like the ones you're seeing: one compiler's <iostream> includes <limits> and the other doesn't. The solution is to always include the headers needed for whatever names you use. In this case that means to #include <limits> in your code, even though it compiles okay as is with one compiler. There's no harm in #include a header that's already been pulled in, so that's okay with both compilers. It's annoying sometimes, but that's just the way things are.
The clang version of the <iostream> header likely #includes the <limits> header, as such you get it automatically as part of #includeing the <iostream> header.
There's nothing you can do about it. That's how that compiler's library is implemented. You can simply add
#include <limits>
to this file, and it should compile on both platforms.

geany: C++ Including libraries and headers

I'm very new in Ubuntu and programming C++ on Ubuntu using Geany.
The problem I have here is that:
the classes i want to iclude to my project will receive an error,
I type,
#include <vector>
the error given here is,
fatal error: vector: No such file or directory
also I cannot use namespace std,
typing using namespace std returns the following error,
error: unknown type name 'using'
Here is the code:
#include <stdio.h> //no problem here
#include "stdlib.h" //no problem here
#include <vector> //this is a problem (lets say it returns error 1)
using namespace std; //this is a problem (lets say it returns error 2)
int main(int argc, char **argv)
{
return 0;
}
This sounds like you are using the wrong compiler to compile your C++ code. For example, by invoking gcc test.cpp the C++ file is actually compiled as C and you receive errors such as the one you posted - there is no vector header in C and there is also no using keyword.
If you are using gcc, the correct way to invoke the compiler to compile C++ is via the g++ symlink, i.e. g++ test.cpp
If you are using clang, the executable is called clang++ instead.
Both compilers support the -x parameter to manually change the language to C++, although in that case you also have to specify that the compiler needs to link your files with the C++ standard library. For example: gcc -x c++ test.cpp -lstdc++

Why g++ compiler is not able to find unique_ptr?

I am trying to compile a small C++ code which invloves unique_ptr as given below.
#include <iostream>
#include <memory>
using namespace std;
int main()
{
unique_ptr<int> p1(new int);
}
when I tried to compile the code using g++, it is throwing up 'unique_ptr' was not declared in this scope. I was trying to compile on Linux box. even I tried with '-std=c++11' option. It was saying 'unrecognized command line option -std=c++11'. Can any one please let me know how to fix this?
You need to include it, it comes out of the <memory> library
#include <memory>
According to the GCC 4.4 release notes, unique_ptr was not in GCC's standard C++ library before 4.4.
So you might want to check your GCC version first, using g++ --version like #40two said.