I have the following code:
#include <type_traits>
int main()
{
}
g++ file.cc -std=c++0x
works just fine.
However, I need to use clang++ for some reason.
When I try
clang++ file.cc -std=c++0x
I get a bunch of errors:
In file included from file.cc:1:
In file included from /usr/include/c++/4.4.4/type_traits:50:
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:41: error: expected ')'
struct is_function<_Res(_ArgTypes......)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:28: note: to match this '('
struct is_function<_Res(_ArgTypes......)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:12: error: redefinition of 'is_function<type-parameter-0-0 (type-parameter-0-1, ...)>'
struct is_function<_Res(_ArgTypes......)>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.4.4/tr1_impl/type_traits:227:12: note: previous definition is here
struct is_function<_Res(_ArgTypes...)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:233:29: error: type qualifier is not allowed on this function
struct is_function<_Res(_ArgTypes...) const>
....
clang++ --version gives:
clang version 2.8 (branches/release_28)
Target: x86_64-redhat-linux-gnu
Thread model: posix
Any ideas how to fix that? (-std=c++11 doesn't work, not recognized.)
Clang 2.8 does not support C++11 features, you need to upgrade your compiler to use them.
Related
I'm new to C++ and read about curly bracket initializer which is available in C++ 11. I try to create a simple union which looks like this
union UExample {
UExample(const uint12_t value = 0) : raw{value} {}
uint12_t raw;
};
When I try to compile the file I get this error
./stack.h:18:57: error: expected member name or ';' after declaration specifiers
UBankedAddress(const uint12_t value = 0) : raw{value} {}
^
./stack.h:18:49: error: expected '('
UBankedAddress(const uint12_t value = 0) : raw{value} {}
^
./stack.h:18:55: error: expected ';' after expression
UBankedAddress(const uint12_t value = 0) : raw{value} {}
Dose anyone has an idea to solve this?
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I found a solution for this problem. It relates to the g++ compiler on MacOS. clang++ should be used instead.
Before (not working):
g++ stack.cpp -o stack
After (working):
clang++ -std=c++11 -stdlib=libc++ stack.cpp -o stack
Trying to compile the sqlpp17 codebase with gcc 8.2.1 and clang 6.0.1 have been a really strange experience. The code pushes the compilers to the limits and I hit probably a few compiler bugs in the meantime.
From the GCC Docs, [[maybe_unused]] is implemented since version 7, but if used this way:
struct foo {
foo([[maybe_unused]] bool thing1)
{
}
};
I hit this specific error:
<source>:2:9: error: expected unqualified-id before '[' token
foo([[maybe_unused]] bool thing1)
^
<source>:2:9: error: expected ')' before '[' token
foo([[maybe_unused]] bool thing1)
~^
)
Compiler returned: 1
Now, I know too little about C++17 to know if this error is correct, I know that clang 6 compiles that part fine (and fails somewhere else).
So, who's right, clang or gcc? (flags are -std=gnu++17 for both clang and gcc, generated by CMake)
This is a known bug in g++: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81429 G++ doesn't parse correctly [[maybe_unused]] attribute for first argument of the constructor.
I get an error when compile code containing <experimental/any>.
Code in main.cpp:
#include <experimental/any>
int main() { }
Compile this (clang version is 3.9):
clang++ main.cpp -o main -std=c++1z
Error after the compiling:
In file included from main.cpp:2:
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:364:34: error:
no template named '__any_caster'; did you mean 'any_cast'?
return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:361:30: note:
'any_cast' declared here
inline const _ValueType* any_cast(const any* __any) noexcept
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:372:34: error:
no template named '__any_caster'; did you mean 'any_cast'?
return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:369:24: note:
'any_cast' declared here
inline _ValueType* any_cast(any* __any) noexcept
^
2 errors generated.
As #chris mentioned in the comments:
You could try with libc++. Perhaps there's an incompatibility with Clang in libstdc++'s new header.
This turns out to be true. Clang 3.9 is still experimental, and so it uses experimental headers, including a experimental C++ standard library. By default, it is provided by GCC, and so an incompatibility occurs between the GCC implementation and the Clang implementation.
I try to compile the simple code
#include <atomic>
int bar = 0;
void foo(std::atomic<int>&flag)
{ bar = flag; }
with clang++ 3.2 (downloaded as llvm 3.2 from llvm.org; on mac os.x 10.8.3 this fails with the error
/> clang++ -std=c++11 -stdlib=libc++ -O3 -march=native -c test.cc
In file included from test.cc:1:
/usr/include/c++/v1/atomic:576:17: error: first argument to atomic operation must be a pointer to non-const _Atomic type ('const _Atomic(int) *' invalid)
{return __c11_atomic_load(&__a_, __m);}
^ ~~~~~
/usr/include/c++/v1/atomic:580:53: note: in instantiation of member function
'std::_1::_atomic_base::load' requested here
operator _Tp() const _NOEXCEPT {return load();}
^
test.cc:5:9: note: in instantiation of member function 'std::_1::_atomic_base::operator int' requested here
bar = done;
When I use /usr/bin/clang++ instead (which comes with the OS or Xcode) it compiles just fine. The libc++ is that at /usr/lib/c++/v1 in both cases.
What am I missing? Is there another libc++ that comes with llvm 3.2 but which I'm missing? (I cannot find anything in the clang3.2 tree).
Xcode now bundles libc++ within the Xcode.app directory. You can inspect this directory by control-clicking Xcode.app and choose "Show Package Contents".
I try to use std::this_thread::sleep_for() function but got the error
error: 'std::this_thread' has not been declared.
The flag _GLIBCXX_USE_NANOSLEEP included.
What else is needed to force it to work?
MinGW ==> gcc version 4.7.2 (GCC)
SSCCE:
#include<thread>
int main() {
std::this_thread::sleep_for(std::chrono::seconds(3));
}
command line:
g++ -D_GLIBCXX_USE_NANOSLEEP -std=gnu++0x ssce.cpp -o ssce.exe
result of compilation:
ssce.cpp: In function 'int main()':
ssce.cpp:4:8: error: 'std::this_thread' has not been declared
Use MinGW with POSIX threads, Luke.
http://sourceforge.net/projects/mingwbuilds/