0

This is a tutorial I've been following and I've done everything that it tells but it doesn't work. I have three files: the main.cpp, burrito.h (the class), and burrito.cpp.

And here are the three files respectively.

main.cpp

#include <iostream>
#include "Burrito.h"
using namespace std;

int main() {

    Burrito bo;

    return 0;
}

Burrito.h

#ifndef BURRITO_H
#define BURRITO_H


class Burrito {
    public:
        Burrito();
};

#endif // BURRITO_H

Burrito.cpp

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

using namespace std;

Burrito::Burrito() {
    cout << "Hello World" << endl;
}

When I build and run I get the following error:

...undefined reference to `Burrito::Burrito()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 6 seconds)
1 errors, 0 warnings

I'm compiling using CodeBlocks.

Tadeusz Kopec for Ukraine
  • 12,283
  • 6
  • 56
  • 83
David G
  • 94,763
  • 41
  • 167
  • 253

3 Answers3

5

I'm using CodeBlocks

This is the issue.

If you’re starting to learn C++ it’s (unfortunately) essential to learn about translation units. An IDE like Code::Blocks hides this detail from you – and does it wrong, in this case (this isn’t really the fault of Code::Blocks though, it can’t automatically guess what to do in this case without configuation).

In the beginning, drop the IDE, go to the command line for compiling. Compile the two translation units separately and link them together explicitly.

g++ -o burrito.o burrito.cpp
g++ -o main.o main.cpp
g++ -o main main.o burrito.o

Every good beginners’ C++ book will explain how this works.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • How do I bring up the command line? Is that the command "prompt"? – David G Jul 04 '12 at 14:28
  • @David Yes. You are probably under Windows? Unfortunately, the command line under Windows sucks (it’s essentially stuck in the last millenium) – but you can either use the PowerShell or `cmd.exe` which you can start via the start menu. – Konrad Rudolph Jul 04 '12 at 14:29
  • Okay so I went in there, typed in the first of the three lines you gave me, and it said `g++ is not recognized as an internal or external command, operable program or batch file`. I'm really new and I don't know anything about computers or programming. – David G Jul 04 '12 at 14:33
  • In this case, I'd use a single command: `g++ -o main burrito.cpp main.cpp`. Or, with the Microsoft compiler: `cl /EHs main.cpp burrito.cpp`. Although somewhat more complicated, I'd also recommend adding the options necessary to turn the compiler into a C++ compiler (rather than compiling something that's almost C++): `-std=c++98 -pedantic` for g++; I'm not sure for `cl`. – James Kanze Jul 04 '12 at 14:35
  • @David You need to add the appropriate directory to the `path` system variable. _Normally_, I would expect this to be done automatically when installing the software, but Microsoft doesn't do it for their compiler, and apparently, wherever you got `g++` from didn't do it for g++ either. So go to the `start` menu, open up settings->control panel->system->advanced->environment variables, and add the appropriate directories to the `path`. (In the case of `cl`, there are other environment variables that need to be set as well. Microsoft makes things difficult.) – James Kanze Jul 04 '12 at 14:40
  • Okay. I opened up everything. And I'm here -- http://gyazo.com/79211093e6ff57d13ddfc82cfd4c65bb.png?1341412768 So how do I add the appropriate directories to the path? – David G Jul 04 '12 at 14:50
  • @David MS adds shortcut to the start menu, that starts cmd and sets up the variables for cl (and other tools). I think the basic idea is, that they don't clutter the path for that stuff - could have something to do with their configuration... – MFH Jul 04 '12 at 20:03
1

When you're linking objects together to get the final executable file you're forgetting to link the compiled-object from burrito.cpp file correctly

If you're building with a Makefile, your final output rule should have something like "-o main main.o burrito.o"

Salepate
  • 327
  • 3
  • 11
0

Using Code Blocks 13.12 I right clicked on the Burritto.cpp file chose Properties, then chose the Build tab and checked the compile file and link file check boxes then clicked ok saved everything then ran and it worked great.

Hillid
  • 1