OpenMP on Mac not working anymore - c++

I had OpenMP compiling and executing in C/C++ on my Mac but then I formatted and reinstalled a fresh copy of OS X. I believe the only difference in the setup was that I had Xcode 5 before the format, and now I have Xcode 6.
Now I get fatal error: 'omp.h' file not found
I read this has something to do with clang and gcc, but, my confusion lies with why did it work before and now its not working?

After some research I seem to have found an answer.
GCC included in the latest version of Xcode (Xcode 6) is only a symbolic link to clang. Since clang does not support OpenMP at this time, you need to install a different version of GCC. The easiest way to do this would be to use Homebrew or MacPorts.
Keep in mind that even when you do this you will probably need to alter your $PATH to have /usr/local come before /usr/bin. This is because Homebrew will have placed your newly installed GCC in /usr/local. Also, some implementations may name the command gcc-49 instead of plain gcc.

Mac OSX uses clang.The gcc compiler in OS X does not support OpenMP. To use this feature a new gcc compiler needs to be installed.
Go to Terminal, if you have not installed Hombrew, install it:
/usr/bin/ruby -e "$(curl -fsSL https://`enter code here`raw.githubusercontent.com/Homebrew/install/master/install)"
then install new version of gcc
brew reinstall gcc --without-multilib
This will not make changes to the existing gcc compiler installed by Xcode as we are reinstalling it.
After running the command given below compile the files using the new version of gcc using the syntax : gcc-version -fopenmp filename.c
To find the version type gcc and then press tab. This will list out all the possible variants of gcc. The version number can be found out from this. For example : gcc-6, gcc-4.9,etc

Related

How to install gcc 11.2.0 on macOS Big Sur with the Apple M1 processor

How do I install the latest version of GCC on my macOS Big Sur. I am using Visual Studio Code, version 1.60 and I want to run C++ programs using it. I tried using homebrew to install GCC but it kept on giving me errors.
E.g.: When I typed in the path as /opt/homebrew/Cellar/gcc/11.2.0/bin
Result: zsh: permission denied: /opt/homebrew/Cellar/gcc/11.2.0/bin
What is wrong with the permission? How will I make it allow.
Firstly, /opt/homebrew/Cellar/gcc/11.2.0/bin is a directory, so you can't run that.
Secondly, homebrew generally makes symbolic links in /usr/local/bin for everything it installs, so you should add that to your PATH, e.g.
export PATH=/usr/local/bin:$PATH
Then you need to look in /usr/local/bin to see what program name you need, e.g.
ls /usr/local/bin/gcc*
and if you see gcc-11 in there, you then need to compile with:
gcc-11 program.c
Note you will need to look for g++* if you actually mean C++ rather than C.

How to change MacOS Default from clang to homebrew gcc

I'm having trouble finding how to change the default c++ compiler from clang to the homebrew version of gcc I have installed. The end goal here is to be able to run a command line gradle build using gcc.
I've checked to make sure that the first directory in the PATH variable is /usr/local/bin as suggested in some places, but it still defaults to clang/LLVM

Can I have gcc/g++ on MacOS X 10.11 pointing to the ACTUAL gcc/g++?

