2

我最近再次开始使用 C++ 应用程序。我使用 NetLink for Sockets 和 Visual Studio 进行开发。

我做了一个小代码,但我遇到了一些错误。我已经包含了来自 NetLink 的文件,并添加了 ws2_32.lib(在链接器/通用和编译指示注释中),但我仍然遇到这些错误:

Error   1   error LNK2019: unresolved external symbol "void __cdecl NL::init(void)" (?init@NL@@YAXXZ) referenced in function _main  c:\Users\Joshua\documents\visual studio 2012\Projects\OBPP\OBPP\Main.obj
Error   4   error LNK2019: unresolved external symbol "public: class NL::Socket * __thiscall NL::Socket::accept(void)" (?accept@Socket@NL@@QAEPAV12@XZ) referenced in function "private: virtual void __thiscall OnAccept::exec(class NL::Socket *,class NL::SocketGroup *,void *)" (?exec@OnAccept@@EAEXPAVSocket@NL@@PAVSocketGroup@3@PAX@Z)  c:\Users\Joshua\documents\visual studio 2012\Projects\OBPP\OBPP\Main.obj
Error   6   error LNK2019: unresolved external symbol "public: bool __thiscall NL::SocketGroup::listen(unsigned int,void *)" (?listen@SocketGroup@NL@@QAE_NIPAX@Z) referenced in function _main c:\Users\Joshua\documents\visual studio 2012\Projects\OBPP\OBPP\Main.obj
Error   5   error LNK2019: unresolved external symbol "public: __thiscall NL::SocketGroup::SocketGroup(void)" (??0SocketGroup@NL@@QAE@XZ) referenced in function _main  c:\Users\Joshua\documents\visual studio 2012\Projects\OBPP\OBPP\Main.obj
Error   2   error LNK2019: unresolved external symbol "public: __thiscall NL::Socket::Socket(unsigned int,enum NL::Protocol,enum NL::IPVer,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,unsigned int)" (??0Socket@NL@@QAE@IW4Protocol@1@W4IPVer@1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z) referenced in function _main  c:\Users\Joshua\documents\visual studio 2012\Projects\OBPP\OBPP\Main.obj
Error   3   error LNK2019: unresolved external symbol "public: __thiscall NL::Socket::~Socket(void)" (??1Socket@NL@@QAE@XZ) referenced in function _main    c:\Users\Joshua\documents\visual studio 2012\Projects\OBPP\OBPP\Main.obj
Error   7   error LNK1120: 6 unresolved externals   c:\users\joshua\documents\visual studio 2012\Projects\OBPP\Debug\OBPP.exe

我正在使用的代码(只是想尝试接受):

#include <iostream>

#include "netlink\socket.h"
#include "netlink\socket_group.h"

#pragma comment(lib, "Ws2_32.lib")
#define PORT 30000

class OnAccept: public NL::SocketGroupCmd {
        void exec(NL::Socket* socket, NL::SocketGroup* group, void* reference) {
                NL::Socket* newConnection = socket->accept();
                group->add(newConnection);
            std::cout << "\nConnection " << newConnection->hostTo() << ":" << newConnection->portTo() << " added...";
            std::cout.flush();
    }
};

int main()
{
    NL::init();
    NL::Socket s(PORT);
    NL::SocketGroup group;

    group.setCmdOnAccept(&OnAccept());

    group.add(&s);

    if (group.listen(2000))
    {
            std::cout << "Listening on Sockets, port " << PORT << std::endl;
    }

    getchar();
}
4

2 回答 2

1

I am the developer of the library. It can't be used with the headers only, you need to link the static/dynamic compiled library.

You have to build the library first, to generate the .lib file. Once you have the netLink.lib file built (or whatever you name it) you have to add that dependency to the linker.

于 2014-04-24T18:12:01.273 回答
0

我不是 Visual Studio 开发人员,但错误消息表示unresolved external symbol链接器无法找到某些符号。看看你的链接命令是否包含 Netlink 库?

于 2014-04-11T21:30:29.447 回答