I have some code that compiles, but won’t link. I’m not sure why this is – I know that this issue is usually solved by fixing what’s linked together, but I don’t know why this is breaking. TransGame is a class that extends Game, and SearchGame is a class that extends TransGame.
Here’s the error I’m getting:
mrdmnd@dr-wily:~/ConnectFour$ make all
g++ -c -o SearchGame.o SearchGame.cc
g++ -c -o TransGame.o TransGame.cc
g++ -c -o Game.o Game.cc
g++ SearchGame.o TransGame.o Game.o -o SearchGame
TransGame.o: In function `TransGame::hash()':
TransGame.cc:(.text+0x5f): undefined reference to `Game::positioncode()'
collect2: ld returned 1 exit status
make: *** [SearchGame] Error 1
Here’s my makefile:
CFLAGS = -c -Wall
SOURCES = SearchGame.cc TransGame.cc Game.cc
OBJECTS = $(SOURCES:.cc=.o)
EXECUTABLE = SearchGame
.PHONY : all clean
all : $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $(OBJECTS) -o $@
*.o :
$(CXX) $(CFLAGS) $< -o $@
clean:
rm -rf *.o
Here are the relevant bits of code:
TransGame.cc
#include "Game.h"
#include "TransGame.h"
// snip
void TransGame::hash() {
long htemp = positioncode();
//snip
}
TransGame.h
#ifndef TRANSGAME_H
#define TRANSGAME_H
//snip
class TransGame : public Game {
public:
//snip
void hash();
//snip
};
#endif
Game.cc
#include "Game.h"
// snip
inline long Game::positioncode() {
return color[nplies & 1] + color[0] + color[1] + BOTTOM;
}
Game.h
#ifndef GAME_H
#define GAME_H
//snip
class Game {
public:
//snip
long positioncode();
//snip
};
#endif
Any help would be greatly appreciated – I’m sure I’m missing something obvious in my makefile.
Your function
Game::positioncode()is an inlined function and needs to be in the header, since the compiler needs to know its definition to replace the call topositioncodewith the code.There are two ways: You can either add the function below the definition of
class Gameor you can add it directly to the declaration: