使用2015版SFML

时间:2017-06-08 18:09:42

标签: c++ network-programming visual-studio-2017 sfml

所以我正在尝试编写一些与网络相关的代码,特别是使用SFML的端口扫描程序。我正在运行VS 2017,已经下载了2015年的SFML版本,但人们已经说过这应该适用于2017年。

到目前为止,我的演示代码试图查看库是否正常工作:

#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;
}

但是如果这有效的话,我就不会在这里。

  

严重级代码描述项目文件行抑制状态   错误LNK2019未解析的外部符号&#34; public:virtual __thiscall sf :: Socket :: ~Socket(void)&#34; (?? 1Socket @sf @@ UAE @ XZ)在函数&#34; public:virtual __thiscall sf :: TcpSocket :: ~TcpSocket(void)&#34;中引用(?? 1TcpSocket @sf @@ UAE @ XZ)PortScannerWin32 c:\ Users \ James \ documents \ visual studio 2017 \ Projects \ PortScannerWin32 \ PortScannerWin32 \ PortScanner.obj 1

     

错误LNK2019未解析的外部符号&#34; public:__ thishisall sf :: TcpSocket :: TcpSocket(void)&#34;函数&#34; bool __cdecl port_is_open(类std :: basic_string,类std :: allocator&gt; const&amp;,int)&#34;中引用了(?? 0TcpSocket @sf @@ QAE @XZ); (?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

     

错误LNK2019未解析的外部符号&#34; public:enum sf :: Socket :: Status __thiscall sf :: TcpSocket :: connect(class sf :: IpAddress const&amp;,unsigned short,class sf :: Time)& #34; (?connect @ TcpSocket @sf @@ QAE?AW4Status @ Socket @ 2 @ ABVIpAddress @ 2 @ GVTime @ 2 @@ Z)在函数&#34; bool __cdecl port_is_open中引用(类std :: basic_string,类std :: allocator &gt; const&amp;,int)&#34; (?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

     

错误LNK2019未解析的外部符号&#34; public:void __thiscall sf :: TcpSocket :: disconnect(void)&#34; (?disconnect @ TcpSocket @sf @@ QAEXXZ)函数&#34; bool __cdecl port_is_open(类std :: basic_string,类std :: allocator&gt; const&amp;,int)&#34; (?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

     

错误LNK2019未解析的外部符号&#34; public:__ thishisall sf :: IpAddress :: IpAddress(class std :: basic_string,class std :: allocator&gt; const&amp;)&#34; (?? 0IpAddress @ sf @@ QAE @ ABV?$ basic_string @ DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@@ Z)在函数&#34; bool __cdecl中引用port_is_open(class std :: basic_string,class std :: allocator&gt; const&amp;,int)&#34; (?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

     

错误LNK2001未解析的外部符号&#34; public:static class sf :: Time const sf :: Time :: Zero&#34; (?Zero @ Time @ sf @@ 2V12 @ B)PortScannerWin32 c:\ Users \ James \ documents \ visual studio 2017 \ Projects \ PortScannerWin32 \ PortScannerWin32 \ PortScanner.obj 1
  警告LNK4272库机类型&#39; x64&#39;与目标机器类型的冲突&#39; X86&#39; PortScannerWin32 D:\ SFML-2.4.2 \ lib \ sfml-network-d.lib 1

     

错误LNK1120 6未解析的外部端口PortScannerWin32 c:\ users \ james \ documents \ visual studio 2017 \ Projects \ PortScannerWin32 \ Debug \ PortScannerWin32.exe 1

这些是我的错误,如果我诚实,我根本就没有任何线索。我已逐字逐句地阅读了教程并输入了依赖项等。

任何人都知道出了什么问题?我会说明显的答案是2015年在VS 2017中使用,但是很多论坛都表示没有问题,因为SFML 2015中有更新允许这样做。

2 个答案:

答案 0 :(得分:3)

您没有链接到正确的库平台。

  

警告LNK4272库机类型&#39; x64&#39;与目标机器类型的冲突&#39; X86&#39; PortScannerWin32 D:\ SFML-2.4.2 \ lib \ sfml-network-d.lib 1

这表明当您的项目是32位时,您将链接到64位库。确保使用正确的库,或更新项目以将其构建为64位项目。

答案 1 :(得分:3)

我对这个库没有任何了解,但这些似乎是非常标准的链接器错误。将VS 2017(Platform Toolset v141)与使用VS 2015(Plaform Toolset v141)编译的库链接起来应该没有问题。微软在VS 2015和2017年的编译器之间保持了二进制兼容性,允许将编译的代码混合在同一个可执行文件中。

您的头文件包括(SFML/Network.hpp)&#34;承诺&#34;您正在使用的类的实现。链接工作是匹配&#34;承诺&#34;与实际的实现,包含在静态库(.lib)中,并将它们放在一起以创建您的exe。

请注意上次警告:

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

作为第一步,我会将Visual Studio中项目的平台值更改为x64,因此&#34; bitness&#34;该库与您的可执行文件匹配。这是本机机器代码,因此生成的代码必须是相同的架构。

请注意,更改此设置时,可能需要设置项目属性(如include和库路径)以及链接器输入,以便编译代码。您可以使用项目属性表将x64值设置为x86平台中的值。