Paras_ydv's blog

By Paras_ydv, history, 3 hours ago, In English

Hi everyone,

I do CP on a MacBook, and the first thing every Mac user hits is this:

main.cpp:1:10: fatal error: 'bits/stdc++.h' file not found

because g++ on macOS is actually clang. I used to copy stdc++.h into the Xcode folder, but it got wiped every time Xcode updated, and pb_ds / rope still didn't work. Installing gcc from brew also works, but then you type g++-15 everywhere and the editor keeps using clang anyway.

So I made a small tool for it, cp-mac:

brew install paras-ydv/cp/cp-mac
cp-mac link     # only on apple silicon, once

or curl -fsSL https://raw.githubusercontent.com/Paras-ydv/cp-mac/main/install.sh | sh if you don't use brew.

cp-mac-demo

After that, a normal g++ a.cpp (and VS Code etc.) compiles stuff like this:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template <class T> using ordered_set = tree<T, null_type,
    less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
    ordered_set<int> s;
    for (int x : {5, 1, 9, 3}) s.insert(x);
    cout << *s.find_by_order(1) << " " << __lg(1024) << "\n";   // 3 10
}

What it covers right now:

  • bits/stdc++.h and bits/extc++.h (c++11 up to c++26)
  • pb_ds and rope, taken from gcc's own source, so they behave the same as on CF
  • __gcd, __lg, random_shuffle, bind2nd etc. in c++17 and later
  • -D_GLIBCXX_DEBUG actually does bounds checking

It also has cprun a.cpp, which compiles with a 256 MB stack. This one bit me a few times: macOS gives you 8 MB of stack, so a recursive dfs on a 1e6 line graph segfaults locally (around depth 130k for me) even though it passes on CF. cprun also prints time/memory after each run, and cprun -d turns on ASan + UBSan.

Things it can't fix: _Find_first / _Find_next on bitset, __float128, and long double being only 64-bit on M-series Macs.

GitHub: https://github.com/Paras-ydv/cp-mac 40s demo: https://youtu.be/gote8COPk9M

It's open source. If something that compiles on CF still doesn't compile on your Mac with this, drop the code in the comments and I'll look at it.

  • Vote: I like it
  • +4
  • Vote: I do not like it

»
2 hours ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Helpful stuff