LoginSignup
6
4

More than 3 years have passed since last update.

clang で bits/stdc++.h を使う

Posted at

はじめに

:rooster:並に忘れるのでメモです。

やりたいこと

clang++ で bits/stdc++.h 使いたいわー!:heart_eyes:

できたこと

/usr/local/include/bits に stdc++.h を作成したらできた:relaxed:

環境

  • macOS Catalina 10.15.5

やったこと

とりあえず

main.cpp
#include <bits/stdc++.h>
using namespace std;

int main(){
    cout << "HOGE HOGE パラダイス" << endl;
}

を用意していざぁ!

hoge@foo % clang++ main.cpp
main.cpp:1:10: fatal error: 'bits/stdc++.h' file not found
#include <bits/stdc++.h>
         ^~~~~~~~~~~~~~~
1 error generated.

怒られちゃった🥺

clangちゃんどこ探してんの・・・?

clangちゃん に bits/stdc++.h 見つけさせたいのは山々ですが
bits/stdc++.hをどこに作成すればいいのか分かりません:disappointed_relieved:

いつもどおり拙いワードを並べてガンガン検索でガン速ぅ↑(???????)していると...
OS X clang include lib search path にたどり着きました!

$ clang -x c -v -E /dev/null

でそれっぽいのが表示されてるけどどういうこと・・・?

てなわけで
clang - the Clang C, C++, and Objective-C compiler
にオプションの説明を見に行きました。

-x

-x <language>
Treat subsequent input files as having type language.

てことは今回はc++だな!

-v

-v
Show commands to run and use verbose output.

これで詳細を出力してくれるんですね!

-E

-E
Run the preprocessor stage.

プリプロセッサだけ走らせ・・・・る!
いまいちピンときてないので(致命的)下記ページで確認。
C/C++のビルドの仕組みとライブラリ
分かりやすかった:grin:

/dev/null

入力ファイルとして渡している/dev/nullはwikipedia先生によると

/dev/null は通常、プロセスの不要な出力ストリームを捨てるのに使うか、入力ストリームのための空のファイルとして使う。

なるほど空ファイルとして使うんすね。そんなのあった・・・な?(鶏脳)

Include Lib Search Path

以上をまとめると

clang++ -x c++ -v -E /dev/null

でいけるってことだな!(clang++ は clang でも可)

早速実行すると

hoge@foo % clang++ -x c++ -v -E /dev/null
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.15.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -E -disable-free -disable-llvm-verifier -discard-value-names -main-file-name null -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=all -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=10.15.4 -target-cpu penryn -dwarf-column-info -debugger-tuning=lldb -target-linker-version 556.6 -v -resource-dir /Library/Developer/CommandLineTools/usr/lib/clang/11.0.3 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -stdlib=libc++ -internal-isystem /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1 -internal-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include -internal-externc-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Library/Developer/CommandLineTools/usr/include -Wno-objc-signed-char-bool-implicit-int-conversion -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -fdeprecated-macro -fdebug-compilation-dir /Users/hoge/pcon/test/clangtest -ferror-limit 19 -fmessage-length 73 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=macosx-10.15.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o - -x c++ /dev/null
clang -cc1 version 11.0.3 (clang-1103.0.32.62) default target x86_64-apple-darwin19.5.0
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1
 /Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
# 1 "/dev/null"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 376 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "/dev/null" 2

あった!見たかったのはここ!

#include <...> search starts here:
 /usr/local/include
 /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1
 /Library/Developer/CommandLineTools/usr/lib/clang/11.0.3/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
 /Library/Developer/CommandLineTools/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks 

てなわけで今回は /usr/local/include に bits/stdc++.hを作成します。
(上記の場所ならどこでもいいんだろうけど)

bits/stdc++.h を作成

/usr/local/include に bits ディレクトリを作成

cd /usr/local/include
mkdir bits

あとは
こことかから
stdc++.hを拾ってきてbitsにぶっこんで終了〜!!

hoge@foo % clang++ main.cpp -o hoge    
hoge@foo % ./hoge                     
HOGE HOGE パラダイス

無事clang君もbits/stdc++.h 見つけられたんやなって。
めでたしめでたし。

最後に

最初に

clang++ -v main.cpp

でも確認できるってことじゃん・・・:fearful:
まぁ色々勉強になったんで良しとします。ちゃんとmanしような!:wink:

参考文献

OS X clang include lib search path
How can I include <bits/stdc++> in Xcode
clang - the Clang C, C++, and Objective-C compiler
C/C++のビルドの仕組みとライブラリ
/dev/null

6
4
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
6
4