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

GCC 7 on Mac OS X doesn't find C++ standard library

本文关键字:找不到 C++ 标准 GCC OS 上的 Mac      更新时间:2023-10-16

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

#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)
我需要

更改什么才能编译和运行这个简单的测试程序?

我不是你尝试用 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} ...

在您的制作文件中解决此问题。