which gcc am I using? - c++

I am attempting to compile a package on my Mac laptop and my Mac Mini desktop. It compiles successfully on the laptop, but not the mini (gives the following error: gcc: error: unrecognized command line option '-Xarch_x86_64'). Both machines are running OS X Yosemite (10.10.2). On both machines, when I type
which gcc
I get:
gcc: aliased to nocorrect gcc
(I don't know what this means) When I type
echo $PATH
I get:
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
This makes me think that if a gcc is found in /usr/local/bin, that's the one that will be my gcc. When I type
ls -l /usr/local/bin/gcc
I get:
/usr/local/bin/gcc -> /usr/local/bin/gcc-4.8
on both machines. However, when I type
gcc --version
on the Mini I get:
gcc (Homebrew gcc48 4.8.4) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
and on the laptop I get:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.1.0
Thread model: posix
Which is the version installed by XCode. (This is what I get on both machines from /usr/bin/gcc --version)
How is the laptop using gcc 4.2 when the first gcc in my path is a symlink to gcc 4.8, and how do I make the mini do the same?

The nocorrect prefix is a zsh construct that will inhibit spelling correction.
It looks like you may have installed a specific gcc version with homebrew on the Mac Mini and you're using the system compiler (or rather, the one installed with XCode command line tools) on the laptop.
If you don't want to use the gcc installed with homebrew, you can just do:
brew unlink gcc
That will make the symlink go away and you'll use the next compiler. Which is hopefully the one you want.

When which gcc returns something like gcc: aliased to nocorrect gcc, the way to get the actual path would be to unalias foo, like this:
$ unalias gcc
$ which gcc
/usr/bin/gcc
This will remove the alias for the current shell, and you'll have to open up a new terminal or re-alias it yourself.
If you'd rather not do that, then you can use bash to determine the where the command is coming from:
$ bash -c 'which gcc'
/usr/bin/gcc

Related

How to configure g++ as the default compiler in Mac OS (M1)

So, I wanted to use some header files native to GNU C++:
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
I read that in MacOS, gcc and g++ are both linked to clang. So, we had to install the gcc using homebrew and use that.
But after installing gcc using homebrew. When I run
g++ --version
I get
Apple clang version 12.0.5 (clang-1205.0.22.9)
Target: arm64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
But running g++-12 --version I get:
g++-12 (Homebrew GCC 12.2.0) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
My VSCode runs g++ (Apple One) to compile C/C++ files. For the goal I wanted to accomplish at the start, I read that we need to have the g++ (installed using homebrew) do the compiling.
So, I ran the following commands:
cd /opt/homebrew/bin
ls -s g++-12 g++
But now, even when I compile the following code:
#include <iostream>
int main()
{
std::cout << 1;
}
I get the following error:
In file included from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/bits/postypes.h:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iosfwd:40,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ios:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/ostream:38,
from /opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/iostream:39,
from test.cpp:1:
/opt/homebrew/Cellar/gcc/12.2.0/include/c++/12/cwchar:44:10: fatal error: wchar.h: No such file or directory
44 | #include <wchar.h>
| ^~~~~~~~~
compilation terminated.
Now, removing the link using rm g++ reverts back to my original configuration. But that configuration can't run the headers I requested at the start. Is there any way to solve this?
Most of the above have mostly been taken from here. But I didn't find any solution. But, I have seen people using the same and getting successful.
EDIT:
I found a website where there was a solution. It was to compile not using g++. Rather use
g++-12 --sysroot=$(xcrun --show-sdk-path)
When I used this, it solved the problem. Can someone explain why this happened?
The header error likely indicates the g++ you installed via Homebrew may not be compatible with the Apple Xcode version installed in the macos system directories.
The solution is probably to reinstall one or both packages.
EDIT:
g++-12 --sysroot=$(xcrun --show-sdk-path) changes the search path for system header includes from the default (which was probably set when homebrew installed g++) to the one provided by the Xcode SDK currently installed.

Cannot cross-compile for arm with c++17

I am trying to cross-compile c++14/c++17 code for my raspberry pi. I am sure it worked on my old PC. However, when I try to compile it on my new pc, it returns me the following error.
$ arm-linux-gnueabihf-g++ main.cpp -std=c++17
arm-linux-gnueabihf-g++: error: unrecognized command line option ‘-std=c++17’
I thought it is the compiler issue, the version of the compiler I am using is:
$ arm-linux-gnueabihf-g++ --version
arm-linux-gnueabihf-g++ (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03) 4.8.3 20140303 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I try to get the newest compiler with
sudo apt-get install libc6-armel-cross libc6-dev-armel-cross binutils-arm-linux-gnueabi libncurses5-dev build-essential bison flex libssl-dev bc
Yet, the version is still the same, which can only compile c++11.
How can I update the arm-linux-gnueabi-g++ compiler?? I am pretty sure there is a compiler that works since I used it before, but not sure how I get it.
I found the easiest way to solve this issue is to directly install the newest cross-compiler
sudo apt-get install g++-9-arm-linux-gnueabihf
and use the compiler as (just an example of using c++17)
arm-linux-gnueabihf-g++-9 main.cpp -std=c++17

How do I make sure that my default C/C++ compiler is GCC

I'm trying to install Riak from source on macOS (https://docs.riak.com/riak/kv/2.2.3/setup/installing/mac-osx.1.html#installing-from-source).
There is a note:
Riak will not compile with Clang. Please make sure that your default
C/C++ compiler is GCC
How do I find out which compiler is the default and how to change it?
macOS Catalina (10.15.4), which command prints:
$ which clang
/usr/bin/clang
$ which gcc
/usr/bin/gcc
On macOS Catalina (and prior versions, and most likely subsequent versions too), there are two aspects to the problem and some suggested solutions.
What is the name of the compiler used by make by default?
$ mkdir junk
$ cd junk
$ > x.cpp
$ > y.c
$ make x y
c++ x.cpp -o x
cc y.c -o y
$ cd ..
$ rm -fr junk
This shows that the names used by make are cc and c++. Those are not obviously clang or clang++, but neither are they obviously gcc and g++.
$ which cc c++
/usr/bin/cc
/usr/bin/c++
$
Which compiler is it really?
Which compiler really lives behind the names cc, c++, gcc, g++, clang, and clang++? We can check which compiler these really are by getting them to identify their version:
$ for compiler in cc c++ gcc g++ clang clang++
> do
> which $compiler
> $compiler --version
> done
/usr/bin/cc
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/c++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/gcc
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/g++
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
/usr/bin/clang++
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$
As you can see, the versions installed in /usr/bin are all the same compiler, and that compiler is clang or clang++.
This was run on a machine with macOS Mojave 10.14.6 and XCode 11.3.1. The latest version of XCode — 11.4.1 — is only available on Catalina. However, the general conclusion is the same — all the C and C++ compilers are really clang and clang++ in disguise.
How do you get GNU GCC onto your machine?
How do you get a real GNU GCC — a real GCC, not clang in disguise — onto your machine?
Use Brew to install GCC (I've not checked which version of GCC is current).
Or use MacPorts (again, I've not checked which version of GCC is current).
If you're adventuresome, do it yourself (but I've not yet succeeded at building GCC 9.3.0 on Catalina; I have a GCC 9.2.0 built on macOS Mojave 10.14.x that works OK on Catalina though — with one environment variable needed to locate the headers).
Maybe Fink — it lists GCC 8.4 as being made available in 2020; I don't know about newer versions.
Be aware that Apple has taken to hiding the system header files miles out of the way (not in /usr/include — and you can't modify that part of the file system to add a symlink to where they've hidden them):
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
(You mean you couldn't guess that? Me neither!)
How do you change the default compiler?
Once you have GCC installed somewhere appropriate, you need to ensure you use the 'real' GCC and not the 'fake' in /usr/bin. You do that in part by ensuring that the bin directory for the 'real' GCC occurs on your PATH before /usr/bin. I have GCC 9.3.0 installed under /opt/gcc/v9.3.0, so /opt/gcc/v9.3.0/bin appears on my PATH long before /usr/bin does.
You also need to ensure that the configuration for riak (the software you're installing) uses the correct compilers. If there's a ./configure script, run it with the correct path specified for the compilers. For example, I might use:
./configure CC=/opt/gcc/v9.3.0/bin/gcc CXX=/opt/gcc/v9.3.0/bin/g++
You can also set these values as environment variables.
If it uses cmake or some other configuration package, you'll need to consult the installation instructions. That's usually README or sometimes INSTALL.
See also (increasingly older posts):
Can't compile a C program on a Mac after upgrade to Catalina
Can't compile a C program on a Mac after upgrade to Mojave
Install GNU GCC on a Mac

OS X Yosemite gcc unrecognized command line option 'mdll' after brew install gcc

I installed gcc using brew because I needed to install brew's Fortran package. It seems that the gcc package installed by brew install gcc does not support the mdll flag. How can I install the correct versions of Fortran and gcc?
$ which gcc
/usr/local/bin/gcc
$ gcc --version
gcc (GCC) 4.9.2 20141029 (prerelease)
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gcc -mdll
gcc: error: unrecognized command line option ‘-mdll’
gcc: fatal error: no input files
compilation terminated.
According to the gcc(1) manpage:
-mdll
This option is available for Cygwin and MinGW targets. It
specifies that a DLL---a dynamic link library---is to be generated,
enabling the selection of the required runtime startup object and
entry point.
Since OS X isn't either Cygwin or MinGW, it's safe to assume that no version of GCC will enable it on OS X.
If you're trying to build a dynamically linked object ("shared object" in UNIX terminology), check out the -dynamic/-dynamiclib flags. They seem to be the rough equivalent on OS X.

"-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