“-std=gnu++0x”option for MacOS

2020-03-24 07:33发布

问题:

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)

回答1:

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



回答2:

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


回答3:

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