0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Macで<bits/stdc++.h>を使えるようにする

Last updated at Posted at 2025-06-20

概要

MacにおいてC++で標準ライブラリすべてを一度にインクルードする<bits/stdc++.h>を使えるようにする方法をまとめました。エディタはVSCodeを仮定していて、OSのバージョンはmacOS Sequoiaです。

0. <bits/stdc++.h>とは

<iostream><vector>など標準ライブラリを全て読み込むためのコマンドです

1.gccをインストール

Homebrewをインストールしてない場合はまず、homebrewをインストールしてください。homebrewがインストールできたら、

brew install gcc

でgccをインストールできます。

2.pathを確認

g++-15などバージョン付きのコマンドがどこにあるか調べます。Macにおいて標準的に入っているg++は実はg++ではなくClangなので注意です(後述)。 インストールしたGCCのバージョン名を確認して、

 which g++-15    

でpathを調べます。おそらく、/opt/homebrew/bin/か、/usr/local/bin/にあるはずです。バージョン名が分からなければ、

 ls /opt/homebrew/bin/g++-*

 ls /usr/local/bin/g++-*

を打ち込んでどちらにあるか確認してください。

3.c_cpp_properties.jsonを編集

VSCodeを開き、Command + Shift + Pを押して、コマンドパレットからC/C++: Edit Configurations (JSON)を選択します。

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/opt/homebrew/bin/g++-15", 
            "cStandard": "c17",
            "cppStandard": "c++17",                 
            "intelliSenseMode": "macos-gcc-arm64"   
        }
    ],
    "version": 4
}

デフォルトの状態からの変更点は
"compilerPath": "/usr/bin/clang"
"compilerPath": "/opt/homebrew/bin/g++-15"に、

"intelliSenseMode": "macos-clang-arm64"
"intelliSenseMode": "macos-gcc-arm64" に、

"cppStandard": "c++14"
"cppStandard": "c++17"
にした点です。

tasks.jsonの編集

Command + Shift + Pを押して、コマンドパレットを開き、Tasks: Configure Default Build Taskを開き"command:"に先ほど調べたパスを入れます。他はお好みで設定してください。以下に私の設定を載せておきます。

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",
            "command": "/opt/homebrew/bin/g++-15",//g++のパスを入れる
            "args": [//お好みで
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}

<bits/stdc++.h>をインクルード

<bits/stdc++.h>がインクルードできるかテストファイルをコンパイルしてみましょう

#include 
using namespace std;

int main() {
    cout << "Hello, World!" << std::endl;
    return 0;
}

うまくいっていれば、右上の三角ボタンを押すと実行ファイルができているはずです。

./<yourfilename>

で実行できます。

補足:Mac に入っているg++はclangのエイリアスであり、g++ではない。

ターミナルでg++ --versionと打ってみましょう。おそらく次のように表示されるはずです。

Apple clang version 17.0.0 (clang-1700.0.13.5)
Target: arm64-apple-darwin24.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

次にclang++ --versionと打ってみましょう。上と全く同じ内容が表示されるはずです。
これはg++の実体がClangであることを示しています。 これはもともとMacはコンパイラとしてGCCを使用していたが、Clnagに移行する際、既存のプログラムとの整合性を保つためにg++がclang++のエイリアス(別名)として登録されたという歴史のためであるようです。

参照

参考になるであろう記事を挙げておきます。

<bits/stdc++.h>については以下の解説が非常に参考になります。

また以下のAtCoderのサイトにある解説も参考になります

・そもそもClang,gccとは何か

<bits/stdc++.h>に関する関連記事

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?