skyscraper's blog

By skyscraper, history, 5 years ago, In English

I was trying to figure out how to include bits/stdc++.h on macos since it uses clang, So once i found the solution i thought of creating a clear video about how to do it.

Video Link : here

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

| Write comment?
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For me this only works in Xcode. It still doesn't compile in CLion or in the terminal.

»
5 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Tbh, it's not worth using bits/stdc++.h. If you type out only the headers you need (with an autocomplete for standard headers maybe), you'll lose some seconds, but on the other hand, you'll save seconds of compilation time because the compiler won't have to go through all the headers on your system and figure out which declarations you need and which ones can be optimised out. When you need to debug and aren't slow at debugging, waiting 2 more seconds for each recompilation can be quite a waste.

  • »
    »
    5 years ago, # ^ |
      Vote: I like it -10 Vote: I do not like it

    I use a precompiled bits/stdc++.h header and my compilation time is around 0.6 seconds

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      How can i get a precompiled bits/stdc++.h header?

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it +4 Vote: I do not like it

        Not sure if this will work on Windows but it's worth a try:

        • Find the file stdc++.h which is referenced by the compiler.
        • Compile the file using the same command line options as usual (warnings, optimizations, etc)

        You should get a file called stdc++.pch right next to stdc++.h. Continue using g++ as usual. Next time you compile a file which includesbits/stdc++.h you should see a substantial change in compilation time (2-4 times faster)

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +10 Vote: I do not like it

      Why is this down-voted? It's intended way to use bits/stdc++, that's what it was created for.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

this video help me to solve the same problem, you can change clang to g++ if you like

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

First open finder. Then press shift+cmd+G.

Then copy this /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/ and paste it in the box. then click Go.

When you're in v1 folder create a new folder called bits after that create a new file called stdc++.h

Then go to this page: https://github.com/tekfyl/bits-stdc-.h-for-mac/blob/master/stdc%2B%2B.h copy the content and paste it in stdc++.h file and save it.

And that's it.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    bits/stdc++.h disappears everytime I update Xcode, is there any way to fix this?

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Well, I find it necessary to install GCC. With GCC there is also pbds, _Find_first, etc... It’s frustrating for me to be unable to use GCC features on my Mac.

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    the way i mentioned above, allows us to us pbds... and all gcc libreries, you can change clang to gcc on that way or you can alse compile using g++8

»
4 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

I use mac and the following includes do the trick almost all the time (atleast till problem D)

headers
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I was face a same problem with it. after search couple of hour to fix the ‘bits/stdc++.h’ file not found error

Write a simple tutorial how to fix in macOs and windows fix in ‘bits/stdc++.h’ file not found here is the tutorial link. hope this will help

TUTORIAL

If you have more suggestions please tell me I will add

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For Xcode 13 or later do this :

  1. In finder, press Cmd+Shift+G

  2. Paste : /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1

  3. Create a new folder "bits" here

  4. Download the file or create "stc++.h" and paste the content from: https://gist.github.com/frankchen0130/9ac562b55fa7e03689bca30d0e52b0e5

»
3 years ago, # |
Rev. 3   Vote: I like it 0 Vote: I do not like it

For those who don't still have a problem including the file on macOS

  1. go to finder
  2. type command+shit+g
  3. paste this /usr/local/include
  4. create a folder named bits
  5. paste bits/stdc++.h inside of it
  6. restart your editor

I hope this helps.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I arrived at this myself and then saw ur comment.

    I also had to remove these two lines from my stdc++.h file, namely, #include <cstdalign> and #include <cuchar> and it worked fine.

    MacOS -> 11.6

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      For me, I only need to remove #include <cstdalign>. (MacOS 12.4)

»
3 years ago, # |
  Vote: I like it -13 Vote: I do not like it

Step 1: Install linux.

Marinush

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

Even this video didn't helped me out in order to solve the problem, I am still getting the same error. Should I also have to check other things to work this out?

  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Run g++-12 instead of regular g++. Regular g++ is clang which doesnot have bits/stdc++. g++-12.

    user@home-MacBook-Pro ~ % g++ --version
    Apple clang version 14.0.0 (clang-1400.0.29.202)
    Target: x86_64-apple-darwin22.1.0
    Thread model: posix
    InstalledDir: /Library/Developer/CommandLineTools/usr/bin
    

    The below one is the g++ compiler you need to use(below one is without clang).

    user@home-MacBook-Pro ~ % g++-12 --version
    g++-12 (Homebrew GCC 12.2.0) 12.2.0
    Copyright (C) 2022 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
  • »
    »
    2 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Note, g++-12 automatically has bits/stdc++ so you just need to switch the compiler only. No need to copy extra files.

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Hi, Can you please tell me which compiler is faster and best among all. I am also using clang in mac.

      • »
        »
        »
        »
        2 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        you can use VS code or Sublime text

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I know this is an old post, but I kept finding it when I search this problem, so I wanted to share my 2024 solutions. To fix the missing <bits/stdc++.h> file we have these options for each of the two compilers in MacOS:

Xcode Clang

You can install Clang without the Xcode app from the App Store with: xcode-select install. To install the stdc++.h header file, you need to create the bits folder inside /usr/local/include/, then you need to define _GLIBCXX_HOSTED inside the stdc++.h for it to work, then append the rest of stdc++.h from the gcc mirror repo on GitHub. The commands are as follows:

mkdir /usr/local/include/bits
echo '#define _GLIBCXX_HOSTED 1' > /usr/local/include/bits/stdc++.h
curl -sS https://raw.githubusercontent.com/gcc-mirror/gcc/master/libstdc%2B%2B-v3/include/precompiled/stdc%2B%2B.h >> /usr/local/include/bits/stdc++.h

the downsides are that you may need to manually remove these headers: <cstdalign>,<cuchar>,<memory_resources> in order for clang to work, and that tree_policy may still not be usable

GNU gcc

You can easily download gcc from homebrew, then you need to remove the g++ to clang simlink in /usr/local/bin, then create a simlink for the gnu g++ compiler. The commands are as follows:

brew analytics off # not necessary, but I didn't know that homebrew had analytics
brew update && brew install gcc
sudo rm /usr/local/bin/g++
sudo ln -s $(ls /usr/local/bin/g++-*) /usr/local/bin/g++

Reference: this Spanish post from a Mexican competitive programing club