未解析符号的C ++链接器错误

时间:2018-06-22 23:08:14

标签: c++ linker linker-errors

我对C ++还是很陌生,并且正在努力理解为什么在构建解决方案时会出现错误,而在我编译解决方案中的文件时却找不到错误。我的代码是这样的:

#include <winsock2.h>
#include <WS2tcpip.h>

using namespace std;


int ResolveHostName(const char* pszHostName, sockaddr_in* pAddr)
{

    int ret;
    HRESULT hr = S_OK;
    addrinfo* pResultList = NULL;
    addrinfo hints = {};
    int result = -1;

    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;

    ret = getaddrinfo(pszHostName, NULL, &hints, &pResultList);

    result = (ret == 0) ? 1 : -1;
    if (result != -1)
    {
        // just pick the first one found
        *pAddr = *(sockaddr_in*)(pResultList->ai_addr);
        result = 0;
    }

    if (pResultList != NULL)
    {
        ::freeaddrinfo(pResultList);
    }

    return result;
}


int main()
{

    SOCKET sock = -1;
    WSADATA data = {};
    sockaddr_in addrRemote = {};
    int result;

    WSAStartup(MAKEWORD(2, 2), &data);


    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock != -1)
    {
        if (ResolveHostName("192.168.1.35", &addrRemote) != -1)
        {
            addrRemote.sin_port = htons(80);
            result = connect(sock, (sockaddr*)&addrRemote, sizeof(addrRemote));

            if (result != -1)
            {
                const char* msg = "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n";
                int msg_len = strlen(msg);
                result = send(sock, msg, msg_len, 0);
                if (result != -1)
                {
                    char szBuffer[10000 + 1];
                    result = recv(sock, szBuffer, 10000, 0);
                    if (result > 0)
                    {
                        // safely null terminate
                        szBuffer[result] = '\0';
                        printf("%s\n", szBuffer);
                    }
                }
            }
        }
    }

    if (sock != -1)
    {
        closesocket(sock);
        sock = -1;
    }

    return 0;
}

...这是我从SO复制的基本套接字连接示例,并修复了一些错误。链接器虽然给了我未解决的外部符号错误(例如,closesocket,{{1 }},connecthtons等)。错误代码为recv,最后的致命错误为LNK2019

我知道它们的链接器无法解析它们,但是我不确定为什么函数和main都在同一个Main.cpp文件中。

有什么想法吗?

谢谢

0 个答案:

没有答案