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.

3
  • 5
    How are you compiling? That's the issue. Commented Jul 4, 2012 at 14:21
  • There's a button that says "Build and Run". I click that and in the console it says "Compiling...". That's all I know. I'm using CodeBlocks. Commented Jul 4, 2012 at 14:22
  • Possible duplicate of stackoverflow.com/questions/5971206/… Commented Jul 4, 2012 at 14:41

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

7 Comments

How do I bring up the command line? Is that the command "prompt"?
@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.
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.
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.
@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.)
|
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"

2 Comments

I'm using CodeBlocks. So how would I do that??
Did you make sure that burrito.cpp belongs to the same project as your main file?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.