clang 3.2 fails on std::atomic -- a libc++ issue? - c++

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".

Related

Intel-Compiler fails C++14-check with `attribute "__malloc__" does not take arguments`

I have the following code (intended to detect if the compiler supports C++14):
#include <memory>
#include <algorithm>
// Check the version language macro, but skip MSVC because
// MSVC reports 199711 even in MSVC 2017.
#if __cplusplus < 201402L && !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#error "insufficient support for C++14"
#endif
int main()
{
auto ptr = std::make_unique<int>(42);
constexpr int max = std::max(0, 1);
(void) ptr;
(void) max;
return 0;
}
When compiling it with g++ (version 11.2.1) and the line g++ -std=c++14 test.cpp -o test it works fine. When compiling it with the intel compiler (version 2021.3.0 (gcc version 11.2.1 compatibility)) instead using icpc -std=c++14 test.cpp -o test, it fails with
In file included from /usr/include/c++/11/cwchar(44),
from /usr/include/c++/11/bits/postypes.h(40),
from /usr/include/c++/11/iosfwd(40),
from /usr/include/c++/11/bits/shared_ptr.h(52),
from /usr/include/c++/11/memory(77),
from test.cpp(1):
/usr/include/wchar.h(155): error: attribute "__malloc__" does not take arguments
__attribute_malloc__ __attr_dealloc_free;
^
In file included from /usr/include/c++/11/cstdlib(75),
from /usr/include/c++/11/bits/stl_algo.h(59),
from /usr/include/c++/11/algorithm(62),
from test.cpp(2):
/usr/include/stdlib.h(565): error: attribute "__malloc__" does not take arguments
__attr_dealloc_free;
^
In file included from /usr/include/c++/11/cstdlib(75),
from /usr/include/c++/11/bits/stl_algo.h(59),
from /usr/include/c++/11/algorithm(62),
from test.cpp(2):
/usr/include/stdlib.h(569): error: attribute "__malloc__" does not take arguments
__THROW __attr_dealloc (reallocarray, 1);
^
In file included from /usr/include/c++/11/cstdlib(75),
from /usr/include/c++/11/bits/stl_algo.h(59),
from /usr/include/c++/11/algorithm(62),
from test.cpp(2):
/usr/include/stdlib.h(797): error: attribute "__malloc__" does not take arguments
__attr_dealloc_free __wur;
^
compilation aborted for test.cpp (code 2)
What exactly is going wrong here, and how can I fix it?
Short update: Looks as if CUDA is running into similar issues, and it might be related to glibc 2.34: https://forums.developer.nvidia.com/t/cuda-11-5-samples-throw-multiple-error-attribute-malloc-does-not-take-arguments/192750/15
Compiling and executing the shared code using icpc 2021.4 and it runs fine.
Used below command to compile the code.
icpc -std=c++14
Below are the environment details.
Operating System: Ubuntu 18.04.3 LTS
Kernel: Linux 4.15.0-76-generic
For compatibility, Kindly refer the link for IntelĀ® C++ Compiler Classic System Requirements
https://software.intel.com/content/www/us/en/develop/articles/oneapi-c-compiler-system-requirements.html

C++20 modules on clang (Windows): typeinfo error in simplest example

File first_module.cppm
export module first_module;
int foo(int x) {
return x;
}
export int e = 42;
export int bar() {
return foo(e);
}
Pre-compiling (no problems):
$ clang++ --std=c++20 -fmodules --precompile first_module.cppm -o first_module.pcm
Compiler information:
$ clang++ -v
clang version 10.0.0
Target: x86_64-pc-windows-msvc
File first-main.cc
import first_module;
int main() {
return bar();
}
Compiling (no problems):
$ clang++ --std=c++20 -fmodules first-main.cc -fmodule-file=first_module.pcm first_module.pcm
Everything is ok.
File second-main.cc
import first_module;
#include <iostream>
int main() {
std::cout << bar() << std::endl;
}
Compiling same way:
$ clang++ --std=c++20 -fmodules second-main.cc -fmodule-file=first_module.pcm first_module.pcm
Result: ton of errors like:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\include\eh.h:56:14: error: reference to 'type_info' is ambiguous
_In_ type_info const& _Type,
^
note: candidate found by name lookup is 'type_info'
note: candidate found by name lookup is 'type_info'
I have feeling that I am doing something wrong, because I have newest MSVS (updated recently), newest clang, but something still not working on Windows on trivial examples.
Or may be this is known bug? Tried to google it, no results.
The core problem resides in that your current Clang installation it's working with the MSVC toolchain and there's a known issue when you compile using #include directives with the Microsoft toolchain with Clang using the modules feature.
You can find it here, opened since 2018
https://github.com/llvm/llvm-project/issues/38400

How to fix lib libstdc++'s `std::variant` (from GCC 9.1) not working with Clang 8?

MSYS2's GCC package was recently updated to 9.1, but Clang doesn't like the new <variant> libstdc++ header that comes with it.
When compiling following simple program:
#include <variant>
int main()
{
std::variant<int, float> x;
}
I get:
# clang++ -std=c++17 foo.cpp
In file included from foo.cpp:1:
Z:\...\msys2\mingw64\include\c++\9.1.0\variant:1559:55: error: '__get' is missing exception specification 'noexcept'
friend constexpr decltype(auto) __detail::__variant::__get(_Vp&& __v);
^
foo.cpp:5:30: note: in instantiation of template class 'std::variant<int, float>' requested here
std::variant<int, float> x;
^
Z:\...\msys2\mingw64\include\c++\9.1.0\variant:263:5: note: previous declaration is here
__get(_Variant&& __v) noexcept
^
1 error generated.
Here is the complete <variant> header if you want to look at it.
While I'm waiting for an official fix, I did as Clang suggested and added noexcept to the header.
It seems to work so far.
Can this solution cause any problems? Should I do something else?
Bonus points if you know if it's a libstdc++ bug or a Clang bug.
The fix is correct. It's a libstdc++ bug, see https://bugs.llvm.org/show_bug.cgi?id=41863 and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90397

clang++ error on <experimental/any>

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.

why doesn't clang++ compile the following code?

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.