Using gcc instead of clang in ghci or ghc - macos

On Mac OSX 10.9, the default c compiler bundled with Xcode is clang. I installed gcc-4.9 with homebrew. Now I have two different gccs, one is clang, the other is gcc. The default is clang.
I want to use gcc when compiling Haskell files with ghc, and I want also gcc when I launch ghci. How to do this change?

Reproducing my directions I've been sharing with haskellers for the past few months via https://gist.github.com/cartazio/7131371
Type ghc --print-libdir
The output will be a path like /Library/Frameworks/GHC.framework/Versions/7.6.3-x86_64/usr/lib/ghc-7.6.3
Go to that directory and edit the settings file.
There'll be a line indicating the path to the C compiler. It'll probably say /bin/gcc
Change that line to /usr/local/bin/gcc-4.8 (or whichever gcc version you brew installed, such as /usr/local/bin/gcc-4.2)

Related

Interdependences of homebrew llvm, gcc and XCode

So far, I have understood that on a MacOS system, on which I have installed XCode and nothing from Homebrew, clang++ comes from XCode, and g++ is an alias for XCode's clang++. Now, when I install Homebrew's llvm oder gcc, there seems to be some dependency on the stuff provided by XCode.
Specifically, I'd like to know:
which headers and libraries are used if I use Homebrew's llvm/gcc?
in particular, which are the standard libraries used? I somewhere read that XCode's llvm doesn't use libc++ anymore.
how can I control that?
what parts from XCode are used?
which compatibility issues do I have to face?
is there a Homebrew-only build toolchain I can use?
I am sorry for this question, but after several build errors occurring with one compiler but not the other, I am a bit lost in how my system works. So far, I have installed XCode and Homebrew's llvm, and try compiling my program with either of them, with different outcome.

Can't compile with gcc-4.9

I eventually want to use valgrind to find what is causing the occasional bizarre output in a C program which refines a model against experimental data using OpenMP parallel programming.
To avoid the use of the nominal gcc (ie clang) compiler, I used brew to install gcc-4.9 on my MacPro running Yosemite (OS x 10.10.5). However, when trying to compile my program with gcc-4.9, with or without -fopenmp, I get numerous error messages of the type:
/var/folders/qc/1j0gr_l48xnfd9001s6tt6f80000gn/T//ccRxnrnU.s:30597:suffix
or operands invalid for `movq'
I have no idea what the problem triggering these error messages is. Can anyone help?
The following summarises what was worked out in the comments section and did lead to the issue being resolved. Not all steps may be necessary, but most are probably good practice.
Step 1 - Clean up
If you have been trying lots of different, potentially incompatible, methods to get OpenMP set up, it is probably a good idea to clean them up first. So, something like:
brew rm --force gcc # or maybe gcc#4.9
Step 2 - Update Xcode and Command Line Tools
If you have upgraded macOS since installing Xcode, it is probably advisable to update Xcode and its "Command Line Tools"
Consider uninstalling and re-installing Xcode - it is available for free from the App Store.
Update/install the "Command Line Tools" after installing/updating with:
xcode-select --install
Step 3 - Install gcc
Now, try installing gcc afresh, ensuring that you use the --without-multilib option:
brew install gcc#4.9 --without-multilib
Hopefully you can now compile OpenMP code with:
/usr/local/bin/gcc -fopenmp program.c -o program
I am unsure exactly why the --without-multilib option is needed and prefer to quote #hristo-iliev:
Multilib usually refers to the coexistence of both 64-bit and 32-bit
versions of each library so that 32-bit software could be run on
64-bit OS. In the GCC case that probably refers to having all GCC
runtime libraries in "fat" Mach-O format, i.e. versions for both i386
and x86_64 in the same shared library file. It could be that libgomp
(the GNU OpenMP runtime library) cannot be built in such a way.
See this question.
Keywords: gcc, g++, GNU Compiler, OpenMP, fopenmp, -fopenmp, Xcode, multilib, Command Line Tools, macOS, OSX, homebrew, brew

Where is the system llvm-config on macOS?

If I'm not mistaken, the compiler etc. installed as part of XCode use llvm. But I can't find an llvm-config binary to generate proper flags for command-line compilation to an llvm target. Where is such a thing on macOS?
I know I can install a new llvm via MacPorts or Homebrew, but I don't want to duplicate what's already there; I just want a way to get at the system configuration.
llvm-config is used to build programs that would link against LLVM.
Xcode does not distribute the LLVM libraries, and does not support linking against clang itself, so llvm-config is of no use in this context. What is your use case?

How to link homebrew gcc-4.x to gcc?

In OS X, how can I link gcc-4.8 from homebrew to gcc? I already tried to link gcc-4.8 to /usr/local/bin/gcc but this does not work. I really need this because I have shared makefiles in a course at the university.
There are at least a couple of options.
I have my GCC 4.8.2 installed under /usr/gcc/v4.8.2 and I ensure that /usr/gcc/v4.8.2/bin is on my PATH ahead of /usr/bin. That way, the undecorated name gcc means GCC 4.8.2. (This is on Mac OS X 10.9.1 Mavericks, just for the record.)
Specify CC=/usr/gcc/v4.8.2/bin/gcc on command lines, etc. Painful, so I don't regard it as a real option.
Make /usr/bin/gcc point to /usr/gcc/v4.8.2/bin/gcc instead of being an executable that runs clang.
I distrust modifying /usr/bin or /bin (or /sbin or /usr/sbin). I seldom use the system-provided Perl, for example, but always use my own build, using the same general technique.

Obtaining GCC for OSX with Developer Tools installed

I want to start working with C++0x. I see that GCC 4.7 has a fair amount of functionality available. I already have XCode 3.2 installed in /Developer
I downloaded: http://fileboar.com/gcc/snapshots/LATEST-4.7/gcc-4.7-20110528.tar.bz2
Can I somehow compile this in /opt/gcc-4.7? How do I then work with my path so I can compile with GCC 4.7 from the command-line but have OSX use the version it needs?
OSX does not need gcc to run - the Developer tools are optional. So you only need to choose between gcc's when you compile. In Xcode you chose explicitly the gcc andin Makefiles you can set $(CC) or similar to the full path.
Alternatively rename the gcc-4.7 gcc to gcc-4.7 and use that so gcc is always the Apple one.
For ease of using multiple C++ compilers I use macports (or fink or homebrew) which will compile the compilers with the correct patches and also has a port select command to switch between the C++ compilers

Resources