TR kizaki Tech Memo

C++ setting on VScode for codeforces, couldn't find bits/stdc++.h[solved]

#include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/Users/$HOME/dev/atcoder/abc002/src/a.cpp).

cannot open source file "bits/stdc++.h”

Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx12.0.0 -Wundef-prefix=TARGET_OS_ -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name a.cpp -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fno-strict-return -fno-rounding-math -funwind-tables=2 -target-sdk-version=13.1 -fvisibility-inlines-hidden-static-local-var -target-cpu penryn -tune-cpu generic -debugger-tuning=lldb -target-linker-version 820.1 -v -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -I/usr/local/include/ -I/usr/local/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1 -internal-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -internal-externc-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Wno-reorder-init-list -Wno-implicit-int-float-conversion -Wno-c99-designator -Wno-final-dtor-non-final-class -Wno-extra-semi-stmt -Wno-misleading-indentation -Wno-quoted-include-in-framework-header -Wno-implicit-fallthrough -Wno-enum-enum-conversion -Wno-enum-float-conversion -Wno-elaborated-enum-base -Wno-reserved-identifier -Wno-gnu-folding-constant -Wno-cast-function-type -Wno-bitwise-instead-of-logical -fdeprecated-macro -fdebug-compilation-dir=/Users/$HOME/dev/atcoder/abc053/src -ferror-limit 19 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fgnuc-version=4.2.1 -fno-cxx-modules -fcxx-exceptions -fexceptions -fmax-type-align=16 -fcommon -fcolor-diagnostics -clang-vendor-feature=+messageToSelfInClassMethodIdReturnType -clang-vendor-feature=+disableInferNewAvailabilityFromInit -clang-vendor-feature=+disableNonDependentMemberExprInCurrentInstantiation -fno-odr-hash-protocols -clang-vendor-feature=+enableAggressiveVLAFolding -clang-vendor-feature=+revert09abecef7bbf -clang-vendor-feature=+thisNoAlignAttr -clang-vendor-feature=+thisNoNullAttr -mllvm -disable-aligned-alloc-awareness=1 -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /var/folders/7c/xsq3zg0d4ybdxd8qvb6wgypw0000gn/T/a-c91824.o -x c++ a.cpp
clang -cc1 version 14.0.0 (clang-1400.0.29.202) default target x86_64-apple-darwin21.6.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/Library/Frameworks"
ignoring duplicate directory "/usr/local/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
a.cpp:1:10: fatal error: 'bits/stdc++.h' file not found
#include <bits/stdc++.h>
         ^~~~~~~~~~~~~~~
1 error generated.

ref: MacでGCCを"正しく"環境構築しよう!

(2) インストールされたGCCの確認

┌─(~/dev/atcoder/abc053/src)────┐
└─(21:59:46 on main)──> ls -l /usr/local/bin | grep g++     1 ↵ ──(Tue,May23)─┘
lrwxr-xr-x  1 $HOME
  admin         31  7 May 19:33 g++-13 -> ../Cellar/gcc/13.1.0/bin/g++-13
lrwxr-xr-x  1 $HOME
 admin         46 14 May 19:18 wasm32-clang++ -> ../Cellar/emscripten/3.1.37/bin/wasm32-clang++
lrwxr-xr-x  1 $HOME
  admin         51 14 May 19:18 wasm32-wasi-clag++ -> ../Cellar/emscripten/3.1.37/bin/wasm32-wasi-clang++
lrwxr-xr-x  1 $HOME
 admin         53  7 May 19:33 x86_64-apple-darwin21-g++-13 -> ../Cellar/gcc/13.1.0/bin/x86_64-apple-darwin21-g++-13

(3) バージョンの省略

/usr/local/bin内にg++をシンボリックリンクとして作成し、リンク先に/usr/local/bin内のg++-{num}を指定することで、バージョンを省略することができます。

┌─(~/dev/atcoder/abc053/src)────┐
└─(22:25:47 on main ✭)──> ln -s /usr/local/bin/g++-13 /usr/local/bin/g++
└─(22:26:57 on main ✭)──> ls -l /usr/local/bin | grep g++       ──(Tue,May23)─┘
lrwxr-xr-x  1 $HOME
 admin         21 23 May 22:26 g++ -> /usr/local/bin/g++-13
