Home > OS >  Compiling a C plus plus program using GCC compiler
Compiling a C plus plus program using GCC compiler

Time:12-31

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

CodePudding user response:

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.

  • Related