对 getaddrinfo 的未定义引用

发布于 2024-10-20 10:17:48 字数 1276 浏览 5 评论 0原文

我已经收到这个错误有一段时间了,谷歌也没有提供太多帮助。

我是 Winsock 编程的新手,正在尝试从在线资源中学习。我正在尝试使用 MSDN 网站上的详细信息构建一个简单的服务器。每当我编译代码(MinGW)时,我都会收到标题中提到的错误(对 getaddrinfo 的未定义引用)。下面是代码:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define WINVER WindowsXP

#include <windows.h>
#include <winsock2.h>
#include <winsock.h>
#include <ws2tcpip.h>
#include <stdio.h>

int main() {
    WSADATA wsaData;
    int iResult;
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    #define DEFAULT_PORT "27015"

    struct addrinfo *result = NULL, *ptr = NULL, hints;

    ZeroMemory(&hints, sizeof (hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    // Resolve the local address and port to be used by the server
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }
    return 0;
}  

我正在使用以下命令进行编译:

gcc msdn_np.c -o msdn_np.exe -lWS2_32

I have been getting this error for quite some time now and Google has not been of much help either.

I am a newbie to Winsock programming and trying to learn from online resources. I am trying to build a simple server using details on MSDN website. Whenever I compile the code (MinGW), I get the error mentioned in the title (Undefined reference to getaddrinfo). Below is the code:

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#define WINVER WindowsXP

#include <windows.h>
#include <winsock2.h>
#include <winsock.h>
#include <ws2tcpip.h>
#include <stdio.h>

int main() {
    WSADATA wsaData;
    int iResult;
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    #define DEFAULT_PORT "27015"

    struct addrinfo *result = NULL, *ptr = NULL, hints;

    ZeroMemory(&hints, sizeof (hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    // Resolve the local address and port to be used by the server
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if (iResult != 0) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }
    return 0;
}  

I am compiling with the following command:

gcc msdn_np.c -o msdn_np.exe -lWS2_32

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

中性美 2024-10-27 10:17:48

这不是和这里同样的问题吗?

http://programmingrants.blogspot.com/2009/09/tips-on- undefined-reference-to.html

基本上不要忘记链接 Ws2_32.lib (消息来自链接器,所以这应该是原因),但您似乎已经这样做了。

...如果您正在使用旧版本的 Windows 编程工具,请在包含标头之前放置 #define _WIN32_WINNT 0x0501 告诉它您拥有高于 XP 的版本(现在不太可能仍然需要,但也许)。

可能是其他简单的问题。库的正常 (Unix) 约定是在它们前面加上 lib。此后,-lWS32_32 的含义将是搜索名称为 libWS32_32.a 的文件。它很可能找不到它,因为它缺少包含该库的目录的路径。您可以添加 -L 后跟正确目录的路径。或者,您甚至不需要 -l 来链接库,只需将库的完整路径(在这种情况下是文件系统上显示的文件的真实名称)也应该可以工作。

该问题也可能与路径相关。例如,如果库的路径包含空格,则可能会出现问题。如果是这样,您可以尝试将库文件放在名称更简单的目录中。

请向用户提供一些有关您的实际配置(库文件在哪个目录中)以及您实际尝试过的反馈。您还可以尝试设置 LIBS 和 LIBPATH 环境变量(最简单的方法可能是从 makefile 执行此操作)。

Isn't it the same problem as here ?

http://programmingrants.blogspot.com/2009/09/tips-on-undefined-reference-to.html

Basically do not forget to link with Ws2_32.lib (the message is from the linker, so that's should be the reason) but you seem to be doing that already.

... if you're working with an old version of windows programming tools say to it you have a version higher than XP by putting #define _WIN32_WINNT 0x0501 before including headers (unlikely to still be necessary nowaday, but maybe).

The could be other simple problems. Normal (Unix) convention for libraries is to prepend them with lib. Henceforth, meaning for -lWS32_32 would be to search for a file whose name is libWS32_32.a. It is likely it does not find it because it's missing the path to the directory containing the library. You could add a -L followed by the path to the correct directory. Alternatively you do not even need -l to link with a library, just putting the full path to the library (in this case the real name of the file as it appears on filesystem) should also work.

The problem can also be path related. For instance problems can occurs if path to library contains spaces. If this is so you can try putting you library files in a directory with a simpler name.

Please give use some feedback of your actual configuration (in which directory is the library file) and what you actually tried. You could also tries setting LIBS and LIBPATH environment variables (simplest way is probably to do that from a makefile).

吾家有女初长成 2024-10-27 10:17:48

http://msdn.microsoft.com/en- us/library/ms738520(v=vs.85).aspx

看一下“备注”下的内容。当您未按 ANSI-C 进行编译时,您可能会尝试使用该函数的 ANSI-C 版本。

http://msdn.microsoft.com/en-us/library/ms738520(v=vs.85).aspx

Take a look at the stuff under "remarks". It is possible that you are trying to use an ANSI-C version of the function when you aren't compiling as ANSI-C.

在巴黎塔顶看东京樱花 2024-10-27 10:17:48

WindowsXP 是在 w32api.h 中定义的,您必须在 #define WINVER 之前添加 #include代码>.

WindowsXP was defined in w32api.h, you have to #include <w32api.h> before your #define WINVER.

青衫负雪 2024-10-27 10:17:48

另一个可能的原因是_WIN32_WINNT。检查 ws2tcpip.h 头文件以获取使用 getaddrinfo() 的最低版本。

Another possible reason is _WIN32_WINNT. Check ws2tcpip.h header file for the minimum version to use getaddrinfo().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文