Archive for the ‘c++’ Category

gcc: fatal error: ‘bits/stdc++.h’ file not found

August 4, 2016

This post discusses how to use bits/stdc++.h in cpp files in OS X environment.

environment

  • OS X El Capitan, version 10.11.5
  • Xcode Version 7.3.1

what is bits/stdc++.h
This header file is very convenience since we don’t need to include other stdc++ header files. It spares the programmer time and is very useful when you need to develop tools for yourself quickly or join a programming competition. However, it increases the object size since it includes lots of other header files. So it’s better to understand the trade off while using this header file.

  1 #include <bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5 int main()
  6 {
  7     int n;
  8     cin >> n;
  9 
 10     vector<int> v(n);
 11     for (auto &a: v)
 12         cin >> a;
 13 
 14 
 15     for (auto a: v)
 16         cout << a << " ";
 17     cout << endl;
 18 
 19     return 0;
 20 }

symptom and root cause
The gcc reports errors while the source code built includes bits/stdc++.h. The reason is that the gcc in Xcode Command Line Tools of OS X is clang rather than GNU Compiler Collection.

$ g++ -std=gnu++11 main.cpp
main.cpp:1:10: fatal error: 'bits/stdc++.h' file not found
#include <bits/stdc++.h>
         ^
1 error generated.

a solution to this error
This header file is only included in gcc(GNU Compiler Collection) but not in clang. Therefore, we need to install gcc(GNU Compiler Collection) if we want to use bits/stdc++.h. gcc(GNU Compiler Collection) shows how to install gcc(GNU Compiler Collection) in OS X.

conclusion
This posts shows that the header file bits/stdc++.h is only exclusive to gcc(GNU Compiler Collection). And we need to use GNU gcc to compile programs which use this header file.

Advertisements

%d bloggers like this: