【发布时间】:2020-02-26 19:43:01
【问题描述】:
我在 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:警告:忽略文件 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd, 文件是为不受支持的文件格式构建的( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) 这不是 被链接的架构(x86_64): /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib/libSystem.tbd 架构 x86_64 的未定义符号:“___cxa_atexit”, 参考自: ccEBCemd.o ld 中的 __static_initialization_and_destruction_0(int, int):未找到架构 x86_64 collect2 的符号: 错误:ld 返回 1 个退出状态
g++ -v 说
使用内置规范。 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 目标:x86_64-apple-darwin18 配置为:../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 线程模型:posix gcc 9.2.0 版(自制 GCC 9.2.0_1)`
谢谢。
【问题讨论】:
-
你是怎么编译的?
-
我是这样编译的。
g++ test.cpp或g++ -o test test.cpp -
您是否为 Xcode 安装了 Apple 命令行工具?
-
在我的机器上工作,但我必须使用
g++-9而不是g++。 -
可能是路径问题,例如由命令行工具管理的 gcc 出现在你的 PATH 中,在 homebrew 安装的那个之前。见:apple.stackexchange.com/questions/245891/…