I am having a problem with my linker in a game project I am working on. I have created all of the game logic code in one directory and the networking code in another. My project directory looks like this:
CardProject
GameLogic
makefile
player.h
player.cpp
test_gameLogic.cpp
Networking
server
makefile
server.cpp
client
I have separate makefiles for GameLogic and Networking. The problem occurs when I try to create a Player object in server.cpp. I first build everything in the GameLogic directory and then try to build everything in the Networking directory. When I build the contents of the Networking directory I get a linker error complaining about not finding the player constructor implemented in player.cpp.
I suspect that the makefile in server needs to be modified to be able to find the .o files created when building the GameLogic directory. If you know how to make such a makefile or if you can identify the problem as something else please let me know.
Thanks!
makefile for server (the one in GameLogic is the exact same except app_name = main):
app_name = server
sources = $(wildcard *.cpp)
objects = $(sources:.cpp=.o)
CPP = g++
CPPFLAGS = -Wall -Wextra -pedantic -std=c++17 -std=c++17
$(app_name) : $(objects)
$(CPP) $^ -o $@
clean :
rm -f $(app_name) $(objects)
include statement in server.cpp:
#include "../../GameTheory/player.h"
player.h: (abridged)
#ifndef PLAYER_H
#define PLAYER_H
namespace game {
class Player {
public:
Player() {};
Player(int);
private:
int player_id_;
};
} // game
#endif
player.cpp: (abridged)
#include "player.h"
game::Player::Player(int newID) {
this->player_id_ = newID;
}
linker error:
g++ server_chat.o -o server
Undefined symbols for architecture x86_64:
"game::Player::Player(int)", referenced from:
chat_server::do_accept()::'lambda'(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>)::operator()(boost::system::error_code, boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::executor>) const in server_chat.o
ld: symbol(s) not found for architecture x86_64