0

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
polar
  • 194
  • 1
  • 15
  • 2
    can we see the makefile ? the header ? the full error ? – Jeffrey Jul 07 '20 at 02:57
  • yeah of course here let me make the edit – polar Jul 07 '20 at 04:19
  • The link command issue has just one object file. That's an issue: `g++ server_chat.o -o server` it should list all object files at once – Jeffrey Jul 07 '20 at 12:35
  • @Jeffrey Do you know how to fix it to include all object files besides moving my source code all into the same directory? I'm lousy with makefiles. – polar Jul 07 '20 at 19:01
  • I don't know anything about makefiles. But https://stackoverflow.com/questions/2483182/recursive-wildcards-in-gnu-make suggests `FLAC_FILES = $(shell find flac/ -type f -name '*.flac')` maybe `sources = $(shell find . -type f -name '*.cpp')` will work for you ? – Jeffrey Jul 07 '20 at 19:15

0 Answers0