How to fix linker errors with gcc-11 on M1 Mac

On M1 Mac I installed gcc:

$ brew install gcc
$ brew link gcc

I want openmp and C++ 11. But when I try to compile even simple hello world:

#include <iostream>

int main() {
  std::cout << "Hello" << std::endl;
  return 0;
}

I get linker errors:

Undefined symbols for architecture arm64:
  "__ZNSolsEPFRSoS_E", referenced from:
      _main in cckQ2KfV.o
  "__ZNSt8ios_base4InitC1Ev", referenced from:
      __Z41__static_initialization_and_destruction_0ii in cckQ2KfV.o
  "__ZNSt8ios_base4InitD1Ev", referenced from:
      __Z41__static_initialization_and_destruction_0ii in cckQ2KfV.o
  "__ZSt4cout", referenced from:
      _main in cckQ2KfV.o
  "__ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_", referenced from:
      _main in cckQ2KfV.o
  "__ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc", referenced from:
      _main in cckQ2KfV.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status

How can I fix this?

1 answer

  • answered 2022-04-25 19:35 Joachim

    Ok, this was a simple mistake. I was using gcc-11. I should use g++-11.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum