Mac 에 GNU 컴파일러 설치하기
Mac은 Unix 기반의 운영체제로서 커맨드라인 유틸리티로 다양한 컴파일러들을 이용하는 것이 용이합니다. 그중에서 GNU 컴파일러들은 c,c++, fortran 컴파일러들이 포함되어있는 컴파일러 팩키지 입니다. 오늘은 GNU 컴파일러 설치에 대해서 알아보도록 합니다.
1. 앱스토어에서 Xcode 를 설치합니다.
2. 터미널을 실행시켜서, Command Line Tool 을 설치합니다.
$ Xcode-select --install
3. High Performance Computing for Mac OS X 사이트로 가서 GNU 컴파일러를 다운받습니다.
http://hpc.sourceforge.net/index.php
4. GNU 컴파일러를 설치합니다.
$ cd Downloads
$ sudo tar -xvf gcc-5.3-bin.tar -C /
5. GNU 컴파일러가 잘 설치되었는지 확인합니다.
$ gcc -v
$ g++ -v
$ gfortran -v
출처 - https://wiki.helsinki.fi/display/HUGG/GNU+compiler+install+on+Mac+OS+X
만약 직접 컴파일러들을 소스코드로부터 빌드하고 싶다면 다음과 같은 방법을 따르면 됩니다.
1. 앱스토어에서 Xcode 를 설치합니다.
2. 터미널을 실행시켜서, Command Line Tool 을 설치합니다.
$ Xcode-select --install
3. High Performance Computing for Mac OS X 사이트로 가서 GNU 컴파일러를 다운받습니다.
http://hpc.sourceforge.net/index.php
4. GNU 컴파일러를 설치합니다.
$ cd Downloads
$ sudo tar -xvf gcc-5.3-bin.tar -C /
5. GNU 컴파일러가 잘 설치되었는지 확인합니다.
$ gcc -v
$ g++ -v
$ gfortran -v
출처 - https://wiki.helsinki.fi/display/HUGG/GNU+compiler+install+on+Mac+OS+X
만약 직접 컴파일러들을 소스코드로부터 빌드하고 싶다면 다음과 같은 방법을 따르면 됩니다.
In this tutorial, I will show you how to compile from source and install the current stable version of GCC with Graphite loop optimizations on your macOS computer. The instructions from this tutorial were tested with Xcode 8 and Sierra (macOS 10.12).
Clang, the default compiler for macOS, supports only C, C++, Objective-C and Objective-C++. If you are interested in a modern Fortran compiler, e.g. you will need gfortran that comes with GCC. Another reason to have the latest stable version of GCC on your macOS is that it provides you with an alternative C and C++ compiler. Testing your code with two different compilers is always a good idea.
In order to compile GCC from sources you will need a working C++ compiler. In the remaining of this article I will assume that you have installed the Command Line Tools for Xcode. At the time of this writing Apple’s Command Line Tools maps the gcc and g++ to clang and clang++. If you don’t have the Command Line Tools installed, open a Terminal and write:
which will guide through the installation process.
Let’s start by downloading the last stable version of GCC from the GNU website, so go to: http://gcc.gnu.org/mirrors.html and download gcc-7.1.0.tar.bz2. I’ve saved the archive in my Downloads folder.
We will also need three other libraries for a successful build of gcc: mpc, mpfr and gmp. Use the above links and download the last versions for all of them: gmp-6.1.2.tar.bz2, mpc-1.0.3.tar.gz and mpfr-3.1.5.tar.bz2, also save them in your Downloads folder.
For enabling the Graphite loop optimizations you will need two extra libraries, go to ftp://gcc.gnu.org/pub/gcc/infrastructure/ and download isl-0.16.1.tar.bz2.
Extract the above five archives in your Downloads folder and open a Terminal window.
We will start by compiling the gmp library:
Create a new folder named build in which the compiler will save the compiled library:
And now the fun part … write in your Terminal:
If you see no error message we can actually compile the gmp library:
In a few minutes you will have a compiled gmp library. If you see no error message … congratulations, we are ready to install the library in the /usr/local/gcc-7.1 folder (you will need the administrator password for this):
We will do the same steps for MPFR now:
Configuration phase:
The second parameter will just inform the configure app that gmp is already installed in /usr/local/gcc-7.1.
After the configure phase is finished, we can make and install the library:
Now, we are going to build MPC:
At this time you should have finished to build and install the necessary prerequisites for GCC.
Next step is to build the library for the Graphite loop optimizations:
We are ready to compile GCC now. Be prepared that this could take more than one hour on some machines … Since I’m interested only in the C, C++ and Fortran compilers, this is the configure command I’ve used on my machine:
The above command instructs the configure app where we have installed gmp, mpfr, mpc and isl; also it tells to add a prefix to all the resulting executable programs, so for example if you will invoke GCC 7.1.0 you will write gcc-7.1, the gcc command will invoke Apple’s version of clang.
If you are interested in building more compilers available in the GCC collection modify the –enable-languages configure option.
And now, the final touches:
Grab a coffee, maybe a book, and wait … this should take approximately, depending on your computer configuration, an hour … or more … and about 5.24GB of your disk space for the build folder.
Install the compiled gcc in /usr/local/gcc-7.1:
Now, you can keep the new compiler completely isolated from your Apple’s gcc compiler and, when you need to use it, just modify your path by writing in Terminal:
If you want to avoid writing the above command each time you open a Terminal, save the above command in the file .bash_profile from your Home folder.
You should be able to invoke any of the newly compiled compilers C, C++, Fortran …, invoking g++ is as simple as writing in your Terminal:
Remember to erase your build directories from Downloads if you want to recover some space.
Next, I’ll show you how to check if the compiler was properly installed by compiling and running a few examples. GCC 7 uses by default the C++14 standard and C11 for the C coders, you should be able to compile any valid C++14 code directly. In your favorite text editor, copy and save this test program (I’ll assume you will save the file in your Home directory):
Compiling and running the above lambda example:
We could also compile a C++ code that uses threads:
Next, we present a simple C++ code that uses regular expressions to check if the input read from stdin is a floating point number:
If you are a Fortran programmer, you can use some of the Fortran 2008 features like doconcurrent with gfortran-7.1:
The above code can be compiled with (assuming you’ve named it tst_concurrent_do.f90):
댓글
댓글 쓰기