From: bang b. <she...@gm...> - 2012-05-18 01:48:45
|
Hi, everyone: I am very sorry to trouble you, but I need your help for my question about linking ws2_32 library. I use many function about network in my programm, but when linking them, some functions are undefined reference, and some are ok. The following is an example: /* example.c */ #include <windows.h> #include <winsock2.h> #include <winsock.h> #include <ws2tcpip.h> #include <stdio.h> #define DEFAULT_PORT "27105" int main(int argc, char* argv[]) { int iresult; struct addrinfo* result = NULL, *ptr = NULL, hints; char szPath[128] = ""; ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; hints.ai_flags = AI_PASSIVE; iresult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); gethostname(szPath, sizeof(szPath)); printf("hello wolrd\n"); return 0; } gcc example.c -o example.exe -L/mingw/bin -lws2_32 (I copied the ws2_32.dll to mingw/bin) ERROR: C:/DOCUME~1/tsinghua/LOCALS~1/Temp/cc6OKmlS.o:a.c:(.text+0xb1): undefined reference to `gethostname@8' collect2: ld returned 1 exit status NOTE: I have used tool to look the function symbols in ws2_32 and it has "gethostname", and if the ws2_32 library is not found, "getaddrinfo" function will be not found, but it found the function, why? Thank you very much!!! -- ADDRESS:清华大学FIT楼4-308 TEL: 156-1166-2737 -- ADDRESS:清华大学FIT楼4-308 TEL: 156-1166-2737 |