1

I installed gcc by brew on Mac OS mojave, but both of gcc and g++ are not working.

I tried compile below simple code by g++ but I got error messages.

This program can be compiled correctly by default clang, so I am wondering there is any library link error. But I am not sure.

I got same error in the case of gcc.

#include <iostream>
using namespace std;

int main(){
    int i;
    cin >> i;
    cout << i * 2 + 1 << endl;
} 

ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, file was built for unsupported file format ( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) which is not the architecture being linked (x86_64): /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd Undefined symbols for architecture x86_64: "___cxa_atexit", referenced from: __static_initialization_and_destruction_0(int, int) in ccEBCemd.o ld: symbol(s) not found for architecture x86_64 collect2: error: ld returned 1 exit status

g++ -v says

Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/9.2.0_1/libexec/gcc/x86_64-apple-darwin18/9.2.0/lto-wrapper Target: x86_64-apple-darwin18 Configured with: ../configure --build=x86_64-apple-darwin18 --prefix=/usr/local/Cellar/gcc/9.2.0_1 --libdir=/usr/local/Cellar/gcc/9.2.0_1/lib/gcc/9 --disable-nls --enable-checking=release --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-9 --with-gmp=/usr/local/opt/gmp --with-mpfr=/usr/local/opt/mpfr --with-mpc=/usr/local/opt/libmpc --with-isl=/usr/local/opt/isl --with-system-zlib --with-pkgversion='Homebrew GCC 9.2.0_1' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-multilib --with-native-system-header-dir=/usr/include --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk Thread model: posix gcc version 9.2.0 (Homebrew GCC 9.2.0_1) `

Thank you.

fukino
  • 11
  • 4
  • How do you compile? – Thomas Sablik Oct 31 '19 at 12:37
  • 1
    I compiled it like this. `g++ test.cpp` or `g++ -o test test.cpp` – fukino Oct 31 '19 at 13:11
  • 1
    Did you install Apple Command Line Tools for Xcode? – Thomas Sablik Oct 31 '19 at 13:18
  • Works on my machine, but I have to use `g++-9` not `g++`. – Eljay Oct 31 '19 at 13:52
  • It might be a path issue e.g. the gcc managed by command line tools appears in your PATH before the one installed by homebrew. See: https://apple.stackexchange.com/questions/245891/installed-gcc-with-homebrew-now-how-to-use-that-gcc-instead-of-clang – Chris B Oct 31 '19 at 14:01
  • I added symbolic link like `ln -s /usr/local/bin/g++-9 /usr/local/bin/g++`. And I added `export PATH="/usr/local/bin:$PATH"` to bash_profile. I also added alias as the link you mentioned. – fukino Oct 31 '19 at 14:30
  • It looks like the sysroot is set to the command line tools SDK. Try compiling with `--sysroot=/` e.g. `g++ --sysroot=/ -o test test.cpp` – Chris B Oct 31 '19 at 16:15

2 Answers2

0

To conclude, the error solved by updating Xcode. Thank you for your help.

As you commented, I tried to g++ --sysroot=/ -o test test.cpp, then I got a new error such as

fatal error: wchar.h: No such file or directory #include ^~~~~~~~~

Also I tried to compile the code having only int main(){} without any library. Then this was succeeded.

From this, I understood there is a problem related in standard library. This problem sometimes happened discussed at Can't compile C program on a Mac after upgrade to Mojave.

Anyway, the solution for this kind of problem seemes to be updating or reinstalling Xcode or gcc.

Thanks!

fukino
  • 11
  • 4
0

I noticed your error log: file was built for unsupported file format ( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) which is not the architecture being linked (x86_64)

I guess you should reinstall your gcc & g++ with command: arch -x86_64 brew install gcc, which will install them in x86_64 architecture.

LiuYuan
  • 127
  • 9