C++11 on Mac with Clang or GCC - c++

I have Xcode 4.5.2 on Moutain Lion, and I have install the lastest "Command Line Tools" but when I tried to compile with g++ or clang++ (and the options -std=c++11 -stdlib=libc++) I get an error.
With g++:
cc1plus: error: unrecognized command line option "-std=c++11"
cc1plus: error: unrecognized command line option "-stdlib=libc++"
With clang++:
clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later)
It's in a Qt project.
So how can I used the C++11 on my Mac ?

As you found, g++ does not support those command line options.
It sounds like you're using Xcode.
For clang, you should look at the project settings, and make sure that the "Deployment Target" is set to 10.7 (or 10.8)
What the error message is telling you is that libc++ is not available for 10.6 and before.

I installed gcc-4.7 on my Mac to make C++11 work. GCC in its current version is fairly good at supporting C++11, so this should be a fair choice.
The installation can be done by Homebrew and is not that complicated (at least I was able to do it...)
To install Homebrew if you do not already have it:
ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
Now run
brew doctor
and fix whatever problems come up (there is something written in the hombrew documentation for that). Finally, install current gcc:
brew install gcc
If everything goes well you should be able to access g++-4.7, which allows -std=c++0x.

Try -std=c++0x if c++11 doesn't work. Support for the -std=c++11 option is relatively new in GCC and you might not have a recent enough version.
I'd trust Marshall on the libc++ issue.

Related

clang++ installed via homebrew (macOS): compilation errors

After installing clang++ (tried v. 6.0.1 and 7.0) with:
brew install --with-toolchain llvm
very trivial programs result to the following error:
In file included from test.cpp:1:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/iostream:38:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/ios:215:
In file included from /usr/local/Cellar/llvm/7.0.0/include/c++/v1/iosfwd:90:
/usr/local/Cellar/llvm/7.0.0/include/c++/v1/wchar.h:119:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
Command used to compile:
clang++7() {
LDFLAGS="-L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib"
CPPFLAGS="-I/usr/local/opt/llvm/include"
/usr/local/opt/llvm/bin/clang++ -std=c++11 $CPPFLAGS $LDFLAGS $1
}
Is it possible to use the official clang instead of Apple's version?
With Apple's version, we do not even know which version of LLVM it really is...
It appears that as of Mojave (10.14), Xcode doesn't install system headers in /usr/include anymore. There is a compatibility package that does, but it's not recommended.
Instead, the official solution is for tools to search for headers in /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk. That path can be obtained from
xcrun --show-sdk-path
The release notes say
The Command Line Tools package installs the macOS system headers inside the macOS SDK. Software that compiles with the installed tools will search for headers within the macOS SDK provided by either Xcode at:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk
or the Command Line Tools at:
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
depending on which is selected using xcode-select.
If you built clang yourself, this can be achieved by passing the -isysroot option to clang:
clang++ -isysroot "$(xcrun --show-sdk-path)" …
See also: https://github.com/Homebrew/homebrew-core/issues/32765
It works for me to add a -I (minus eye) option to the clang++ command line, pointing to /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include

installing gfortran in cygwin: gfortran: cyglto_plugin.dll not found

I'm trying to get the gfortran compiler with cygwin. When attempting to compile a hello world program, I get the following error:
gfortran: fatal error: -fuse-linker-plugin, but cyglto_plugin.dll not found
compilation terminated.
To install the compiler, I used the cygwin setup and selected
gcc-fortran: GNU compiler collection
libgfortran3: GCC fortran runtime library
I also mistakenly selected the toolchains for mingw, even though I don't have the mingw compiler (as far as I'm aware). g++, which I installed awhile ago, works fine.
What do I need to do to compile/find the library?
It is because of the gcc and gfortran versions are not same, try checking that. If so, do the following
$ apt-cyg remove gcc-fortran
$ apt-cyg install gcc-fortran

Compile OpenMP programs with gcc compiler on OS X Yosemite

