CodeBlocks: Undefined reference to getaddrinfo()
By nick | Code12 Dec 2011
If you’re encountering the error “Undefined reference to getaddrinfo()” he’s how to solve the problem. It seems the winsock examples on the MSDN site are very much intended for use with Visual C and therefore you may have to do a few additional things to get everything working correctly.
Firstly you’ll need to add a few Link Settings. In codeblocks you can find the settings page shown below under Project/Build Options/Link Settings.
As you can see above, you need to add “libws2_32.a” to the Link Libraries and “-lwsock32″ to the linker options.
Additionally:
You may need to add the following to your code:
#define _WIN32_WINNT 0×501

Not quite working yet. I’m following the MSDN article pretty closely but the code still gives the same error after your fix.
I works now. The #define _WIN32… line needs go before the #includes.
Aha, thanks for that correction! I’ll amend the post.
Sorry to hear that, please let me know if you find the solution so we can help others!