>> 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.
Really informative post, helped me alot, thanks.
ReplyDeleteWeb Development
Thanks man, I couldn't figure out why getaddrinfo wasn't being linked :)
ReplyDeleteGracias, no podía resolver getaddrinfo por culpa del macro _WIN32_WINNT, encima que trabajo sobre windows 7 que es versión 0x0601 :/
ReplyDeleteSaludos
This comment has been removed by the author.
ReplyDeleteThis didn't work for me. What did work though, is to #include <ws2tcpip.h>
ReplyDeleteI really really thank you man... i was into this for 2 days... and your article has been the solution... God (Allah) Bless you...
ReplyDeleteOutsourcing 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.
ReplyDeletethank you!!!
ReplyDeleteHello, how to get the hexadecimal code of windows 10.
ReplyDeleteaşk kitapları
ReplyDeleteyoutube abone satın al
cami avizesi
cami avizeleri
avize cami
no deposit bonus forex 2021
takipçi satın al
takipçi satın al
takipçi satın al
takipcialdim.com/tiktok-takipci-satin-al/
instagram beğeni satın al
instagram beğeni satın al
btcturk
tiktok izlenme satın al
sms onay
youtube izlenme satın al
no deposit bonus forex 2021
tiktok jeton hilesi
tiktok beğeni satın al
binance
takipçi satın al
uc satın al
sms onay
sms onay
tiktok takipçi satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
instagram beğeni satın al
tiktok beğeni satın al
twitter takipçi satın al
trend topic satın al
youtube abone satın al
takipcialdim.com/instagram-begeni-satin-al/
perde modelleri
instagram takipçi satın al
instagram takipçi satın al
takipçi satın al
instagram takipçi satın al
betboo
marsbahis
sultanbet
Buy anything, anywhere, pay with crypto: www.payb.io
ReplyDeletebetmatik
ReplyDeletekralbet
betpark
tipobet
slot siteleri
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
0QES
Bbw Sex Dolls are made from high-quality TPE andラブドール feature realistic curves and features that will leave you breathless
ReplyDeleteçekmeköy
ReplyDeletekepez
manavgat
milas
balıkesir
3Q66
yurtdışı kargo
ReplyDeleteresimli magnet
instagram takipçi satın al
yurtdışı kargo
sms onay
dijital kartvizit
dijital kartvizit
https://nobetci-eczane.org/
MSZ2X8
salt likit
ReplyDeletesalt likit
KAZ
Instagram Takipçi Satın Al
ReplyDeleteToptan Telefon Kılıfı
Resimli Magnet
Silivri Çatı Ustası
Çerkezköy Çatı Ustası
6NFPN4
çeşme transfer
ReplyDeletekralbet
bor yağı filtre kağıdı
yağ süzme filtre kağıdı
J2Z6
sdfdfgvdfgfhfgh
ReplyDeleteشركة تنظيف مكيفات
dfvgvdfxbgfhfgh
ReplyDeleteشركة تنظيف مكيفات
شركة تسليك مجاري بالدمام kuYoXk1t5a
ReplyDeleteشركة مكافحة حشرات بالاحساء Imc55MlQA4
ReplyDeleteشركة تسليك مجاري بالاحساء pzpLRLYOQf
ReplyDeleteشركة تسليك مجاري بالقطيف XoljugcWEN
ReplyDeleteعزل اسطح بالجبيل cpI0CZme4a
ReplyDeleteعزل اسطح بالجبيل Aq2TlCVaUr
ReplyDeleteشركة عزل اسطح بالدمام r8dYtxHjXa
ReplyDelete