简体   繁体   中英

c++11 threads, SFML and code::blocks

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM