c++compiler-errors

C++ : Undefined symbols for architecture x86_64 on MacOS Mountain Lion


Ok, I just switched to MacOS after a while of Windows/Linux coexistence. I open a simple text editor, write a simple program just to check if everything is all right. I know there are other threads about this error but it just seems that in all the other cases the problem lied in a specific part of their program, which in turn is far more complex than this. I just want to understand why the compiler says what it says even with a simple thing like this.

#include <iostream>
#include "numeri.h"
int main(void)

{

  std::cout << numeri() << std::endl;

  return 0;
}

with

//numeri.h

int numeri(void);

and

//numeri.cpp
#include "numeri.h"
int numeri(void)
{
  return (3);
}

what I get from gcc helloworld.cpp -Wall is

Undefined symbols for architecture x86_64:
  "numeri()", referenced from:
      _main in cc6WY2MJ.o
  "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 cc6WY2MJ.o
  "std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
      _main in cc6WY2MJ.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in cc6WY2MJ.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in cc6WY2MJ.o
  "std::cout", referenced from:
      _main in cc6WY2MJ.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 cc6WY2MJ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

2 cases:

1 - I forgot how to use a simple header (I prefix, I'm a physics student, I can program for what it regards physics and numeric simulations [or at least, I could], but I just don't know that much about linkers-compilers-architecture and this kind of stuff)

2 - something's wrong


Solution

  • The C++ compiler is g++, . Also you need to add numeri.cpp to your commmand line.

    g++ helloworld.cpp numeri.cpp -Wall