WoooHaaaa
WoooHaaaa

Reputation: 20460

Why my C++ class definition fails?

main.cpp

#include <iostream>
#include "Burrito.h"

using namespace std;

int main(){
  Burrito b;
  return 0;
}

Burrito.h

#ifndef BURRITO_H
#define BURRITO_H

class Burrito{
    public:
      Burrito();
};

#endif

Burrito.cpp

#include "Burrito.h"
#include <iostream>

Burrito::Burrito(){

}

Compile & Link :

lzsb$ g++ main.cpp -o main
Undefined symbols for architecture x86_64:
  "Burrito::Burrito()", referenced from:
      _main in ccVpCr0z.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
lzsb$ 

Platform:

Mac OS 10.6.8

G++ : i686-apple-darwin10 --with-gxx-include-dir=/usr/include/c++/4.2.1

Upvotes: 2

Views: 360

Answers (2)

Haatschii
Haatschii

Reputation: 9319

You need to compile the Burrito.cpp file as well. The compiler creates object files from each .cpp file and links them afterwards. This is where your call fails, because the linker can't find the referenced Burrito class in any of your object files. To fix your compiler call just add Burrito.cpp

g++ main.cpp Burrito.cpp -o main

Upvotes: 10

PAntoine
PAntoine

Reputation: 689

Your compile line should be:

g++ Burrito.cpp main.cpp -o main

Upvotes: 4

Related Questions