How to make clang search for gcc's headers? - c++

I want to replace gcc with clang (3.3) to build my C++11 code, so I should use clang's option -stdlib=libstdc++ (to make it see STL headers). The option works: clang see headers like string, but can't find c++11 headers (type_traits) because clang searches in 4.2 directories:
clang++ -stdlib=libstdc++ -E -x c++ - -v < /dev/null
...
/usr/include/c++/4.2
/usr/include/c++/4.2/backward
/usr/include/clang/3.3
/usr/include
...
How to make it look at never versions of GCC's headers?
As far as I understand, only libc++ (not libstdc++) is supported by clang for C++11 so the only way it so install libc++?

Either uninstall gcc 4.2 or use the --gcc-toolchain=<value> option.
--gcc-toolchain=<value> Use the gcc toolchain at the given directory
For example: clang++ --gcc-toolchain=/usr/local/... -stdlib=libstdc++ ...
As far as I understand, only libc++ (not libstdc++) is supported by clang for C++11 so the only way it so install libc++?
Both C++ standard libraries are supported.

Related

Does clang6 implement std::optional?

I want to use the C++17 std::optional but it seems to be absent in clang:
> cat test.cxx
#include <optional>
int main(int, char **) {
return 0;
}
> $CXX --version | head -n1
clang version 6.0.0 (trunk 317775)
> $CXX -std=c++17 test.cxx
test.cxx:1:10: fatal error: 'optional' file not found
#include <optional>
^~~~~~~~~~
1 error generated.
As you can see I am using a rather new version of clang and, as far as I know, clang 6 should have complete C++17 support. On first glance it looks like this is an clang issue, especially because including <experimental/optional> works fine, but maybe it is me who is missing something. Do you have any ideas?
Thanks
As pointed out in the comments, Clang is probably using the system's libstdc++ headers by default, and your system's libstdc++ is too old to have C++17 support.
Either install a newer GCC (which comes with a newer libstdc++) and then tell Clang how to find it using the --gcc-toolchain=/path/to/new/gcc option, or install libc++ alongside Clang and tell it to use libc++ with the -stdlib=libc++ option.
If you choose to install a newer libstdc++ then you will need at least GCC 7.1, which was the first to provide <optional>.

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,

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.

PARI/GP and gcc

I am trying to install PARI/GP and in the configuration step I get:
$ ./Configure
[...]
Looking for the compilers ...
...cc is /usr/bin/cc
...gcc is /usr/local/bin/gcc
GNU compiler version 4.8.0 20120705 (experimental) (GCC)
###
### C compiler does not work. PARI/GP requires an ANSI C compiler! Aborting.
###
### Compiler was: /usr/local/bin/gcc
$ gcc --version
gcc (GCC) 4.8.0 20120705 (experimental)
This is strange because the documentation says:
"Only ANSI C and C++ compilers are supported. Choosing the GNU compiler
gcc/g++ enables the inlining of kernel routines (about 20% speedup; if you
use g++, it is a good idea to include the -fpermissive flag). If you choose
not to use gcc, the C++ version of Pari will be a little faster because of
general inlining, but can be used in library mode only with C++ programs.
We strongly recommand using gcc all the way through."
I have also tried with g++ with the same result.
I am trying to compile on a linux x86_64.
Any ideas?
Thanks in advance,
M;
The config/get_cc script in PARI's top level tried to compile a test program
and failed.
Look for the line
$CC $CFLAGS $extraflag -o $exe ansi.c 2>/dev/null && $RUNTEST $exe
and remove the 2>/dev/null. Configure should now print out explicit error
messages from the compiler. They should provide a hint.
I had the same problem. Here is the solution for Linux Mint 17.1 64-bit:
sudo apt-get install gcc libc6-dev libgmp-dev
This command also installs the GMP library (recommended for PARI/GP).
Thanks to K.B. for the hint on how to see the problem.

How can I get clang++ installed by macports to use a non-system libc++

clang++-mp-X.Y seems to trip up when told to use libc++
clang++-mp-X.Y -std=c++0x -std=libc++ SOME_SOURCE_FILE
Often generates errors which I do not see when using
clang++- -std=c++0x -std=libc++ SOME_SOURCE_FILE
I'm guessing that the system libc++ and system clang (XCode 4.2.1) are made for each other. How can I get macports's clang to use a version of libc++ that it will work with?
From libc++:
To use your tip-of-trunk libc++ on Mac OS with clang you can:
export DYLD_LIBRARY_PATH=<path-to-libcxx>/lib
clang++ -std=c++11 -stdlib=libc++ -nostdinc++ -I<path-to-libcxx>/include -L<path-to-libcxx>/lib test.cpp