lrwxr-xr-x  1 $HOME
  admin         31  7 May 19:33 g++-13 -> ../Cellar/gcc/13.1.0/bin/g++-13
lrwxr-xr-x  1 $HOME
  admin         46 14 May 19:18 wasm32-clang++ -> ../Cellar/emscripten/3.1.37/bin/wasm32-clang++
lrwxr-xr-x  1 $HOME
 admin         51 14 May 19:18 wasm32-wasi-clag++ -> ../Cellar/emscripten/3.1.37/bin/wasm32-wasi-clang++
lrwxr-xr-x  1 $HOME  
admin         53  7 May 19:33 x86_64-apple-darwin21-g++-13 -> ../Cellar/gcc/13.1.0/bin/x86_64-apple-darwin21-g++-13

**[4] g++コマンドの確認

**(1) 実行可能なg++コマンド

┌─(~/dev/atcoder/abc053/src)────┐
└─(22:42:11 on main ✭)──> which g++                             ──(Tue,May23)─┘
/usr/local/bin/g++

┌─(~/dev/atcoder/abc053/src)────┐
└─(22:42:37 on main ✭)──> which -a g++                          ──(Tue,May23)─┘
/usr/local/bin/g++
/usr/bin/g++
/usr/local/bin/g++
/usr/bin/g++

(2) GCC環境とClang環境

さらに、/usr/local/bin/g++はインストールしたGCC環境のC++コンパイラを指し、/usr/bin/g++はすでにインストールされているClang環境のC++コンパイラ5を指します。また、それぞれのコンパイラの情報については下記のコマンドで確認することができます。

#gcc環境のC++コンパイラ
┌─(~/dev/atcoder/abc053/src)────┐
└─(22:44:02 on main ✭)──> /usr/local/bin/g++ -v                 ──(Tue,May23)─┘
Using built-in specs.
COLLECT_GCC=/usr/local/bin/g++
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/13.1.0/bin/../libexec/gcc/x86_64-apple-darwin21/13/lto-wrapper
Target: x86_64-apple-darwin21
Configured with: ../configure --prefix=/usr/local/opt/gcc --libdir=/usr/local/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-13 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-zstd=/usr/local/opt/zstd --with-pkgversion='Homebrew GCC 13.1.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=x86_64-apple-darwin21 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.1.0 (Homebrew GCC 13.1.0)
   #clang環境のC++コンパイラ
┌─(~/dev/atcoder/abc053/src)────┐
└─(22:45:16 on main ✭)──> /usr/bin/g++ -v                       ──(Tue,May23)─┘
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: x86_64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

  • MacでのGCCのコンパイルエラーの対処法について

(1)原因の確認

fatal error: '〇〇.h' file not found〇〇.h(ヘッダファイル)が見つからないことが原因です。まずはヘッダファイルのインクルード元を見つけるためにサーチパスを確認しましょう

サーチパスは適当なC++のファイル(hoge.cc)を用意して-vオプション付きでコンパイル(% g++ -v hoge.cc2)した時の出力の中にあります3。また、自分の出力結果の該当部分のみを参考に載せておきます4

┌─(~/dev/atcoder/abc053/src)────┐
└─(22:46:43 on main ✭)──> g++ -v a.cpp

#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include/
 /usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin21/13/../../../../../../include/c++/13
 /usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin21/13/../../../../../../include/c++/13/x86_64-apple-darwin21
 /usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin21/13/../../../../../../include/c++/13/backward
 /usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin21/13/include
 /usr/local/Cellar/gcc/13.1.0/bin/../lib/gcc/current/gcc/x86_64-apple-darwin21/13/include-fixed
 /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/usr/include
 /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Frameworks
End of search list.
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
End of search list.
a.cpp:1:10: fatal error: 'bits/stdc++.h' file not found
#include <bits/stdc++.h>
         ^~~~~~~~~~~~~~~
