「clang++ fatal error iostream file not found」の版間の差分

提供: C++入門
移動: 案内検索
(ページの作成:「インストールしたclang++でコンパイルすると「fatal error: 'iostream' file not found」となってしまうケースについて、試行錯誤を...」)
 
 
行4: 行4:
  
 
== 概要 ==
 
== 概要 ==
前提として、[[apt-get]]で[[Ubuntu]]に[[clang++]]をインストールしました。
+
前提として、apt-getで[[Ubuntu]]に[[clang++]]をインストールしました。
 
+
 
== 迷走の記録 ==
 
== 迷走の記録 ==
 
 
[[clang++]]でHello WorldレベルのC++のソースコードをコンパイルしようとします。
 
[[clang++]]でHello WorldレベルのC++のソースコードをコンパイルしようとします。
 
iostreamという当たり前なものが見つからないと表示されました。
 
iostreamという当たり前なものが見つからないと表示されました。
行207: 行205:
 
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0x67613669b3e59f3b3db8198c666b298a39ccb819, stripped
 
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0x67613669b3e59f3b3db8198c666b298a39ccb819, stripped
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
== 関連項目 ==
 
== 関連項目 ==
 
* [[clang++]]
 
* [[clang++]]
<!-- vim: filetype=mediawiki -->
+
<!-- vim: filetype=mediawiki
 +
-->

2015年10月31日 (土) 21:33時点における最新版

インストールしたclang++でコンパイルすると「fatal error: 'iostream' file not found」となってしまうケースについて、試行錯誤をしました。

概要

前提として、apt-getでUbuntuclang++をインストールしました。

迷走の記録

clang++でHello WorldレベルのC++のソースコードをコンパイルしようとします。 iostreamという当たり前なものが見つからないと表示されました。

$ clang++ hello.cpp
hello.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^
1 error generated.

find で検索するとシステムでiostreamはあります。

$ find /usr/include/ -name iostream
/usr/include/c++/4.7/iostream

冗長オプションで、clang++が何をやっているのか確認してみましょう。

$ clang++ hello.cpp  -v
Ubuntu clang version 3.4-1ubuntu1 (trunk) (based on LLVM 3.4)
Target: x86_64-pc-linux-gnu
Thread model: posix
 "/usr/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all
 -disable-free -disable-llvm-verifier -main-file-name hello.cpp
 -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose
 -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64
 -target-linker-version 2.23.52.20130727 -v -resource-dir
 /usr/bin/../lib/clang/3.4 -internal-isystem
 /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++ -internal-isystem
 /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/x86_64-linux-gnu
 -internal-isystem
 /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/backward
 -internal-isystem /usr/local/include -internal-isystem
 /usr/bin/../lib/clang/3.4/include -internal-isystem
 /usr/include/clang/3.4/include/ -internal-externc-isystem
 /usr/include/x86_64-linux-gnu -internal-externc-isystem
 /usr/include/x86_64-linux-gnu -internal-externc-isystem /usr/include
 -fdeprecated-macro -fdebug-compilation-dir /tmp -ferror-limit 19
 -fmessage-length 94 -mstackrealign -fobjc-runtime=gcc
 -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions
 -fdiagnostics-show-option -fcolor-diagnostics -backend-option -vectorize-loops
 -o /tmp/hello-A9AsFc.o -x c++ hello.cpp
clang -cc1 version 3.4 based upon LLVM 3.4 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/backward"
ignoring nonexistent directory "/usr/bin/../lib/clang/3.4/include"
ignoring duplicate directory "/usr/include/x86_64-linux-gnu"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++
 /usr/local/include
 /usr/include/clang/3.4/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
hello.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^
1 error generated.

iostreamは、一応あります。

$ find /usr/include/ -name iostream
/usr/include/c++/4.7/iostream

インクルードパスを追加して、もう一度、コンパイルを試します。

$ clang++ hello.cpp  -I /usr/include/c++/4.7 -I \
/usr/include/x86_64-linux-gnu/c++/4.7/
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)

stdc++ が見つからないようです。

stdc++ は、以下のところにあります。

$ find /usr/lib/x86_64-linux-gnu/ -name '*stdc++*'
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
/usr/lib/x86_64-linux-gnu/libstdc++.so.6

-Lと-Wlで追加してみましたが、まだ、ld が見つけられないと言っています。

$ clang++ hello.cpp  -I /usr/include/c++/4.7 -I \
/usr/include/x86_64-linux-gnu/c++/4.7/ -L /usr/lib/x86_64-linux-gnu/ \
-Wl,-rpath=/usr/lib/x86_64-linux-gnu/
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)

ほかのパスにもごっそり、libstdc++ がありました。

