Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang seems to use the gcc libraries

Tags:

c++

gcc

clang

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.

like image 474
bolov Avatar asked Jun 21 '14 13:06

bolov


People also ask

Does Clang use GCC?

Clang is compatible with GCC. Its command-line interface shares many of GCC's flags and options. Clang implements many GNU language extensions and compiler intrinsics, some of which are purely for compatibility.

Is Clang same as GCC?

Clang is much faster and uses far less memory than GCC. Clang aims to provide extremely clear and concise diagnostics (error and warning messages), and includes support for expressive diagnostics. GCC's warnings are sometimes acceptable, but are often confusing and it does not support expressive diagnostics.

Why does Apple use Clang instead of GCC?

"GCC is licensed under the GPL license. clang uses a BSD license, which allows it to be used by projects that do not themselves want to be GPL."

Does Clang use Libstdc ++?

Clang supports use of either LLVM's libc++ or GCC's libstdc++ implementation of the C++ standard library.


2 Answers

You need to install libc++ and make clang use it with -stdlib=libc++

like image 198
Baum mit Augen Avatar answered Oct 23 '22 12:10

Baum mit Augen


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)

like image 11
Rolis Avatar answered Oct 23 '22 12:10

Rolis