0

I am trying to compile this little program in file demo.cpp by gcc demo.cpp,

#include <iostream>
using namespace std;

int main() {
  cout << "Hello World!";
  return 0;
}

It does not do so successfully. I get Undefined symbols for architecture arm64:,

This seems like pretty popular error but people are getting it on way more complicated programs. Note that it works if I comment the cout line. I am not sure exactly what am I doing wrong here. gcc -v returns,

Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
scribe
  • 673
  • 2
  • 6
  • 17
  • 10
    Use `g++` to compile c++ code. [What is the difference between g++ and gcc?](https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc) – Retired Ninja Dec 31 '22 at 00:58
  • I was reading [here](https://www.w3schools.com/cpp/cpp_getstarted.asp), "A compiler, like GCC, to translate the C++ code into a language that the computer will understand." Could you explain a little if `g++` is still called a "GCC" compiler? – scribe Dec 31 '22 at 01:02
  • @scribe: Read the link and you'd have known. It's the same compiler, just with different implicit switches (with `g++` switches being the defaults you want to compile C++ code). – ShadowRanger Dec 31 '22 at 01:23
  • Better to use clang++. – sweenish Dec 31 '22 at 01:28
  • GCC is a generic term for the 'GNU Compiler Collection', the gcc command uses the GCC c compiler, g++ uses the GCC c++ compiler, gfortran invokes the GCC fortran compiler.... – pm100 Dec 31 '22 at 01:46
  • @ShadowRanger, the link was added as an edit after I'd already commented. I did read it. – scribe Dec 31 '22 at 02:11

1 Answers1

-1

This is probably because the GCC compiler, according to my experience, is supposed to compile C language codes only, not C++. G++ is used to compile C++. The specific problem with the cout line is that C uses printf(), not cout, to print to the terminal. Therefore, the C compiler outputs your line as undefined symbols.

Hudson
  • 312
  • 2
  • 18