Error while using CUDA and C++11 - c++

I am using CUDA 4.1 and GCC 4.5 ... (FINALLY! CUDA supports GCC 4.5, but still waiting for GCC 4.6). Anyways, is it possible to use C++11 with the CUDA 4.1?
I tried passing:
--compiler-options "-std=c++0x"
to nvcc and it throws a bunch of errors at me:
/usr/include/c++/4.5/exception_ptr.h(100): error: copy constructor for class "std::__exception_ptr::exception_ptr" may not have a parameter of type "std::__exception_ptr::exception_ptr"
/usr/include/c++/4.5/exception_ptr.h(100): error: expected a ")"
/usr/include/c++/4.5/exception_ptr.h(110): error: expected a ")"
/usr/include/c++/4.5/exception_ptr.h(132): error: identifier "type_info" is undefined
/usr/include/c++/4.5/exception_ptr.h(101): error: identifier "__o" is undefined
/usr/include/c++/4.5/exception_ptr.h(112): error: expected a ">"
/usr/include/c++/4.5/exception_ptr.h(112): error: identifier "__o" is undefined
/usr/include/c++/4.5/nested_exception.h(62): error: expected a ";"
/usr/include/c++/4.5/nested_exception.h(64): error: expected a ";"
/usr/include/c++/4.5/nested_exception.h(77): error: member function "std::nested_exception::~nested_exception" may not be redeclared outside its class
/usr/include/c++/4.5/nested_exception.h(77): error: function "std::<error>" may not be initialized
/usr/include/c++/4.5/nested_exception.h(77): error: expected an expression
/usr/include/c++/4.5/nested_exception.h(82): error: expected a ")"
/usr/include/c++/4.5/nested_exception.h(110): error: expected a ")"
/usr/include/c++/4.5/nested_exception.h(115): error: expected a ")"
/usr/include/c++/4.5/nested_exception.h(122): error: expected a ")"
/usr/include/c++/4.5/nested_exception.h(127): error: expected a ")"
/usr/include/c++/4.5/nested_exception.h(127): error: function template "std::__throw_with_nested" has already been defined
/usr/include/c++/4.5/bits/cpp_type_traits.h(180): error: identifier "char16_t" is undefined
/usr/include/c++/4.5/bits/cpp_type_traits.h(187): error: identifier "char32_t" is undefined
/usr/include/c++/4.5/bits/cpp_type_traits.h(187): error: class "std::__is_integer<<error-type>>" has already been defined
21 errors detected in the compilation of "/tmp/tmpxft_00000ef2_00000000-4_test_cuda.cpp1.ii".
Example test.cu
#include <cuda.h>
__host__ void test() {
// nothing in method
}
Compiles fine with:
nvcc -c -o test.o test.cu
But not with C++0x
nvcc -c -o test.o test.cu --compiler-options "-std=c++0x"

No, as of this answer, nvcc does not support c++11 constructs, even if they are supported by the host compiler.
The reason it isn't as simple as passing -std=c++0x to the host compiler is that nvcc has to parse the entirety of the code in order to split it into its __host__and __device__ halves. This preprocess happens before the host compiler is presented with any code at all, so nvcc's parser needs to be able to grok c++11 for it to work.

Update on #Jared Hoberock's answer based on another message he posted on Thrust's Google Group: it seems that C++11 support has been added to CUDA 6.5 (although it is still experimental and undocumented).
Dummy example: test.cu
#include <cuda.h>
#include <iostream>
__host__ void test() {
float a = 12.;
double b = 3.;
auto c = a * b;
std::cout << c << std::endl;
}
int main()
{
test();
return 0;
}
Compilation & execution
$ nvcc -std=c++11 test.cu -o test
$ ./test
36
Without -std=c++11, I get the following (expected) error:
test.cu(7): error: explicit type is missing ("int" assumed)
Note: this example may fail to compile with GCC 5.1.
Update
CUDA 7.0 officially introduced C++11 support:
CUDA 7 adds C++11 feature support to nvcc, the CUDA C++ compiler. This means that you can use C++11 features not only in your host code compiled with nvcc, but also in device code. New C++ language features include auto, lambda functions, variadic templates, static_assert, rvalue references, range-based for loops, and more. To enable C++11 support, pass the flag --std=c++11 to nvcc (this option is not required for Microsoft Visual Studio).

Related

C++ compiler errors compiling libpqxx basic exmaple, what is wrong and how to solve it?

im trying to build the example here https://github.com/jtv/libpqxx#programming-with-libpqxx using c++ compiler on my beagleboneblack, ececuting the command: g++ -o taks tasks.cpp -lpqxx -lpq
I belive i have installed the lpqxx library with success.
Gcc compiler version: gcc (Debian 8.3.0-6) 8.3.0
When compiling i get the following errors (and several other not show here):
warning: structured bindings only available with -std=c++17 or -std=gnu++17
for (auto [name, salary] : tx.query<std::string, int>(
^
tasks.cpp:20:43: error: ‘using work = class pqxx::transaction<>’ {aka ‘class pqxx::transaction<>’} has no member named ‘query’
for (auto [name, salary] : tx.query<std::string, int>(
^~~~~
tasks.cpp:20:60: error: expected primary-expression before ‘,’ token
The first is a warning, but the others error. Not familiar with C#:
What does these errors mean?
How do i get on solving them?
Is it compiler issues or is it code issues?
I just have one file in my direcory and that is copy-paste of the example from the link, am i missing some header files that define some stuff for me?

Unable to use aligned `operator new` in a module with Clang

I'm experimenting with Clang "modules" feature, and I'm trying to compile following piece of code:
export module a;
#include <new>
export void *foo()
{
return ::operator new(1, std::align_val_t(1));
}
export int main() {}
Try it live
When I tried clang++ -std=c++2a -pedantic-errors -fmodules-ts --precompile -x c++-module a.cpp -o a.pcm, I got
error: ISO C++ requires a definition in this translation unit for function 'operator new'
because its type does not have linkage [-Werror,-Wundefined-internal-type]
a.cpp:7:14: note: used here
return ::operator new(1, std::align_val_t(1));
^
1 error generated.
Removing -pedantic-errors fixes the error, but when I try to link the resulting module using clang++ -std=c++2a -fmodules-ts a.pcm -o a.exe, I get
Z:\Lander\msys2\tmp\a-cfaf65.o:a.pcm:(.text+0x10): undefined reference to
`_ZnwyW1aESt11align_val_t'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
It's especially annoying since <iostream> (indirectly) seems to rely on the aligned operator new, so I can't use it in modules too. As well as some other standard headers.
What's going on here?
It it's a Clang bug, how can I work around it?
My Clang is the latest version provided by MSYS2:
# clang++ --version
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-w64-windows-gnu
Thread model: posix
EDIT:
Filed a bug report, let's see what happens...
The standard library isn't part of your module a. So don't include the header after the export module a;. Include the header before that.

cross compile c++ on linux to windows

clang++ main.cpp -std=c++11 -target x86_64-unknown-win32 -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/
main.cpp:6:10: fatal error: 'string' file not found
#include <string>
^
1 error generated.
that's weird because:
/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/ssp$ ls
ssp.h stdio.h string.h unistd.h
and this result the same:
clang++ main.cpp -std=c++11 -target x86_64-unknown-win32 -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/ssp
well. I used to compile c code from linux to windows just fine using:
x86_64-w64-mingw32-gcc main.c
and I tried:
x86_64-w64-mingw32-g++ -std=c++0x main.cpp
also:
x86_64-w64-mingw32-g++ -std=c++0x main.cpp -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include
still give bunch of errors like
main.cpp:70:23: sorry, unimplemented: non-static data member initializers
main.cpp:70:23: error: in-class initialization of static data member ‘origin’ of non-literal type
main.cpp: In constructor ‘Item::Item(std::string)’:
main.cpp:83:18: error: ‘stoul’ is not a member of ‘std’
main.cpp:88:4: error: ‘origin’ was not declared in this scope
main.cpp:89:17: error: ‘stof’ is not a member of ‘std’
main.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Item&)’:
....
x86_64-w64-mingw32-g++ (GCC) 4.6.3 seems to be the latest version on ubuntu repo.
I'm at lost..why the hell clang won't use the headers. why the hell x86_64-w64-mingw32-g++ won't listen that it should use c++11 standards.
The GCC version you use does not support non-static data member initializers as shown here. Find out here which version of GCC supports the C++11 feature set you required.

Where is definition of std::function in clang++ (3.3/Xcode)

Problem Solved => see the update at the end
I'm trying to use std::function but it looks like just include <functional> does not provide the definition. I have tried to compile following code:
#include <functional>
std::function<int(int)> f = nullptr;
with c++11 as compile option:
% clang++ -c -std=c++11 t.cc
cause:
t.cc:3:6: error: no type named 'function' in namespace 'std'
std::function<int(int)> f = nullptr;
~~~~~^
t.cc:3:14: error: expected unqualified-id
std::function<int(int)> f = nullptr;
^
2 errors generated.
what am I missing? I know C++ well but new to clang++/C++11 thus I lack of important knowledge, I guess.
I'm using clang++ on MacOS X 10.8.
Update 1
I have tried a sample at cppreference.com site but it won't compile too. Giving some option solve the problem?
Update 2
Tried above sample from cppreference.com with clang++ -c -std=c++11 -stdlib=libc++11 x.cc, and compiler still says:
x.cc:1:10: fatal error: 'functional' file not found
#include <functional>
^
1 error generated.
Where is functional? I guess I should give -stdlib=libc++11 or whatever but it does not work too:
clang: error: invalid library name in argument '-stdlib=libc++11'
How I can find list of argument for -stdlib? (note: in man page, only available options are libc++ and libstdc++ both of them don't work)
Or functional just does not work?
This is not about the definition of the function. You don't have a linker error. You have a compiler error. The problem is, presumably, that the BSD/GNU/Darwin standard library installed in the real sysroot doesn't support C++11. You have to use the one that comes with Clang by specifying the -stdlib=libc++ compiler flag.
For C++11, it's best to always invoke clang as: clang++ -std=c++11 -stdlib=libc++
I use this most of the time, so I set the environment variable $CXX to this value. That way, I'm getting the dialect and library option in both compilation and linking. -std=c++11 is insufficient, as clang will still use the (old) system gcc headers in /usr/include/c++/4.2.1.
-stdlib=libc++ will use the clang headers in /usr/lib/c++/v1 such as <functional>.
There's a similar question with an answer by Howard Hinnant, who is (IIRC) an Apple engineer.

c++11: clang refuses numeric_limits<> in my template definition, while gcc accepts it - which is right?

The offending code:
template <class Bar,
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
size_t foo(Bar const& b) { omitted... }
It compiles fine on gcc 4.7.2 with -std=c++11. On clang 3.0 I get the following error:
foo.hpp:35:28: error: non-type template argument of type 'unsigned long' is not an integral constant expression
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As far as I can tell, I am supposed to be able to use numeric_limits in this way in c++11. Is clang wrong here, or am I unaware of something?
EDIT:
Compilation flags are: clang++ -o foo.o -c -W -Wall -Wextra -Werror -std=c++11 -stdlib=libc++ -g -I. foo.cpp
Your code compiles just fine with clang++ 3.2, see here.
I would say there is nothing wrong with your code but you should upgrade to a newer version of clang.
Note: The code doesn't compile with the Intel C++ Compiler 13.0.1 due to a compiler bug (thanks #Xeo):
Compilation finished with errors:
source.cpp(6): internal error: assertion failed: ensure_il_scope_exists: NULL IL scope (shared/cfe/edgcpfe/il.c, line 7439)
size_t MAX_SIZE = std::numeric_limits<size_t>::max()>
^
compilation aborted for source.cpp (code 4)
To use C++11 library features with clang you need to use the libc++ standard library implementation, otherwise you get the ancient library from GCC 4.1.2, which doesn't support C++11
See https://stackoverflow.com/a/14790442/981959 and https://stackoverflow.com/a/14150421/981959 and many other questions.