Wednesday, 2 September 2009

getting around the "undefined reference to getaddrinfo" issue

>> main.cpp:(.text+0x18b): undefined reference to `getaddrinfo@16'
>> collect2: ld returned 1 exit status

After spending some time looking into winsock2 and *trying* to put together some code I've come across the message above when trying to compile the app.
After some trial and error I finally found out the reason why this message wouldn't go away. Apparently there are two things that have to be taken into account here.

1. Redefine your Windows version
According to msdn(1), some functions are only available to specific windows versions and therefore one must explicitly tell the compiler that the program is being compiled for that particular version. As it turns out, this is the case of the getaddrinfo() function.
By taking a quick look at the tcp/ip specific extensions for Winsock2 header file (ws2tcpip.h) it's easy to see this OS version dependency in the function declaration:

(...)
#if (_WIN32_WINNT >= 0x0501)
void WSAAPI freeaddrinfo (struct addrinfo*);
int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,struct addrinfo**);
int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,char*,DWORD,int);
#else
(...)

In order to tell the compiler your windows version you can set the constant called _WIN32_WINNT to the hex code of that version.
Since the getaddrinfo() is available to Windows XP onwards, it is sufficient to use that windows version code as the value of the constant mentioned above. Add the following line before the inclusion of the headers.

#define _WIN32_WINNT 0x0501


Bear in mind that since you're telling the compiler that you're using Windows XP here, you will not be able to make use of functions that are only available to versions prior to the one you have defined (Windows XP).


2. Link, Link, Link
Easy to forget, but you must remember to link the library that implements the getaddrinfo() to your application otherwise the compiler cannot resolve the function.
The library in question is the "Ws2_32.Lib" which, in my case, came along with the Windows SDK. Once you have the full path of the URL, you can link it by using the following command:

c:\apps\g++ main.cpp "c:\xxxxxxxxxx\xxx\" -o main.exe

NB: I am disregarding here any other library that you may have to link to your app.

By following the two steps above you should be able to run your app fine.
Just remember that these steps may vary depending on the version of your compiler and the libraries you've got installed.

11 comments:

  1. Really informative post, helped me alot, thanks.
    Web Development

    ReplyDelete
    Replies
    1. IEEE Project Domain management in software engineering is distinct from traditional project deveopment in that software projects have a unique lifecycle process that requires multiple rounds of testing, updating, and faculty feedback. A IEEE Domain project Final Year Projects for CSE system development life cycle is essentially a phased project model that defines the organizational constraints of a large-scale systems project. The methods used in a IEEE DOmain Project systems development life cycle strategy Project Centers in Chennai For CSE provide clearly defined phases of work to plan, design, test, deploy, and maintain information systems.


      This is enough for me. I want to write software that anyone can use, and virtually everyone who has an internet connected device with a screen can use apps written in JavaScript. JavaScript Training in Chennai JavaScript was used for little more than mouse hover animations and little calculations to make static websites feel more interactive. Let’s assume 90% of all websites using JavaScript use it in a trivial way. That still leaves 150 million substantial JavaScript Training in Chennai JavaScript applications.

      Delete
  2. Thanks man, I couldn't figure out why getaddrinfo wasn't being linked :)

    ReplyDelete
  3. Gracias, no podía resolver getaddrinfo por culpa del macro _WIN32_WINNT, encima que trabajo sobre windows 7 que es versión 0x0601 :/
    Saludos

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This didn't work for me. What did work though, is to #include &ltws2tcpip.h&gt

    ReplyDelete
  6. I really really thank you man... i was into this for 2 days... and your article has been the solution... God (Allah) Bless you...

    ReplyDelete
  7. Outsourcing to Eastern Europe has a range of advantages due to its attractive cost-to-quality ratio. I would recommend checking out this article to learn more about the software development in Eastern Europe.

    ReplyDelete
  8. Hello, how to get the hexadecimal code of windows 10.

    ReplyDelete