0

EDIT

I have been educated about this topic and I have decided to close this question

P.S In the comments, can someone tell me how to close my question?


I have some simple C++ code here that I want to compile. I am currently on a mac with MacOS Big Sur running on my computer and I don't have the <bits/stdc++.h> file as a "default" header available for all cpp files. In order to add it I went in my /Library/Developer/CommandLineTools/usr/include/c++/v1 folder and made a bits folder with a stdc++.h file manually. However if I try to compile source code with this header included I get the following error:

test.cpp:1:10: fatal error: 'bits/stdc++.h' file not found
#include <bits/stdc++.h>

Here is my clang version as well:

Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: x86_64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

What I want to achieve

  1. To compile the code successfully with my manually created bits/stdc++.h file. Preferably using clang++.
  2. (If possible) Find a way to quickly do it.

What I've already tried

  1. I've already found this thread with similar question asked, but it the answers provided didn't work for me.
  2. I also Linking custom header files when compiling c++, but it seems irrelevant to my problem.

My source code

#include <bits/stdc++.h>

int main()
{
    cout << "Meow Meow Meow\n";
    return 0;
}
The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
Nitro8923
  • 25
  • 7
  • 4
    Better to avoid `#include ` - see here: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h. – wohlstad Jul 22 '22 at 11:27
  • You are playing with internal compiler / library implementation details (like putting custom files into internal directories). And even if we would tell you how to put `` where the compiler would find it, *it would not do what you think it does*. Also, what wohlstad said -- programs that `#include ` are basically broken code. – DevSolar Jul 22 '22 at 11:31
  • I know that, but I am a beginner programmer and I just want to use it for the sake of educational purposes. (My teacher also told me to do it too.) It's also less time wasted on reading and memorising different headers. I don't really do big projects. I usually just do some fun programming on https://orac2.info. and just gives me more time to use on programming. – Nitro8923 Jul 22 '22 at 11:31
  • 2
    The educational thing to take away here is **don't do it**. And if your teacher tells you to, stand up and educate him. Or find a better teacher. `` is not for inclusion by user code, and a terrible habit to get into. – DevSolar Jul 22 '22 at 11:33
  • Thanks everyone for educating me about this problem. I really never knew about the downsides, – Nitro8923 Jul 22 '22 at 11:35
  • 1
    If you have a small project it should be even easier. You usually don't need more than a few #includes. Finding which header is required for which class is something you will benefit from. A good source is: https://en.cppreference.com/w/. – wohlstad Jul 22 '22 at 11:35
  • Thanks everyone so much. I was just wondering, what do I do with the question now? do I delete it or edit it or something? I'm new to stackoverflow. – Nitro8923 Jul 22 '22 at 11:37
  • As "why this isn't a good idea" was the answer that helped you, even though not being the answer you were looking for, I took the liberty of closing the Q as duplicate. – DevSolar Jul 22 '22 at 11:56

1 Answers1

1

First of all - it's greatly discouraged to use headers like stdc++.h.

If you anyway want to do that, you need to find the system directories your version of clang looks at. Run the following commands in the terminal:

% touch file.cpp
% clang++ -c file.cpp -v

At the bottom it should give you output like this:

#include <...> search starts here:
 /usr/local/include
 /Library/Developer/CommandLineTools/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1
 /Library/Developer/CommandLineTools/usr/lib/clang/13.1.6/include
 ...

Just chose a directory you think is more suitable for you custom header and add whatever you want to be available for inclusion:

% cd /usr/local/include
% sudo mkdir bits && cd bits
% sudo touch stdc++.h

Now an include directive of form #include <bits/stdc++.h> should work with any cpp file compiled with clang. You only need to populate it with content.

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49