0
votes

So I am trying to write some network related code, specifically a port scanner using SFML. I am running VS 2017 and have downloaded the SFML build for 2015 however people have said this should work with 2017.

My demo code so far to try and see if the library is working is:

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

using namespace std;

bool port_is_open(const string& address, int port) {
    sf::TcpSocket socket;
    bool open = (socket.connect(sf::IpAddress(address), port) == sf::Socket::Done);
    socket.disconnect();
    return open;
}

int main() {
    cout << "Enter IP number: ";
    string address;
    cin >> address;

    cout << "Enter Port number: ";
    int port;
    cin >> port;

    if (port_is_open(address, port)) {
        cout << "Port " << port << ": OPEN" << endl;
    }
    else {
        cout << "Port " << port << ": CLOSED" << endl;
    }


    return 0;
}

But I wouldn't be here if this was working.

Severity Code Description Project File Line Suppression State Error LNK2019 unresolved external symbol "public: virtual __thiscall sf::Socket::~Socket(void)" (??1Socket@sf@@UAE@XZ) referenced in function "public: virtual __thiscall sf::TcpSocket::~TcpSocket(void)" (??1TcpSocket@sf@@UAE@XZ) PortScannerWin32 c:\Users\James\documents\visual studio 2017\Projects\PortScannerWin32\PortScannerWin32\PortScanner.obj 1

Error LNK2019 unresolved external symbol "public: __thiscall sf::TcpSocket::TcpSocket(void)" (??0TcpSocket@sf@@QAE@XZ) referenced in function "bool __cdecl port_is_open(class std::basic_string,class std::allocator > const &,int)" (?port_is_open@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) PortScannerWin32 c:\Users\James\documents\visual studio 2017\Projects\PortScannerWin32\PortScannerWin32\PortScanner.obj 1

Error LNK2019 unresolved external symbol "public: enum sf::Socket::Status __thiscall sf::TcpSocket::connect(class sf::IpAddress const &,unsigned short,class sf::Time)" (?connect@TcpSocket@sf@@QAE?AW4Status@Socket@2@ABVIpAddress@2@GVTime@2@@Z) referenced in function "bool __cdecl port_is_open(class std::basic_string,class std::allocator > const &,int)" (?port_is_open@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) PortScannerWin32 c:\Users\James\documents\visual studio 2017\Projects\PortScannerWin32\PortScannerWin32\PortScanner.obj 1

Error LNK2019 unresolved external symbol "public: void __thiscall sf::TcpSocket::disconnect(void)" (?disconnect@TcpSocket@sf@@QAEXXZ) referenced in function "bool __cdecl port_is_open(class std::basic_string,class std::allocator > const &,int)" (?port_is_open@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) PortScannerWin32 c:\Users\James\documents\visual studio 2017\Projects\PortScannerWin32\PortScannerWin32\PortScanner.obj 1

Error LNK2019 unresolved external symbol "public: __thiscall sf::IpAddress::IpAddress(class std::basic_string,class std::allocator > const &)" (??0IpAddress@sf@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function "bool __cdecl port_is_open(class std::basic_string,class std::allocator > const &,int)" (?port_is_open@@YA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) PortScannerWin32 c:\Users\James\documents\visual studio 2017\Projects\PortScannerWin32\PortScannerWin32\PortScanner.obj 1

Error LNK2001 unresolved external symbol "public: static class sf::Time const sf::Time::Zero" (?Zero@Time@sf@@2V12@B) PortScannerWin32 c:\Users\James\documents\visual studio 2017\Projects\PortScannerWin32\PortScannerWin32\PortScanner.obj 1
Warning LNK4272 library machine type 'x64' conflicts with target machine type 'X86' PortScannerWin32 D:\SFML-2.4.2\lib\sfml-network-d.lib 1

Error LNK1120 6 unresolved externals PortScannerWin32 c:\users\james\documents\visual studio 2017\Projects\PortScannerWin32\Debug\PortScannerWin32.exe 1

Those are my errors and if I am honest, I literally have no clue at all. I have followed the tutorials word by word and typed in the dependencies etc.

Anyone have any idea what is going wrong? I would say the obvious answer is 2015 being used in VS 2017 but a lot of forums have said that it works with no problems as there was an update in the SFML 2015 that allowed this.

2
Sounds like you haven't linked the library.Sneftel
In Linker->General I have filled out Additional Libraries Directories with D:\SFML-2.4.2\lib, is that not it linked? And I have typed in the dependencies, is there something else I must do?HCF3301

2 Answers

3
votes

I don't have any knowledge of this library, but these seem like pretty standard linker errors. There should be no problem linking VS 2017 (Platform Toolset v141) with libraries compiled with VS 2015 (Plaform Toolset v141). Microsoft have kept binary compatibility between the compilers in VS 2015 and 2017, which allows mixing code compiled with either together in the same executible.

The header file your are including (SFML/Network.hpp) "promises" implementations for the classes you are using. It's the linker's job to match up the "promises" with the actual implementations, contained in a static lib (.lib), and put them together to create your exe.

Note the last warning:

Warning LNK4272 library machine type 'x64' conflicts with target machine type 'X86' PortScannerWin32 D:\SFML-2.4.2\lib\sfml-network-d.lib 1

As a first step, I would change the platform value on your project within Visual Studio to x64, so the "bitness" of the library matches that of your executible. This is native machine code, so the generated code must be for the same architecture.

Note that when changing this, you might need to set project properties such as the include and library paths, as well as linker inputs, so that your code compiles. You can use the project property sheets to set the x64 values to those in the x86 platform.

3
votes

You are not linking against the correct library platform.

Warning LNK4272 library machine type 'x64' conflicts with target machine type 'X86' PortScannerWin32 D:\SFML-2.4.2\lib\sfml-network-d.lib 1

This states that you are linking against a 64bits library while your project is in 32bits. Make sure to take the correct library, or update your project to build it as a 64bits project.