Weird issues with Clang and C++, and uniform initialization - c++

I was attempting to compile and run the example on this page that explores function pointers as a function input. The example I was running was the 66 line one about halfway down the page.
https://www.learncpp.com/cpp-tutorial/function-pointers/
I am on Mac iOS 12.3.1. I tried to compile with
g++ sort.cc
and was getting errors that no semicolons were in my for loops, i believe due to the bracket initialization throughout the code. And when I run it with:
g++ -std=c++11 sort.cc
It works fine.
BUT
Shouldn't my clang be compiling at at least C++11? running
clang -v
I get
Apple clang version 13.1.6 (clang-1316.0.21.2)
Target: x86_64-apple-darwin21.4.0
And from what I can tell clang versions past 4 default to c++14.
Also, when I run using clang or gcc I get errors setting -std=c++xx, but it works fine with g++. But as far as I can tell, g++ and gcc are aliases to clang, and running gcc -v or g++ -v gives me clang version 13.1.6.
So whats going on?

Xcode clang defaults to C++98. Compiling a simple program which has C++11 or later features will tell you that C++11 or later isn't the default. e.g.,
// a.cpp
constexpr int i = 10;
clang a.cpp -c
a.cpp:1:1: error: unknown type name 'constexpr'
constexpr int i = 10;
^
1 error generated.
while clang a.cpp -c -std=c++11 works fine.
Also see: https://trac.macports.org/wiki/CompilerSelection

Related

In Visual Studio Code: "no template named 'initializer_list' in namespace 'std'"? [duplicate]

I wanted to compile C++11 source code within Mac Terminal but failed. I tried g++ -std=c++11, g++ -std=c++0x, g++ -std=gnu++11 and g++ -std=gnu++0x but nothing worked. Terminal always read unrecognized command line option. However, g++ -std=gnu and things like that worked fine (of course C++11 source code could not pass).
Which option should I use to turn on C++11 support?
By the way, the command line tool I'm using is installed within Xcode, and I'm pretty sure that they are up-to-date.
As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.
clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.
xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp
Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.
For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.
XCode uses clang and clang++ when compiling, not g++ (assuming you haven't customized things). Instead, try:
$ cat t.cpp
#include <iostream>
int main()
{
int* p = nullptr;
std::cout << p << std::endl;
}
$ clang++ -std=c++11 -stdlib=libc++ t.cpp
$ ./a.out
0x0
Thanks to bames53's answer for pointing out that I had left out -stdlib=libc++.
If you want to use some GNU extensions (and also use C++11), you can use -std=gnu++11 instead of -std=c++11, which will turn on C++11 mode and also keep GNU extensions enabled.

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.

clang seems to use the gcc libraries

This is the first time I use clang. What I notices is that any error from clang referencing the std library looks like this:
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/ostream:245:7:
^^^ ^^^ ^^^
So it looks like clang links — or at least includes — the gcc libraries.
The command I used: clang++ -c -Wall -Wextra -Werror -g test.cpp -o test.o. (The program had a intentional error just to prove this).
How is this possible? What can I do to make clang use its own libraries (but not break gcc)?
Additional information:
I am on a Ubuntu 14.04 machine.
clang++ --version
Ubuntu clang version 3.5-1ubuntu1 (trunk) (based on LLVM 3.5)
Target: x86_64-pc-linux-gnu
Thread model: posix
g++ --version
g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
I had previously installed several versions (at the same time, used them with update-alternatives) of gcc with apt-get. Right now I have only 4.8 (I have uninstalled the others). Could I have messed up something then? I have never installed clang (I guess it is default with Ubuntu).
Just to clarify: the correct programs compile and run in clang++.
Further tests: I know that gcc didn’t implement yet types like is_trivially_constructible and move operations on iostream in their standard c++11 library (https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html) and that clang has full c++11 conforming library so I tested those compiling with clang and I got the corresponding gcc errors, which only confirms that clang is using gcc libraries.
A very basic program
#include <iostream>
using namespace std;
int main() {
cout << "Yada Yada" << endl;
return 0;
}
gives this error when compiling with -std=c++1y in clang++:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/iostream:39:
...
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/cstdio:120:11: error: no member named 'gets' in the global namespace
using ::gets;
~~^
So right now I can’t compile anything with c++1y in clang.
You need to install libc++ and make clang use it with -stdlib=libc++
I had similar issue: GCC (g++) already was installed on my LinuxMint (Ubuntu base) so when compile with clang, was getting an " error: no member named 'gets' in the global namespace using ::gets ".
resolved by installing libc++-dev (sudo apt-get install libc++-dev) and compiling with -stdlib++ (clang++ -g -std=c++1y -stdlib=libc++ helloworld.cpp -o helloworld)
Your real problem is that you're using C++14 (c++1y was the informal name used to refer to it when it wasn't yet fully formed), with a C++ library belonging to GCC 4.8. GCC 4.8 has complete C++11 support, but hardly even started on C++14 features.
This is caused by C++14 removing std::gets, and the GNU C library anticipating on that by not defining gets in the global namespace while the C++ library has not yet caught up and is trying to make it available in the std namespace.
The proper way to solve this does not require using libc++, just to use a C++ library with C++14 support. GLIBCXX 4.9 (aka libstdc++) already suffices.

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.

clang doesn't know std::atomic_bool, but XCode does

I'm trying to compile C++11 code that declares a variable of type std::atomic_bool. This is on Mac OS 10.8.2 with clang:
clang --version
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin12.2.0
Thread model: posix
clang complains about std::atomic_bool:
clang++ -c -stdlib=libc++ -msse4 -std=c++11 -Wno-unused-parameter -I. -o query.o query.cpp
In file included from query.cpp:1:
[...]
./threadutils.h:33:10: error: no type named 'atomic_bool' in namespace 'std'; did you mean 'atomic_long'?
std::atomic_bool work;
However, the same file compiles fine in an XCode project using the same compiler. So I assume I'm missing something in my manual compiler invocation.
I tried a few variations such as -std=c++0x and -std=gnu++11, to no avail.
I figured it out. Unfortunately I planted a false flag into my question: it didn't work in XCode either, I had a different version of the source file imported there.
The problem was that C++11 defines "a named type atomic_bool corresponding to the specified atomic<bool>", but clang doesn't support that.
Renaming the type from atomic_bool to atomic<bool> fixed it.
I found the same problem. In my case I resolved it by including atomic:
#include <atomic>
static std::atomic_bool varname;
After this, I could call g++ with std=c++11 on a Linux (Ubuntu) as well as compile on XCode.