Mac OS X上的GCC 7找不到C ++标准库

时间:2018-01-06 01:09:31

标签: c++ macos gcc homebrew

我一直在尝试编译以下简单程序:

#include <iostream>
using namespace std;
int main() {
std::cout << "Test compilation" << std::endl;
return 0;
}

我在Mac OS X 10.11.6上使用GGC 7(通过自制软件安装)。

不幸的是,它没有构建这个简单的测试程序(源文件是test.cpp):

$ make test.out 
gcc-7   -O0 -g -std=c++1z -pedantic -Wall -Wextra -Wno-unused-variable -Wno-sign-compare -Wno-missing-braces -Wmissing-field-initializers
 -Werror=implicit test.cpp.cpp  -o test.cpp.out

Undefined symbols for architecture x86_64:

  "std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:

      _main in ccJgvOlx.o

  "std::ios_base::Init::Init()", referenced from:

      __static_initialization_and_destruction_0(int, int) in ccJgvOlx.o

  "std::ios_base::Init::~Init()", referenced from:

      __static_initialization_and_destruction_0(int, int) in ccJgvOlx.o

  "std::cout", referenced from:

      _main in ccJgvOlx.o

  "std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>>&)", referenced from:

      _main in ccJgvOlx.o

  "std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char>>&, char const*)", referenced from:
      _main in ccJgvOlx.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make: *** [test.out] Error 1

我的GCC的配置如下:

$ gcc-7 -v
Using built-in specs.
COLLECT_GCC=gcc-7
COLLECT_LTO_WRAPPER=/usr/local/Cellar/gcc/7.2.0/libexec/gcc/x86_64-apple-darwin15.6.0/7.2.0/lto-wrapper
Target: x86_64-apple-darwin15.6.0
Configured with: ../configure --build=x86_64-apple-darwin15.6.0 --prefix=/usr/local/Cellar/gcc/7.2.0 --libdir=/usr/local/Cellar/gcc/7.2.0/lib/gcc/7 --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-7 --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 --enable-checking=release --with-pkgversion='Homebrew GCC 7.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --disable-nls
Thread model: posix
gcc version 7.2.0 (Homebrew GCC 7.2.0)

为了让这个简单的测试程序编译并运行,我需要更改什么?

1 个答案:

答案 0 :(得分:0)

尝试使用C编译器编译C ++程序时,我不是你正在做的事情。

根据我的经验,如果您使用g++

,应该不会出现这样的问题
g++-7   -O0 -g -std=c++1z -pedantic -Wall test.cpp.cpp  -o test.cpp.out

我建议你改变规则

test.out: test.cpp
    ${CC} ${CPPFLAGS} ...

test.out: test.cpp
    ${CXX} ${CPPFLAGS} ...
在你的makefile中

来解决这个问题。