1 error generated.

**(3)〇〇.hの検索

ref: MacでのGCCのコンパイルエラーの対処法について

┌─(~/dev/atcoder/abc053/src)────┐
└─(21:53:31 on main)──> locate /stdc++.h                    1 ↵ ──(Tue,May23)─┘
/Library/Developer/CommandLineTools/usr/include/c++/v1/stdc++.h
/usr/local/Cellar/gcc/13.1.0/include/c++/13/x86_64-apple-darwin21/bits/stdc++.h

**[4]file not foundへの対処

(2)SDKの確認

次は、インストールすべきSDKが異なる場合があるので確認しましょう。% g++ -vを実行し、出力の中の--with-sysrootオプションの指すディレクトリを確認してください.実は、--with-sysrootオプションで指定されたMacOSのSDKの配下からヘッダファイルがインクルードされます8

--with-sysroot=
/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk

ref: で、結局 #include <bits/stdc++.h> って何?

正しい用法は? プリコンパイル済みヘッダって何?

上のドキュメントに記されている通り、このヘッダファイルの目的は、ユーザーがlibstdc++の機能を全て含む単一のプリコンパイル済みヘッダを生成することにあります。

プリコンパイル済みヘッダとは、特に巨大なヘッダファイルについて、インクルードしたファイルのコンパイル時間を短縮するためにあらかじめ何らかの形でバイナリの形に落とし込んだヘッダファイルのことを指します。

これは例えば<iostream><string>など通常の標準ライブラリについて個別にプリコンパイルして読み込むことも考えられますが、このような標準ライブラリのインクルードが多いプログラムにおいて真にファイルアクセスを最適化するならば、標準ライブラリのファイルを1つのファイルにまとめて一括で読み込むようにしよう、という発想は自然です。bits/stdc++.hはこの目的で提供されます。

よって正しい使い方は以下のとおりです。まずbits/stdc++.hをプリコンパイルしてstdc++.h.gchを生成します。

┌─(~/dev/atcoder/abc053/src)────┐
└─(22:59:54 on main ✭)──> g++ /usr/local/Cellar/gcc/13.1.0/include/c++/13/x86_64-apple-darwin21/bits/stdc++.h -o stdc++.h.gch

┌─(~/dev/atcoder/abc053/src)────┐
└─(23:10:46 on main ✭)──> ls                                    ──(Tue,May23)─┘
a.cpp        a.out        b.cpp        bin          stdc++.h.gch
┌─(~/dev/atcoder/abc053/src)────┐
└─(23:10:49 on main ✭)──> cat a.cpp                             ──(Tue,May23)─┘
#include <bits/stdc++.h>
using namespace std;

int main() {
  int x;
  cin >> x;
  string ans = (x < 1200) ? "ABC" : "ARC";
  cout << ans << endl;
  return 0;
}%

ref: VSCodeでbits/stdc++.hが認識されないときの対処法(競プロ)

VSCodeを開いてみたところ、#include <bits/stdc++.h>で「IncludePathを更新してください」と怒られた。コンパイルは通るのでGCCではなく、VSCode側の問題のようだ。

解決方法

c_cpp_properties.jsonのIncludePathにbitsのパスを追加。

  • 「Ctrl + Shift + P」でコマンドパレットを開く。
  • 「C/Cpp: Edit configurations...」を選択。
  • includePathにbitsのパスを追加。
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/Cellar/gcc/13.1.0/include/c++/13/x86_64-apple-darwin21/bits"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/local/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "macos-gcc-x64"
        }
    ],
    "version": 4
}

※「.vscode」フォルダの中に 「c_cpp_properties.json」ファイルが作成される。

「.vscode」というフォルダが無い場合は、プロジェクトフォルダ直下に自動で作成される。


conclusion

  • VSCODEでErrorが出なくなった。.vscodeのarchitectureに詳しくなれてよかった。
  • PATHでvoltaとpnpmの後ろに usr/local/bin を移動するだけでスムーズに動いた。他のemscriptenとかのtoolも正常に動くかcheckが必要。