0

I'm trying to run this code using c++11 std::thread and the SFML library for networking, however I just cant get it to build. I'm pretty sure it has something to do with how I'm setting up the compiler, but I don't know... How can I get this to work? I've been messing about trying to get this to work, going through tutorials and such for hours now, so I have turned to Stack Overflow for answers. I'm using the code::blocks IDE and the x32-4.8.1-posix-dwarf-rev5 compiler on a windows7 64-bit machine.

#include <iostream>
#include <thread>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>


using namespace std;

void doNetwork() {
    cout << "Starting..." << endl;

    sf::TcpListener listener;
    listener.listen(88);

    // Wait for a connection
    sf::TcpSocket socket;
    listener.accept(socket);
    cout << "New client connected: " << socket.getRemoteAddress() << endl;

    // Receive a message from the client
    char buffer[1024];
    size_t received = 0;
    socket.receive(buffer, sizeof(buffer), received);
    cout << "The client said: " << buffer << endl;

    // Send an answer
    string message = "Welcome, client";
    socket.send(message.c_str(), message.size() + 1);
}

int main()
{
    thread task(doNetwork);
    task.join();
    return 0;
}

This is what the build debug log is saying:

-------------- Build: Debug in bang ---------------

Compiling: main.cpp
Linking console executable: bin\Debug\bang.exe
obj\Debug\main.o: In function `Z9doNetworkv':
C:/Users/user/CppProjects/bang/main.cpp:12: undefined reference to `sf::TcpListener::TcpListener()'
C:/Users/user/CppProjects/bang/main.cpp:13: undefined reference to `sf::TcpListener::listen(unsigned short)'
C:/Users/user/CppProjects/bang/main.cpp:16: undefined reference to `sf::TcpSocket::TcpSocket()'
C:/Users/user/CppProjects/bang/main.cpp:17: undefined reference to `sf::TcpListener::accept(sf::TcpSocket&)'
C:/Users/user/CppProjects/bang/main.cpp:18: undefined reference to `sf::TcpSocket::getRemoteAddress() const'
C:/Users/user/CppProjects/bang/main.cpp:18: undefined reference to `sf::operator<<(std::ostream&, sf::IpAddress const&)'
C:/Users/user/CppProjects/bang/main.cpp:23: undefined reference to `sf::TcpSocket::receive(void*, unsigned int, unsigned int&)'
C:/Users/user/CppProjects/bang/main.cpp:28: undefined reference to `sf::TcpSocket::send(void const*, unsigned int)'
obj\Debug\main.o: In function `ZN2sf11TcpListenerD1Ev':
C:/Users/user/CppProjects/SFML-2.1/include/SFML/Network/TcpListener.hpp:43: undefined reference to `sf::Socket::~Socket()'
obj\Debug\main.o: In function `ZN2sf9TcpSocketD1Ev':
C:/Users/user/CppProjects/SFML-2.1/include/SFML/Network/TcpSocket.hpp:46: undefined reference to `sf::Socket::~Socket()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
10 errors, 0 warnings

The settings I am using with the compiler:

Compiler Flags:

-std=C++0x

Other options:

-std=gnu++0x -U__STRICT_ANSI__

defines:

SFML_STATIC

Linker Settings:

sfml-system-s-d
sfml-network-s-d

Search Directories - Compiler:

C:\Users\user\CppProjects\SFML-2.1\include
C:\Users\user\CppProjects\bang\include

Search Directories - Linker:

C:\Users\user\CppProjects\SFML-2.1\lib\

Toolchain Executables:

-Compilers instalation dir:

C:\Program Files (x86)\mingw-builds\x32-4.8.1-posix-dwarf-rev5\mingw32

-C compiler:

i686-w64-mingw32-gcc-4.8.1.exe

-C++ Compiler:

i686-w64-mingw32-c++.exe

-Linker for dynamic libs:

i686-w64-mingw32-c++.exe

-Linker for static libs:

ar.exe

-Debugger:

gdb.exe

-Resource Compiler:

windres.exe

-Make Program:

mingw32-make.exe
nevernew
  • 650
  • 10
  • 23
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – chris Dec 22 '13 at 13:55
  • You're not linnking the sfml library that contains those symbols. Refer to the sfml documentation. – rubenvb Dec 22 '13 at 13:58
  • I have this in my global linker settings: C:\Users\user\CppProjects\SFML-2.1\lib\sfml-network-s-d.lib C:\Users\user\CppProjects\SFML-2.1\lib\sfml-system-s-d.lib and this in my search directories-linker: C:\Users\user\CppProjects\SFML-2.1\lib\ are these correct? – nevernew Dec 22 '13 at 14:03
  • @user1551924, Looking at a bit of CodeBlocks documentation would tell you that you need `sfml-network-s-d` and `sfml-system-s-d`. – chris Dec 22 '13 at 14:13
  • i've changed the linker settings to sfml-network-s-d and sfml-system-s-d, but i'm getting exactly the same errors when building – nevernew Dec 22 '13 at 15:02
  • can someone help me please? if anyone can get this code building could you let me know what settings you've used? – nevernew Dec 22 '13 at 17:13
  • Did you build the SFML libraries yourself with that same compiler version? – Casey Dec 22 '13 at 18:54

1 Answers1

1

I had a simular problem and how i solved this on a Linux system was: Project / build options

Go to linker settings tab. there i added: sfml-network

Done. Hope it helps

Snowing
  • 11
  • 1