当前位置: 首页 > 知识库问答 >
问题:

未解析的外部符号LNK2019

吕高昂
2023-03-14

首先,我知道这个问题在这个网站上到处都是,但是我看了几乎所有的问题,似乎找不到问题所在。这是在VS 2012中。谢谢。

//Socket.h
#pragma once

#include <iostream>
#include <WinSock2.h>

using namespace std;

const int STRLEN = 256;

class Socket
{
    protected:
        WSADATA wsaData;
        SOCKET mySocket;
        SOCKET myBackup;
        SOCKET acceptSocket;
        sockaddr_in myAddress;
    public:
        Socket();
        ~Socket();
        bool SendData( char* );
        bool RecvData( char*, int );
        void CloseConnection();
        void GetAndSendMessage();
};

class ServerSocket : public Socket
{
    public:
        void Listen();
        void Bind( int port );
        void StartHosting( int port );
};

class ClientSocket : public Socket
{
    public:
        void ConnectToServer( const char *ipAddress, int port );
};

这是插座。cpp公司

//Socket.cpp


#include "stdafx.h"
#include "Socket.h"


Socket::Socket()
{
    if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
    {
        cerr<<"Socket Initialization: Error with WSAStartup\n";
        system("pause");
        WSACleanup();
        exit(10);
    }

    //Create a socket
    mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

    if ( mySocket == INVALID_SOCKET )
    {
        cerr<<"Socket Initialization: Error creating socket"<<endl;
        system("pause");
        WSACleanup();
        exit(11);
    }

    myBackup = mySocket;
}

Socket::~Socket()
{
    WSACleanup();
}

bool Socket::SendData( char *buffer )
{
    send( mySocket, buffer, strlen( buffer ), 0 );
    return true;
}

bool Socket::RecvData( char *buffer, int size )
{
    int i = recv( mySocket, buffer, size, 0 );
    buffer[i] = '\0';
    return true;
}

void Socket::CloseConnection()
{
    //cout<<"CLOSE CONNECTION"<<endl;
    closesocket( mySocket );
    mySocket = myBackup;
}

void Socket::GetAndSendMessage()
{
    char message[STRLEN];
    cin.ignore();//without this, it gets the return char from the last cin and ignores the following one!
    cout<<"Send > ";
    cin.get( message, STRLEN );
    SendData( message );
}

void ServerSocket::StartHosting( int port )
{
     Bind( port );
     Listen();
}

void ServerSocket::Listen()
{
    //cout<<"LISTEN FOR CLIENT..."<<endl;

    if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
    {
        cerr<<"ServerSocket: Error listening on socket\n";
        system("pause");
        WSACleanup();
        exit(15);
    }

    //cout<<"ACCEPT CONNECTION..."<<endl;

    acceptSocket = accept( myBackup, NULL, NULL );
    while ( acceptSocket == SOCKET_ERROR )
    {
        acceptSocket = accept( myBackup, NULL, NULL );
    }
    mySocket = acceptSocket;
}

void ServerSocket::Bind( int port )
{
    myAddress.sin_family = AF_INET;
    myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
    myAddress.sin_port = htons( port );

    //cout<<"BIND TO PORT "<<port<<endl;

    if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
    {
        cerr<<"ServerSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(14);
    }
}

void ClientSocket::ConnectToServer( const char *ipAddress, int port )
{
    myAddress.sin_family = AF_INET;
    myAddress.sin_addr.s_addr = inet_addr( ipAddress );
    myAddress.sin_port = htons( port );

    //cout<<"CONNECTED"<<endl;

    if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
    {
        cerr<<"ClientSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(13);
    } 

}

这是stdafx。h类

#pragma once

#include "targetver.h"

#define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>


// TODO: reference additional headers your program requires here
#include "Socket.h"

这是我的错误消息:

1>------ Build started: Project: Client, Configuration: Debug Win32 ------
1>  stdafx.cpp
1>  Socket.cpp
1>  Client.cpp
1>  Generating Code...
1>Socket.obj : error LNK2019: unresolved external symbol __imp__accept@12 referenced in function "public: void __thiscall ServerSocket::Listen(void)" (?Listen@ServerSocket@@QAEXXZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__bind@12 referenced in function "public: void __thiscall ServerSocket::Bind(int)" (?Bind@ServerSocket@@QAEXH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function "public: void __thiscall Socket::CloseConnection(void)" (?CloseConnection@Socket@@QAEXXZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__connect@12 referenced in function "public: void __thiscall ClientSocket::ConnectToServer(char const *,int)" (?ConnectToServer@ClientSocket@@QAEXPBDH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__htons@4 referenced in function "public: void __thiscall ServerSocket::Bind(int)" (?Bind@ServerSocket@@QAEXH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__inet_addr@4 referenced in function "public: void __thiscall ServerSocket::Bind(int)" (?Bind@ServerSocket@@QAEXH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__listen@8 referenced in function "public: void __thiscall ServerSocket::Listen(void)" (?Listen@ServerSocket@@QAEXXZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__recv@16 referenced in function "public: bool __thiscall Socket::RecvData(char *,int)" (?RecvData@Socket@@QAE_NPADH@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__send@16 referenced in function "public: bool __thiscall Socket::SendData(char *)" (?SendData@Socket@@QAE_NPAD@Z)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket@@QAE@XZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket@@QAE@XZ)
1>Socket.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function "public: __thiscall Socket::Socket(void)" (??0Socket@@QAE@XZ)
1>C:\Users\ajayp_000\documents\visual studio 2012\Projects\Client\Debug\Client.exe : fatal error LNK1120: 12 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

共有3个答案

高峻
2023-03-14

runtimeobject.lib添加到链接器附加依赖项(在项目属性页面中,在Linker-

华良平
2023-03-14

另一种方法是在visual studio中的项目中单击鼠标右键,转到下面的路径并在其中添加“Ws2\u 32.lib”。连接器

程吕恭
2023-03-14

问题是您没有链接到Ws2_32.lib库。要解决此问题,您可以将其添加到项目的链接器/输入设置的附加依赖项选项卡中。或者(如SChepurin在评论中指出的)您可以添加

#pragma comment(lib, "Ws2_32.lib")

到您项目的源文件。

 类似资料:
  • 我有一个cpp文件,其中包括: 当尝试运行AcquireCredentialsHandleW时,我得到.. MSDN说它在sspi.h中。

  • 我在我的MFC vtk项目中使用Visual Studio 2008。我收到一个链接错误,如下所示: vtkPBFEAView。obj:警告LNK4217:本地定义的符号?DrawDC@vtkMFCWindow@@QAEXPAVCDC@@@Z(public:void _thiscall vtkMFCWindow::DrawDC(类CDC*))在函数“protected:virtual void _

  • 如果在中发现未解析的外部符号,我必须做什么。库文件。如果我在中发现未解析的外部符号。c或。cpp我能修好它。但当我在里面发现了未解决的外部符号。lib(library)文件如何解决这个问题? 在我的例子中,我发现了如下链接器错误: 关于这一点的任何建议都将是有益的。

  • 我正在用c和visual studio进行我的第一次测试,我不知道为什么,当程序中没有出现“未解决的外部符号”时,我会遇到一个问题。 错误97错误LNK2001:未解析的外部符号"public:静态类会话*__cdecl会话::实例(无效)"(?Instance@Session@@SAPAV1@XZ)(...)\MyTest\Messages.objMyTest 错误98错误LNK2001:未解析

  • 我得到了这个错误,但我不知道如何修复它。 我正在使用Visual Studio 2013。我将解决方案命名为MyProjectTest这是我的测试解决方案的结构: -功能。H -功能。cpp -main.cpp 我是初学者;这是一个简单的程序,运行时没有错误。我在互联网上阅读并对单元测试感兴趣,因此我创建了一个测试项目: 菜单文件→新建→项目...→已安装→模板→Visual C→测试→本地单元测

  • 我想写一个调用Java方法的C程序。 我试图从C调用Java函数。如本文所述 http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/invocation.html 但我在调试时遇到这个错误,无法处理它。我正在使用Visual studio 2012。这是我的代码C代码。 安装在my comp版本上的Java是C:\Users\