$ gcc 12.c -fopenmp
12.c:9:9: fatal error: 'omp.h' file not found
#include<omp.h>
^
1 error generated.
While compiling openMP programs I get the above error. I am using OS X Yosemite. I first tried by installing native gcc compiler by typing gcc in terminal and later downloaded Xcode too still I got the same error. Then I downloaded gcc through:
$ brew install gcc
Still I'm getting the same error. I did try changing the compiler path too still it shows:
$ which gcc
/usr/bin/gcc
So how do I compile programs with gcc?
EDIT: As of 13 Aug 2017 the --without-multilib option is no longer present in Homebrew and should not be used. The standard installation
brew install gcc
will provide a gcc installation that can be used to compile OpenMP programs. As below it will be installed into /usr/local/bin as gcc-<version>. The current gcc version available from Homebrew (as of writing) will install as gcc-8. You can compile programs with OpenMP support using it via
gcc-8 -fopenmp hello.c
Alternatively you could put an alias in your .bashrcfile as
alias gcc='gcc-8'
and then compile using
gcc -fopenmp hello.c
Note: I'm leaving the original post here in case it is useful to somebody.
The standard gcc available on OS X through XCode and Clang doesn't support OpenMP. To install the Homebrew version of gcc with OpenMP support you need to install it with
brew install gcc --without-multilib
or as pointed out by #Mark Setchell
brew reinstall gcc --without-multilib
This will install it to the /usr/local/bin directory. Homebrew will install it as gcc-<version> so as not to clobber the gcc bundled with XCode.
I finally did some research and I finally came across a solution here: <omp.h> library isn't found in the GCC version (4.2.1) in Mavericks.
I got a new gcc complier from http://hpc.sourceforge.net/
Then I placed a new executable folder by
$ sudo tar -xvf gcc-4.9-bin.tar -C /
Later I switched to it by
export PATH=/usr/local/bin:$PATH that seemed to do the trick!

Eclipse /bin/sh: g++: command not found

I had to upgrade my gcc version to utilize the new C++11 standards. After following the instructions at http://ask.xmodulo.com/upgrade-gcc-centos.html . I started getting the error "/bin/sh: g++: command not found" when I try to build my project. How can I fix this? It looks like gcc installed fine by looking at the below output.
gcc --version
gcc (GCC) 4.7.2 20121015 (Red Hat 4.7.2-5)
G++ appears to be not installed. Find how to install gcc in your OS. After this, you have to make Eclipse find g++. I recommend to you, to install CDT plugins. They have many ready tools for C/C++ programming and it is fully compatible with gcc toolset.

"-std=gnu++0x"option for MacOS

I'm trying to compile a CMake project which uses
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-Wall -std=gnu++0x")
in the CMakeLists.txt file under MacOS X Lion. I have installed XCode 4.2.1. but the compiler fails with this:
cd something/src/lib && /usr/bin/c++ -Dlib_ginacra_EXPORTS -Wall -std=gnu++0x -fPIC -o CMakeFiles/lib_ginacra.dir/utilities.cpp.o -c something/src/lib/utilities.cpp
cc1plus: error: unrecognized command line option "-std=gnu++0x"
The compiler's verion is:
c++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
GCC 4.2 is ancient, but Apple don't ship a newer version.
You can either install a modern GCC from somewhere like Mac Ports (which is probably simpler and quicker) or build it yourself following the instructions at http://gcc.gnu.org/wiki/InstallingGCC
For Lion users facing this issue:
Download and Install the MacPorts-2.2.1-10.7-Lion.pkg MacPorts
installer package from here
in a terminal, search for newer GCC versions:
$ port search --name --glob 'gcc*'
install a newer version (I went with gcc5)
$ sudo port install gcc5
get the name of your new version, and set it as default
$ port select --list gcc
Available versions for gcc:
llvm-gcc42
mp-gcc5
none (active)
$ sudo port select --set gcc mp-gcc5
open a new terminal and validate you're updated:
$ c++ --version
c++ (MacPorts gcc5 5.2.0_0) 5.2.0
Most of you getting that error "cc1plus: error: unrecognized command line option -std=gnu++0x" while installing nodejs extension which requires C++ compilation with node-gyp.
So how to solve this error so here is the solution. Basically you get these errors because of Nodejs different version as many node libraries requires C or C++ compilation while installing. So Nodejs older version uses python 2.7 with gcc compiler less than version 4.2 but Nodejs newer version uses gcc44 compiler that's why you get above error while installing any nodejs library.
So you need to degrade your nodejs and node-gyp version and specify the python version if you have multiple python versions installed on your system and then you will not get above error anymore.
Click here to see full tutorial