This is really annoying. For some reason, on MacOS X 10.11 (probably also on previous versions) there are gcc and g++ commands (in /usr/bin, they are not aliases or so) which, when executed with the -v argument, give:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
So it looks like they actually execute the clang and clang++ compilers by apple. Now, I really need my computer to execute gcc and g++ when I invoke those commands, both from the terminal and through makefiles.
The reasons for this are two:
I like to have my computer do what I ask it to do.
Apparently clang++ compiles stuff using a different c++ standard library from g++, and this is causing me problems when I compile and link my stuff with g++-5 (the ACTUAL g++, installed via homebrew) against CppUnit.
Does anybody know what is the best way to have gcc and g++ actually call gcc and g++?
Include in your PATH, before /usr/bin, a directory that contains a symbolic link named gcc pointing to /…/bin/gcc-5.
The latest Mac OS X does not let you change /usr, from what I hear, so this conservative solution is the only one available.
when I invoke those commands, both from the terminal and through makefiles.
If you adjust your PATH variable in your .profile, both these cases will be covered.
Apple does not actually provide gcc or g++, although (perhaps only misguided) they make aliases to pretend that clang is the same.
You can install gcc and g++ with MacPorts (also with homebrew). I use MacPorts, which puts its executables in /opt/local/bin.
With MacPorts, I see these currently-available ports (programs that have to be compiled to work on one's machine), using this command
port list |grep gcc
gcc410 #5-20140817 lang/gcc410
gcc43 #4.3.6 lang/gcc43
gcc44 #4.4.7 lang/gcc44
gcc45 #4.5.4 lang/gcc45
gcc46 #4.6.4 lang/gcc46
gcc47 #4.7.4 lang/gcc47
gcc48 #4.8.5 lang/gcc48
gcc49 #4.9.3 lang/gcc49
gcc5 #5.2.0 lang/gcc5
gcc6 #6-20151129 lang/gcc6
gccxml-devel #20150423 lang/gccxml-devel
gcc_select #0.1 sysutils/gcc_select
gccmakedep #1.0.3 x11/gccmakedep
According to its webpage, brew would do something similar, but install into /usr/local/bin.
When I installed MacPorts, its installer updated my ~/.profile, adding this to update PATH:
# MacPorts Installer addition on 2015-10-03_at_14:17:30: adding an appropriate PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.
Each of those ports from MacPorts installs gcc with a different name, and the port script has a feature select which establishes a symbolic link, e.g., from gcc to gcc49. brew has something similar. According to How can I brew link a specific version?, you would use
brew switch gcc-package-name package-version
e.g., (guessing at a valid name)
brew switch gcc 4.9

How to use previous versions of g++

I am currently running a debian Jessy whose g++'s version is 4.9. For some reason I need to compile a code in g++-4.7 or previous version.
I got the files of gcc-4.7 and g++-4.7 from a debian wheezy of a friend who has g++-4.7.
I tried to make the apt-get install, it seemed to have worked for the gcc but not for the g++. I put the files in the /bin, but he doesn't seem to locate the g++-4.7 package.
When I try to compile my code I specify g++-4.7 but get the error :
g++: error trying to exec 'cc1plus': execvp: No such file or directory
Any idea how to figure this out?
My advise is to add the wheezy repositories to /etc/apt/sources.list and then install g++-4.7 using apt-get. Using this method you will also get bugfixes etc.
I guess, currently you're just having dependency Problems. These will be solved when you use apt-get.
It is very easy to install gcc from sources. Remember that you have also to use a binutils version which maps to the gcc version. Mostly it is possible to run older gcc versions on actual binutils, but I have also seen a problem during install.
I have installed a long list of gcc versions in /opt/
Simply copy an older gcc version somewhere in the file system can result in problems with using the correct library versions.
If you install different gcc versions and also the related libraries e.g.libstdc++ , don't forget to update your library data base ( ldconfig/LD_LIBRARY_PATH/...) Maybe http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html can help.

Using homebrew, gcc and llvm with C++ 11

Here's my problem: I want to use C++11 features provided by either gcc or clang. However, I have these requirements:
I'm using a mac
I'm dependent on a bunch of libraries provided by homebrew (and really don't want to compile them myself). Specifically OSG, which itself is dependent on a ton of other libraries. And boost, though I can always compile that myself.
Homebrew seems to only want to use gcc (please correct me if I'm wrong). I can't find any options to switch to LLVM instead. While I understand that this might be due to the fact that not all libraries are compatible with LLVM yet, this would still be a nice feature for those that are.
The version of gcc that comes pre-installed on a mac of gcc is 4.2. gcc 4.2 doesn't have the c++11 features required. I've installed 4.7 via homebrew, but searches for how to set homebrew to use it all say don't do it (gcc 4.2 on the mac is not the vanilla version, so the 4.7 version I got won't be able to compile some things).
My questions are: Does anyone have any suggestions or fixes they have implemented to get around this problem? Should I give up on Homebrew? Does anyone know if Homebrew has a plan to switch to LLVM in the future? Does anyone have any upgrade-plan for how to deal with these incompatibilities?
I don't see how homebrew can continue to depend on gcc 4.2 in the long run, but haven't found any real discussion on this matter.
The default GCC on Mac is not real GCC of GNU. It's LLVM-GCC in fact, which is a branch of GCC. Several years ago, LLVM-GCC was terminated, and replaced with DragonEgg, which is a GCC plugin to use LLVM as a GCC backend.
LLVM-GCC is just a compiler frontend, whose role is using GCC frontend to translate the source code into LLVM IR[Intro to LLVM 11.3]. Once IR generated, LLVM backend will use it to generate binary code. This step has nothing to do with GCC.
The above goal was fully achieved from 10.7, whose components were all compiled by clang, a frontend provided by LLVM.
But Apple still kept LLVM-GCC and GCC runtime libraries. I guess its purpose might be providing a opportunity to compile some code GCC ONLY.
Now let's answer your questions:
If you want to use C++11 features, use clang++ -stc=c++11 -stdlib=libc++ instead. And clang might have already supported all c++11 features.
If you want homebrew supporting LLVM, it has already supported, at least on backend.
If you want homebrew using clang as a compiler frontend, it depends on homebrew community schedule. For example, you can append --with-c++11 argument to use clang to compile boost.But you cannot use this argument when brew install autoconf. In fact, some components might not be compiled correctly by clang.
If you know it can be compiled by clang but homebrew hasn't supported yet, you have to hack the corresponding ruby script at $HOMEBREW_ROOT/Library/Formula directory. Fortunately, in most of cases, replacing ./configure blablabla with ./configure blablabla CXX=clang++ -stc=c++11 -stdlib=libc++ works well. And by the way, if your hack is successful, please make a pull request to homebrew.
So, try it and have a fun.
I have an OS X Mountain Lion environment and use C++11. In this answer I'll break your requirement for not compiling your own stuff.
I use Homebrew and, I must say, I advise you to give up on depending on it to provide you clang and libc++ and all its formulas built with them.
What I've done, and I like, is
clone llvm, clang and libc++ from repositories.
install to /opt/local and put /opt/local/bin at top on /etc/paths.
build my development stuff with my new clang.
let Homebrew for installing tools like git and things I'll not develop for, just use.
I've followed clang build instructions for installing it to /opt/local.
For libc++, one detail: after running the buildit script, I've symlinked the include directory to /opt/local/lib/c++/v1 (clang on /opt/local looks for this as default directory), and also symlinked the libs to /opt/local/lib/ (but look that binaries will not automatically link to libc++ on /opt/local/lib. You must use install_name_tool for that).
use
clang++ -std=c++11 -stdlib=libc++
you can also install latest gcc from homebrew-dups
brew install [flags] https://raw.github.com/Homebrew/homebrew-dupes/master/gcc.rb
For LLVM, brew install --HEAD llvm. To use clang or a brew-installed gcc, add --with-clang or --with-gcc=gcc-x.x where x.x is the version you want.