Reputation: 109
I'm having trouble linking my program. All the classes compile fine with
g++ -c main.cpp
g++ -c Server.cpp
g++ -c Client.cpp
But when I go to link them
g++ main.o Server.o Client.o -o main.out -lsfml-network -lsfml-system
I'm getting undefined references for the functions in my Client and Server classes.
main.cpp:(.text+0x1ba): undefined reference to `(anonymous namespace)::Server::Server()'
main.cpp:(.text+0x1c6): undefined reference to `(anonymous namespace)::Server::getMessage()'
main.cpp:(.text+0x210): undefined reference to `(anonymous namespace)::Server::~Server()'
main.cpp:(.text+0x227): undefined reference to `(anonymous namespace)::Client::Client()'
main.cpp:(.text+0x23d): undefined reference to `(anonymous namespace)::Client::sendMessage(std::string const&)'
main.cpp:(.text+0x287): undefined reference to `(anonymous namespace)::Client::~Client()'
main.cpp:(.text+0x3e9): undefined reference to `(anonymous namespace)::Server::~Server()'
main.cpp:(.text+0x407): undefined reference to `(anonymous namespace)::Client::~Client()'
collect2: error: ld returned 1 exit status
Any help is much appreciated.
main.cpp
#include <iostream>
#include <cstdio>
#include <string>
#include "include/Server.hpp"
#include "include/Client.hpp"
void printOptions(std::string const* const, size_t const&);
char const getInput(std::string const* options, size_t const& size);
int main()
{
const std::string YES_NO[] = {
"Yes",
"No"
};
const std::string OPTIONS[] = {
"Server",
"Client"
};
const std::string CONTINUE = "Continue?\n\n";
const std::string PROMPT = "Run as?\n";
const std::string RECIPEINT = "127.0.0.1";
const size_t SIZE = sizeof(OPTIONS) / sizeof(std::string);
std::cout << PROMPT;
const char INPUT = getInput(OPTIONS, SIZE);
char response;
// Server
if (INPUT == '1')
{
Server server;
do
{
server.getMessage();
std::cout << CONTINUE;
response = getInput(YES_NO, 2);
} while (response == '1');
}
// Client
else if (INPUT == '2')
{
Client client;
do
{
client.sendMessage(RECIPEINT);
std::cout << CONTINUE;
response = getInput(YES_NO, 2);
} while (response == '1');
}
// else serious problem
}
/* Function used to display a list of options to the user.
Each option is displayed on a new line preceeded with
its input number and provided option text.
Ex:
options[] = {"Option A", "Option B"}
Will print:
1) Option A
2) Option B
@param options
An array of std::string that will be displayed
as the list of options.
@param size
The number of different options the options array contains.
*/
void printOptions(std::string const* options, size_t const& size)
{
for (size_t i = 0; i < size; i++)
{
std::cout << i + 1 << ") ";
std::cout << options[i] << std::endl;
}
}
/* Used to return a users choice from a list of options.
*WARNING*
Providing an array with more than 9 options will give
unexpected return values.
If it is necesszary to provide a user with mroe than 9 options
have the 9th option be "More options..." from which you may call
getInput() again, with additonal options.
@param options
An array of std::string that will be displayed
for the users choice.
@param size
The number of different optins the optinos array contains.
@return const char
A number from 1 to 9 representing the users choice from
the options array.
1 = array index 0, 9 = array index 8.
*/
const char getInput(std::string const* options, size_t const& size)
{
printOptions(options, size);
char input;
bool needInput = true;
while (needInput)
{
std::cout << "Enter a number: ";
std::cin >> input;
for (size_t i = 0; i < size; i++)
{
char optionBuffer[2];
std::sprintf(optionBuffer, "%zu", i + 1);
if (input == *optionBuffer)
{
needInput = false;
break;
}
}
if (needInput)
std::cout << "Option not available." << std::endl;
}
return input;
}
Server.hpp
#pragma once
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
namespace
{
class Server
{
public:
Server();
~Server();
static const unsigned short SERVER_PORT = 54000;
void getMessage();
protected:
private:
sf::UdpSocket socket;
};
};
Server.cpp
#include "include/Server.hpp"
//using namespace;
Server::Server()
{
this->socket.bind(SERVER_PORT);
}
Server::~Server() {}
// TODO: Ensure that the socket is bound to a port.
void Server::getMessage()
{
sf::IpAddress sendersAddress;
unsigned short sendersPort;
sf::Packet sendersPacket;
// Failed to recieve packet
if (this->socket.receive(sendersPacket, sendersAddress, sendersPort)
!= sf::Socket::Done
)
{
std::cout << "Failed to recieve packet." << std::endl;
}
// Sucessfully recievd packet
else
{
std::string message;
sendersPacket >> message;
std::cout << "Recieved message:\n\n" << message << std::endl;
}
}
Client.hpp
#pragma once
#include "Server.hpp"
#include <SFML/Network.hpp>
namespace
{
class Client
{
public:
Client();
~Client();
void sendMessage(std::string const&);
protected:
private:
sf::UdpSocket socket;
};
};
Client.cpp
#include "include/Client.hpp"
//using namespace;
Client::Client()
{
this->socket.bind(sf::Socket::AnyPort);
}
Client::~Client()
{
}
void Client::sendMessage(std::string const& recipient)
{
std::string message;
sf::Packet packet;
std::cout << "Write a message:\n" << std::endl;
do
{
std::getline(std::cin, message);
} while (!message.size());
socket.send(packet, recipient, Server::SERVER_PORT);
}
Upvotes: 1
Views: 3254
Reputation: 5766
The problem is that your Server
and Client
class declarations are inside unnamed (or anonymous) namespaces. To fix it, simply give your namespaces a name, such as:
namespace MyNamespace
{
class Server
{
// ...
};
}
In the *.cpp
files (including main.cpp
), make sure you have a corresponding using
directive, such as:
using namespace MyNamespace;
Alternatively, you can fully-qualify the names when you use them, e.g. MyNamespace::Server
instead of just Server
.
As a side note, unnamed namespaces are actually valid and sometimes very useful. When the compiler sees a namespace without a name, it comes up with a unique internal name for it, and immediately follows it with a hidden using namespace ...
directive. That's very useful for things which you want to define and use only within a single *.cpp
file, because it can help avoid naming conflicts. (An older way to do something similar involved the static
keyword).
As a rule though, don't use unnamed namespaces in a header file.
Upvotes: 3