0

I have a game program and I am getting VERY frustrated. Everything was running fine, and I decided to clean up my program by making separate files for each set of functions. The code is very long with multiple files, but heres the basic idea:

Im on Windows XP using the Code::Blocks IDE

In my entity.h Ive declared all of my functions and variables for that class. In my entity.cpp Ive included it, as well as in all my other files. But Im still getting a huge list of errors that tell me I have an undefined reference to all of the methods in entity.h as well as all my other header files. For example, I have a function call print() to make it easier to print out things, and thats the first method I call from the entity.h file. I get this error:

Heres the code for print():

void print(string f) {
 cout<<f<<endl;
} 

How Im calling it:

void Player::win(){
entity e;
e.print("You have defeated the orc");

} The error:

In function 'ZN6Player3winEv': undefined reference to 'entity::print(std::string)'

And yes, I do have an object of entity.

CMP6LG
  • 83
  • 1
  • 3
  • 13

4 Answers4

3

Undefined reference errors happen at link time (as opposed to compile time). Your code seems to be compiling correctly, so your headers are probably correct. But you're not linking everything together to make the executable. You didn't mention the platform, compiler, or build system, so I can't tell you exactly how to fix it, but in general the compiler generates a *.o file for each *.cpp file (that's the compilation) and then links the *.o files together to create an executable. You need to make sure all your *.o files are being linked together (also, you might not be compiling one of your *.cpp files into a *.o file).

Coding Mash
  • 3,338
  • 5
  • 24
  • 45
Tom Panning
  • 4,613
  • 2
  • 26
  • 47
1

At a quick guess, you're not linking the entity.o file to the binary.

If you're using linux g++, then something like:

g++ -o binary entity.cpp game.cpp

should compile and link the cpp files to the binary.

For Code::Blocks to select the files that are within a specific build group, you need to right-click on the target application and select properties. This will pop-up the Project/target options. Go to the Build Targets tab. Towards the bottom-right of the page is a list of Build target files. You need to make sure that all the files that contribute to the executable are selected as part of your build target. This means that you need a check-box on the entity.cpp file. This ensures that the file that contains the code for entity::print is compiled and linked into the target executable. Please note that the Build target files tab applies to the selected build target in the list at the left-hand side. You will need to ensure that the file is checked for all the build targets that it belongs to.

In general, when you're adding files in Code::Blocks, and where you see the check-boxes for the targets that the file should be added to you should make sure that all the targets that the new file belong to are checked. The default from the wizard is to leave all the check-boxes unchecked. In your case, as it's a simple project, you should make sure that it is checked for each target (typically it will start with debug and release as the two targets).

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
1

I had the same problem. For me the solution was : When you add a file in your project, dont forget to click on "add to DEBUG" and "add to RELEASE", either the file will be in the project tree yes, but you will have error "undefined reference"

0

The problem is that originally you were defining a free function named print instead of a member function named print of class entity. You need to be in the scope of the class entity to define it as a member function which is what i did using scope resolution operator :: below.

/////vvvvvvvv
void entity::print(string f) {  //note the scope resolution operator:: used here
 cout<<f<<endl;
}
Jason
  • 36,170
  • 5
  • 26
  • 60