Q:

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

I have been trying to compile the following simple program:
#include <iostream>
using namespace std;
int main() {
std::cout << "Test compilation" << std::endl;
return 0;
}

I am using GGC 7 (installed via homebrew) on Mac OS X 10.11.6. 
Unfortunately, it does not build this simple test program (source file is 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

The configuration of my GCC is the following:
$ 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)

What do I need to change in order to have this simple test program compile and run?

A:

I'm not what what you're doing by attempting to compile a C++ program with a C compiler.
According to my experience, there shouldn't be such a problem if you use g++:
g++-7   -O0 -g -std=c++1z -pedantic -Wall test.cpp.cpp  -o test.cpp.out

I suggest that you change the rule from
test.out: test.cpp
    ${CC} ${CPPFLAGS} ...

To
test.out: test.cpp
    ${CXX} ${CPPFLAGS} ...

in your makefile to solve this problem.