$ find /usr/lib -name '*stdc++*'
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
/usr/lib/gcc-snapshot/lib/debug/libstdc++.so.6.0.19
/usr/lib/gcc-snapshot/lib/debug/libstdc++.so.6
/usr/lib/gcc-snapshot/lib/debug/libstdc++.a
/usr/lib/gcc-snapshot/lib/debug/libstdc++.so
/usr/lib/gcc-snapshot/lib/libstdc++.so.6.0.19
/usr/lib/gcc-snapshot/lib/libstdc++.so.6
/usr/lib/gcc-snapshot/lib/libstdc++.a
/usr/lib/gcc-snapshot/lib/libstdc++.so.6.0.19-gdb.py
/usr/lib/gcc-snapshot/lib/libstdc++.so
/usr/lib/gcc-snapshot/libx32/debug/libstdc++.so.6.0.19
/usr/lib/gcc-snapshot/libx32/debug/libstdc++.so.6
/usr/lib/gcc-snapshot/libx32/debug/libstdc++.a
/usr/lib/gcc-snapshot/libx32/debug/libstdc++.so
/usr/lib/gcc-snapshot/libx32/libstdc++.so.6.0.19
/usr/lib/gcc-snapshot/libx32/libstdc++.so.6
/usr/lib/gcc-snapshot/libx32/libstdc++.a
/usr/lib/gcc-snapshot/libx32/libstdc++.so.6.0.19-gdb.py
/usr/lib/gcc-snapshot/libx32/libstdc++.so
/usr/lib/gcc-snapshot/lib32/debug/libstdc++.so.6.0.19
/usr/lib/gcc-snapshot/lib32/debug/libstdc++.so.6
/usr/lib/gcc-snapshot/lib32/debug/libstdc++.a
/usr/lib/gcc-snapshot/lib32/debug/libstdc++.so
/usr/lib/gcc-snapshot/lib32/libstdc++.so.6.0.19
/usr/lib/gcc-snapshot/lib32/libstdc++.so.6
/usr/lib/gcc-snapshot/lib32/libstdc++.a
/usr/lib/gcc-snapshot/lib32/libstdc++.so.6.0.19-gdb.py
/usr/lib/gcc-snapshot/lib32/libstdc++.so
/usr/lib/gcc-snapshot/include/c++/4.9.0/x86_64-linux-gnu/bits/stdc++.h
/usr/lib/gcc-snapshot/include/c++/4.9.0/x86_64-linux-gnu/32/bits/stdc++.h
/usr/lib/gcc-snapshot/include/c++/4.9.0/x86_64-linux-gnu/x32/bits/stdc++.h
/usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.a
/usr/lib/gcc/x86_64-linux-gnu/4.7/libstdc++.so

clang と名の付くパッケージの .so をあさってみましたが、それらしきものはありませんでした。

$ dpkg -l  |fgrep clang | awk '{print $2;}' |xargs dpkg -L  | fgrep so
/usr/share/clang/scan-build/sorttable.js
/usr/share/clang/scan-view/Resources
/usr/share/clang/scan-view/Resources/FileRadar.scpt
/usr/share/clang/scan-view/Resources/GetRadarVersion.scpt
/usr/share/clang/scan-view/Resources/bugcatcher.ico
/usr/lib/llvm-3.4/lib/clang/3.4/include/iso646.h
/usr/lib/llvm-3.4/lib/libprofile_rt.so
/usr/lib/llvm-3.2/lib/libprofile_rt.so
/usr/lib/llvm-3.2/lib/clang/3.2/include/iso646.h
/usr/lib/llvm-3.2/lib/libclang.so.1
/usr/lib/llvm-3.4/lib/libclang.so.1

stdc++系で調べてみました。

$ dpkg -l |fgrep stdc++
ii  libstdc++6:amd64          4.8.1-10ubuntu9 amd64 GNU Standard C++ Library v3
ii  libstdc++6-4.7-dev:amd64  4.7.3-7ubuntu3  amd64 GNU Standard C++ Library v3 (development files)

パス的には、これでよさそうでした。

$ dpkg -L libstdc++6
/.
/usr
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
/usr/share
/usr/share/doc
/usr/lib/x86_64-linux-gnu/libstdc++.so.6
/usr/share/doc/libstdc++6

ldconfig をみてみました。 パスも設定されていて、libstdc++が含まれています。

$ ldconfig -p |fgrep c++
        libstdc++.so.6 (libc6,x86-64) => /usr/lib/x86_64-linux-gnu/libstdc++.so.6
$ cat /etc/ld.so.conf.d/*.conf
# libc default configuration
/usr/local/lib
# Multiarch support
/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu
# Legacy biarch compatibility support
/lib32
/usr/lib32
# Legacy biarch compatibility support
/libx32
/usr/libx32

ファイルも問題なさそうです。

$ ls -ll /usr/lib/x86_64-linux-gnu/libstdc++.so.6
lrwxrwxrwx 1 root root 19 1116 00:22 /usr/lib/x86_64-linux-gnu/libstdc++.so.6 -> libstdc++.so.6.0.18
$ ls -ll /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
-rw-r--r-- 1 root root 979056 1116 00:31 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
$ file /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18
/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0x67613669b3e59f3b3db8198c666b298a39ccb819, stripped

関連項目