Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

c - Undefined reference to getaddrinfo

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
", 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
", iResult);
        WSACleanup();
        return 1;
    }
    return 0;
}  

I am compiling with the following command:

gcc msdn_np.c -o msdn_np.exe -lWS2_32
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...