Archive for August, 2016

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.

Install GNU Compiler Collection in OS X

August 4, 2016

This post discusses how to install gcc(GNU Compiler Collection) in OS X.

environment

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

Xcode, Xcode Command Line Tools and gcc
Xcode is a suite of software development tools with which we could develop applications running on OS X and iOS. Xcode Command Line Tools is a subset of Xcode. It contains many unix-like command line tools such as gcc. However the gcc in Xcode version 7 is actually clang whose backend is llvm. What I need is GNU Compiler Collection rather than clang/llvm.

$ ls -al /usr/bin
lrwxr-xr-x     1 root   wheel         7 Jul  9 03:13 c++ -> clang++
lrwxr-xr-x     1 root   wheel         5 Jul  9 03:13 cc -> clang

install gcc with brew
At first, search formulae related to gcc. It appears that the repository homebrew/versions has formulae of different gcc version.

$ brew search gcc
gcc                               homebrew/versions/gcc44           homebrew/versions/gcc46           homebrew/versions/gcc48           homebrew/versions/gcc5            homebrew/versions/llvm-gcc28    
homebrew/versions/gcc43           homebrew/versions/gcc45           homebrew/versions/gcc47           homebrew/versions/gcc49           homebrew/versions/gcc6   

Then, install the gcc of the version satisfy our needs. In my case, I want to install the latest gcc. So I install the formula homebrew/versions/gcc6. This formula then download gcc source code, make, and make install gcc-6. The build time is about 40 minutes.

$ brew install homebrew/versions/gcc6
==> Tapping homebrew/versions
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-versions'...
remote: Counting objects: 219, done.
remote: Compressing objects: 100% (207/207), done.
remote: Total 219 (delta 20), reused 56 (delta 12), pack-reused 0
Receiving objects: 100% (219/219), 263.71 KiB | 0 bytes/s, done.
Resolving deltas: 100% (20/20), done.
Checking connectivity... done.
Tapped 212 formulae (239 files, 969.8K)
==> Installing gcc6 from homebrew/versions
==> Installing dependencies for homebrew/versions/gcc6: gmp, mpfr, libmpc, isl014
==> Installing homebrew/versions/gcc6 dependency: gmp
==> Downloading https://homebrew.bintray.com/bottles/gmp-6.1.1.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gmp-6.1.1.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/gmp/6.1.1: 17 files, 3.2M
==> Installing homebrew/versions/gcc6 dependency: mpfr
==> Downloading https://homebrew.bintray.com/bottles/mpfr-3.1.4.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mpfr-3.1.4.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/mpfr/3.1.4: 25 files, 3.6M
==> Installing homebrew/versions/gcc6 dependency: libmpc
==> Downloading https://homebrew.bintray.com/bottles/libmpc-1.0.3.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libmpc-1.0.3.el_capitan.bottle.tar.gz
🍺  /usr/local/Cellar/libmpc/1.0.3: 11 files, 350.1K
==> Installing homebrew/versions/gcc6 dependency: isl014
==> Downloading http://isl.gforge.inria.fr/isl-0.14.tar.bz2
######################################################################## 100.0%
==> ./configure --disable-silent-rules --prefix=/usr/local/Cellar/isl014/0.14 --with-gmp=system --with-gmp-prefix=/usr/local/opt/gmp
==> make
==> make install
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

Conflicts with isl in main repository.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/isl014/lib
    CPPFLAGS: -I/usr/local/opt/isl014/include

==> Summary
🍺  /usr/local/Cellar/isl014/0.14: 69 files, 3.2M, built in 29 seconds
==> Installing homebrew/versions/gcc6
==> Downloading https://ftpmirror.gnu.org/gcc/gcc-6.1.0/gcc-6.1.0.tar.bz2
==> Downloading from http://mirror.hust.edu.cn/gnu/gcc/gcc-6.1.0/gcc-6.1.0.tar.bz2
######################################################################## 100.0%
==> Patching
patching file gcc/jit/Make-lang.in
==> ../configure --build=x86_64-apple-darwin15.5.0 --prefix=/usr/local/Cellar/gcc6/6.1.0 --libdir=/usr/local/Cellar/gcc6/6.1.0/lib/gcc/6 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-6
==> make bootstrap
==> make install
🍺  /usr/local/Cellar/gcc6/6.1.0: 1,434 files, 282.3M, built in 39 minutes 45 seconds

verify gcc-6 is GNU Compiler Collection
man page of gcc-6 shows that it is GNU project C and C++ compiler.

$ man gcc-6

GCC(1)                                                                                   GNU                                                                                  

NAME
       gcc - GNU project C and C++ compiler

conclusion
The gcc in Xcode Command Line Tools is not GNU Compiler Collection. We need to install GNU Compiler Collection by ourself.


%d bloggers like this: