Question or issue on macOS:
Homebrew: On the newer Macbooks with the Apple M1 processor, look for g in /opt/homebrew/Cellar/gcc instead of /usr/local/Cellar/gcc. Download ZIP Launching GitHub Desktop. If nothing happens. GCC Version: 10.3.0 Build: MacBook Air with m1 Host: arm64-apple-darwin20.4.0.
I have recently become frustrated with the new clang compiler included with Xcode 5. I was wondering what the best way to install GNU GCC on OS X would be.
Things to consider:
EDIT: Success! Using GCC 4.9.2 (with GMP 5.1.3, MPFR 3.1.2, MPC 1.0.2, ISL 0.12.2, and CLooG 0.18.1) I succesfully built GCC. Tips to take from here:
Hope this helps!
How to solve this problem?
Solution no. 1:
The way I do it is:
Download the source for GCC and numerous supporting packages. The instructions are in the
gcc-4.x.y/INSTALL/index.html
file in the GCC source code, or online at http://gcc.gnu.org/install/.- GNU Multiple Precision Library (GMP) version 4.3.2 (or later) from http://gmplib.org/.
- MPFR Library version 2.4.2 (or later) from http://www.mpfr.org/.
- MPC Library version 0.8.1 (or later) from http://www.multiprecision.org/.
- ISL Library version 0.11.1 from ftp://gcc.gnu.org/pub/gcc/infrastructure/.
- CLooG 0.18.0 from ftp://gcc.gnu.org/pub/gcc/infrastructure/.
Use a script to extract the source for GCC and the support libraries into a directory, create the object directory, and run the build.
This is the script I used for GCC 4.8.2:
When that finishes, run the install too. Then add $HOME/gcc/gcc-4.8.2/bin
(the name you specify in --prefix
plus /bin
) to your PATH ahead of /usr/bin
.
With a decent MacBook Pro with a 5400 rpm spinning disk, it takes an hour or two to compile everything (using the -j8
option to make
), and requires multiple gigabytes of disk space while compiling. SSD is nice when doing this (definitely faster)!
GCC 4.9.0 was released on 2014-04-22. I've installed it using basically the same process, but with CLooG 0.18.1 and ISL 0.12.2 (required updates) and GMP 5.1.3 (and 6.0.0a), MPC 1.0.2 (or 1.0.1) and MPFR 3.1.2 on Mac OS X 10.9.2 Mavericks and an Ubuntu 12.04 derivative. Beware that the gmp-6.0.0a.tar.xz
extracts into directory gmp-6.0.0
(not gmp-6.0.0a
as you might expect).
Between 2014 and 2017-09-27, I've built GCC versions 4.9.0, 4.9.1, 5.1.0, 5.2.0, 5.3.0, 6.1.0, 6.2.0, 6.3.0, 7.1.0 with only minor variations in the build script shown below for GCC 7.2.0 on macOS Sierra (10.12). The versions of the auxilliary libraries changed reasonably often.
macOS Sierra and High Sierra
On 2017-08-14, I used a minor variant of the script above to build GCC 7.2.0 on macOS Sierra 10.12 (using XCode 8 as the bootstrap compiler). One change is that CLooG doesn't seem to be needed any more (I stopped adding it with GCC 6.2.0). This is my current script:
Make sure your version of tar
supports all 4 different compressed file formats (.lz
, .gz
, .xz
, .bz2
), but since the standard Mac version of tar
does that for me, it'll probably work for you too.
On 2017-09-27, I failed to build GCC 7.2.0 on macOS High Sierra 10.13 (using XCode 9 for the bootstrap compiler) using the same script as worked on Sierra 10.12. The immediate error was a missing header <stack>
; I'll need to track down whether my XCode 9 installation is correct — or, more accurately, why it isn't correct since <stack>
is a standard header in C++98 onwards. There's probably an easy fix; I just haven't spent the time chasing it yet. (Yes, I've run xcode-select --install
multiple times; the fact that I had to run it multiple times because of network glitches may be part of the trouble.) (I got GCC 7.2.0 to compile successfully on 2017-12-02; I don't recall what gymnastics — if any — were required to get this to work.)
Time passes; version numbers increase. However, the basic recipe has worked for me with more recent versions of GCC. I have 7.3.0 (installed 2018-01-2), 8.1.0 (installed 2018-05-02), 8.2.0 (installed 2018-07-26), 8.3.0 (installed 2019-03-01) and now 9.1.0 (installed today, 2019-05-03). Each of these versions was built and installed on the current version of macOS at the time, using the current version of XCode for the bootstrap phase (so using macOS 10.14.4 Mojave and XCode 10.2.1 when building GCC 9.1.0)
Solution no. 2:
Homebrew now has the GCC package so you can install it with this command:
Solution no. 3:
Use a pre-compiled binary specifically for OS X 10.9.x Mavericks:
→ gcc-4.9
Compiled using source code from the GNU servers.
This contains current versions (4.7 is the stable release) of gfortran
(free, open source, GNU Fortran 95 compiler), gcc (GNU C) and g++ (GNU
C++) compilers that can perform auto-vectorization (i.e. modify code
to take advantage of AltiVec/SSE, automatically) and other
sophisticated optimizations like OpenMP. For more information, see
this webpage.
Download my binaries, and cd to the download folder. Then gunzip
gcc-4.9-bin.tar.gz (if your browser didn't do so already) and then
sudo tar -xvf gcc-4.9-bin.tar -C /. It installs everything in
/usr/local. You can invoke the Fortran 95 compiler by simply typing
gfortran. You will also need to have Apple's XCode Tools installed
from the Mac App Store. With XCode 4 or 5 you will need to download
the command-line tools as an additional step. You will find the option
to download the command-line tools in XCode's Preferences.
On 10.9 Mavericks, you can get the command-line tools by simply typing
xcode-select --install.
Hope this helps!
The graphical user interface (GUI) domainates the current operatingenvironments for personal computing. However, there are still tons ofpowerful tools, such as gcc and gdb, using the traditional text-basedinterface. Now, let's turn on the terminal within Linux, FreeBSD, Mac OS X, or any other UNIX-like operating system to discover the power ofcommand-line tools!
*If you don't have any linux machine available, please try to login ieng6.ucsd.edu and ieng9.ucsd.edu with ssh clients. If you have problemlogin these machines, please contact Hung-Wei.
Contents
- gcc
- gdb
gcc
gcc is the C and C++ compiler developed by GNU project. It is widelyadopted as the default compiler of UNIX-like systems. If you are using aMac, you may also get gcc by installing Xcode (Developer) Tools in the Mac OSX installation Disc #1.
Basic Usage
Assume that we have a C source file 'garbage.c' with the content of shown below:The basic way of compiling garbage.c into an executable file called 'garbage' is:
- gcc -o garbage garbage.c
If you are interested about how the assembly code of garbage.c look like, youcan also generate the assembly code by replacing the '-o garbage' option with'-S' as:
- gcc -S garbage.c
Linux Download Mac
The gcc will stop compiling the program after the 'garbage.s' file isgenerated. You may use any text editor to browser the content of theassembly code. To figure out what the assembly code does, you may referencethe following documents.Gcc Compiler Download For Mac
- . . .
- A good document on x86 ISA.
- Compile your code with debugging information:
- gcc -g -o garbage garbage.c
- Compile your code with optimizations:
- gcc -On -o garbage garbage.c
- For other optimization/debug options, you may use
- man gcc
- gdb ./garbage
- Break by line: to break the program at the beginning of a certainline, we can use the command 'break source_filename:line_number'.For example, if we want to break at the beginning of main function ingarbage.c, we can do as below:
- Break by function: to break the program at the beginning of a certainfunction, we can use the command 'break source_filename:function_name()'.For example, if we want to break at the beginning of main function ingarbage.c, we can also try below:
- Break by instruction: to break the program at the beginning of a certainmachine instruction, we can use the command 'break *PC'. For example, if we want to break at the beginning of main function in garbage.c, we can also try below:
- s: the debugger will step to the next line in the source code.For example, using the s command, the program will step through line 9 from line 8 after the program interrupted by breakpoint 1.
- si: the debugger will step to the next instruction in the compiled code. For example, using the si command, the program will stepthrough PC 0x00001f91 from 0x00001f8e.
- n: the debugger will step to the next source line. Each function callwill be treat as a single source code line.
Frequently Used Options
In addition to the basic usage, gcc also provides options that help youoptimize or debug your code. For example, you may:gdb
gcc is a debugger by GNU project. Gdb can step through your sourcecode line-by-line or even instruction by instruction. You may also watchthe value of any variable at run-time. In additon, it also helps to identifythe place and the reason making the program crash.
Basic Usage
All program to be debugged in gdb must be compiled by gcc with the option'-g' turning on. Continue with the 'garbage' example, if we want to debug theprogram 'garbage', we can simply start gdb by:
To start running and debugging the program, we can simply type the 'run'command after the (gdb) prompt as below: If the program takes arguments such as 'garbage arg_1 arg_2', we may start running the program with these arguments as:
Breaking Program
To inspect the current state of program exeution, we need to break theexecution of debugging program in gdb. At any point, we may use ctrl-c to interrupt the running program. However, ctrl-c is hard tohelp breaking our program at a specific point. Therefore, gdb provides the 'breakpoint' function that allows us to break debugging program at the'breakpoint' set by ourselves. Gdb allows the breakpoint to be set to anysource code line, function, or even any instruction.To show the current breakpoints we have, we may use the 'info breakpoint'command as:
To disable a breakpoint we have, we may use the 'disable breakpoint_number'.To re-enable disabled breakpoint, we can turn it on by 'enablebreakpoint_number'. To remove a breakpoint, we can use 'deletebreakpoint_number' or replace the 'break' with 'clear' command as wecreate these breakpoints.
To resume the exeution of the interrupted program, we can use the 'continue'or 'c' command.
Stepping through program
Once a running program is interrupted in gdb, we can step the program toinspect how the program is executed. Gdb provides several stepcommands to allow stepping program with different granularities:Inspect variables/register value
Once a running program is interrupted in gdb, we can also inspect the valueof a variable using the 'print' command.
If we are interested about the current value of variable a, wecan simply 'print variable_name'. For example, after line 8 is executed, wecan inspect if the atoi function correctly translate the characters tointeger as below:
Similiarly, if we are interested about the current value of a register, wecan simply 'print $register_name'. For example, after line 8 is executed, wecan inspect $eax as:
If we are interested about all the register values, we can use 'inforegisters' command to display the value in all registers.