clang seems to use the gcc libraries - c++

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.

Related

C++20, how to compile with Clang-10 or GCC11

I'm aware that C++20 is not fully supported (yet) by the compilers, but I really want to learn modules and other C++20 stuff.
Modules are supported in GCC11 and Clang-8+.
Compiler Support of C++20
I've installed Clang-10 on my Ubuntu, but it still gives me errors:
import <iostream>;
using namespace std;
int main(){
cout << "Hello world";
}
What am I doing wrong?
COMMANDS:
clang++ -Wall -std=c++2a -stdlib=libc++ -fimplicit-modules
-fimplicit-module-maps main.cpp -o main
clang++ -Wall -std=c++20 -stdlib=libc++ -fimplicit-modules
-fimplicit-module-maps main.cpp -o main
ERROR: fatal error: 'iostream' file not found
Although c++20 adds modules the c++20 standard library doesn't expose any modules.
Microsoft have implemented some standard library modules which may or may not match a future c++ standard: https://learn.microsoft.com/en-us/cpp/cpp/modules-cpp?view=msvc-160#consume-the-c-standard-library-as-modules. With these your example would be:
import std.core;
using namespace std;
int main(){
cout << "Hello world";
}
As far as I can see neither libc++ or libstdc++ have implemented any modules yet.
By default, gcc trunk use c++17, and clang trunk use c++14, so you have to say compiler, that you want to use c++20
If you are compiling your code in terminal by yourself, than add following flag
--std=c++2a
If you compile your code using Cmake, than add following to your CMakeLists.txt
set(CMAKE_CXX_STANDARD 20)
And if you compile in some IDE(Codeblocks or Visual studio), than somewhere in compiler settings put supporting c++20
trunk means "the main line of development", so this compiler version should be latest officially supported

Why does MinGW GCC tolerate missing return types even with `-pedantic-errors`, unlike regular GCC?

Here's the code:
main() {}
On gcc.godbolt.org, both GCC 10.1 and Clang 10 (with -Wall -Wextra -pedantic-errors -std=c++20) refuse to compile this.
GCC: error: ISO C++ forbids declaration of 'main' with no type [-Wpedantic]
Clang: error: C++ requires a type specifier for all declarations
But on my local machine, MinGW GCC happily accepts this code without any errors or warnings (with the same flags). This is not something new; this specific peculiarity was there for years.
Why does MinGW GCC behave differntly from the regular GCC in this case? Are there any flags to make it diagnose this error?
I got my GCC from MSYS2. It identifies as
# g++ --version
g++.exe (Rev3, Built by MSYS2 project) 10.1.0
Copyright (C) 2020 Free Software Foundation, Inc.
Clang 10 on the same machine does reject the code (the official binary, using libstdc++ from this GCC).
As noted by #ssbssa, MinGW GCC enables -fms-extensions by default, and it's one of the effects of that flag.
Compile with -fno-ms-extensions to fix that.

os x LLVM supports c++11/14, why the include directory is still gcc 4.2.1?

On my new macbook pro, after install xcode, I found:
Both gcc and clang are linking to LLVM 7.3. I guess gcc is just an alias of clang here, right?
As long as gcc supports -std=c++11 and -std=c++14 parameter, why the /usr/include/c++ only has a directory named "4.1.2"? I know gcc 4.1.2 is quite an old version,

Apple Clang; using C++11 with libstdc++

I have an issue when compiling a simple Hello file with an empty function taking initializer_list argument when using both -stdlib=libstdc++ and -std=c++11
If I use only -std=c++11 (which means compiling with libc++)
then the file compiles and prints Hello!
If I comment function_test and I use both -std=c++11 and -stdlib=libstdc++
then the file compiles and prints Hello!
If I keep the function function_test and I use both -std=c++11 and -stdlib=libstdc++
then I get the following error:
$ g++ -stdlib=libstdc++ -std=c++11 -o test test.cpp
test.cpp:1:10: fatal error: 'initializer_list' file not found
#include <initializer_list>
^
1 error generated.
Here is my file
#include <initializer_list>
#include <iostream>
using namespace std;
void function_test(initializer_list<int> something){}
int main(int argc, char * argv[])
{
cout << "Hello!" << endl;
function_test({0});
return 0;
}
Here is my apple clang version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix
--with-gxx-include-dir=/usr/include/c++/4.2.1
^^^^^^^
Notice the "4.2". Your libstdc++ is way to old for C++11. Upgrade it to some 5.x version for full C++11 support.
I found out that upgrading to a newer version of libstdc++ is just not possible with apple-llvm(clang). So using some features of C++11 with libstdc++ is not possible. The reason is this one:
Mainline libstdc++ has switched to GPL3, a license which the
developers of libc++ cannot use. libstdc++ 4.2 (the last GPL2 version)
could be independently extended to support C++11, but this would be a
fork of the codebase (which is often seen as worse for a project than
starting a new independent one). Another problem with libstdc++ is
that it is tightly integrated with G++ development, tending to be tied
fairly closely to the matching version of G++.
source:
http://libcxx.llvm.org/docs/
Thanks to all the answers/comments that helped me reach the answer.

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.