c++ - 在Mac mojave上安装的gcc和g++无法正常工作(简装)

标签 c++ gcc g++ homebrew

我在Mac OS mojave上通过brew安装了gcc,但gcc和g++均无法正常工作。

我尝试通过g++在简单代码下进行编译,但收到错误消息。

默认情况下,可以使用clang正确编译该程序,
所以我想知道是否有任何库链接错误。
但是我不确定。

在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说

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) `



谢谢。

最佳答案

总之,通过更新Xcode解决了该错误。
感谢您的帮助。

如您所言,我尝试g++ --sysroot=/ -o test test.cpp
然后我遇到了一个新的错误,例如

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



我也尝试编译只有int main(){}而没有任何库的代码。然后成功了。

由此,我了解到标准库中存在一个问题。
有时在Can't compile C program on a Mac after upgrade to Mojave中讨论了此问题。

无论如何,解决此类问题的方法似乎是更新或重新安装Xcode或gcc。

谢谢!

关于c++ - 在Mac mojave上安装的gcc和g++无法正常工作(简装),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58643485/

相关文章:

c++ - 模板元编程 : Primitive Recursive?

c++ - 手动创建 gnu_unique_object 符号

c++ - g++ 可变大小数组没有警告?

windows - VisualC++/vmg/vms 的 G++ 等效项

c++ - Cassandra C++ 驱动程序 "undefined reference"错误

c++ - 清空 vector 的特定元素

c++ - 使用 qmake 查找编译器供应商/版本

c++ - C++ 中的用户定义运算符

c - 尝试编译 TPC-H Benchmark 并返回此错误 ld : library not found for -lgcc

c++ - C++ 成员函数中的 "if (!this)"有多糟糕?