LoginSignup
2
5

More than 5 years have passed since last update.

[Clang][C++14][Linux] Clangで標準ライブラリのヘッダでコンパイルエラーが出る場合の対処法

Posted at

LinuxのClangでc++14を使おうとしたら標準ライブラリを使うだけでコンパイルエラーが発生した。その場合の対処方法のメモ

エラー内容

エラー1(GCC向けのヘッダが使われている)
$ clang++-3.8 -std=gnu++14 test.cpp
In file included from test.cpp:1:
/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;
        ~~^
1 error generated.
エラー2(libc++が存在しない)
$ clang++-3.8 -stdlib=libc++ -std=gnu++14 test.cpp
test.cpp:1:10: fatal error: 'cstdio' file not found
#include <cstdio>

対処法

libc++-devのインストール

そもそもGCC向けのヘッダが使われているので、clang向けのヘッダを取得する

libc++-devのインストール
$ apt-get install libc++-dev

clang++のライブラリの指定を -stdlib=libc++ とする

$ clang++-3.8 -stdlib=libc++ -std=gnu++14 test.cpp

以上でコンパイルエラーを回避できた。

参考

c++ - clang seems to use the gcc libraries - Stack Overflow
c++ - Clang doesn't see basic headers - Stack Overflow
“libc++” C++ Standard Library — libc++ 5.0 documentation

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5