c - implicit declaration of function 'getaddrinfo' on Windows with Mingw -
i'm trying create basic client application, based on code on msdn error in title. here complete code:
#include <tchar.h> #include <stdio.h> #include <string.h> #include <stdarg.h> #include <windows.h> #include <winsock2.h> #include <ws2tcpip.h> #pragma comment (lib, "ws2_32.lib") #define debug 1 #define default_buflen 512 #define default_port "9001" void createsocket(); int main(int argc, char* argv[]) { createsocket(); return 0; } void createsocket() { wsadata wsadata; socket connectsocket = invalid_socket; struct addrinfo *result = null, *ptr = null, hints; char *sendbuf = "this test"; int iresult; // initialize winsock iresult = wsastartup(makeword(2,2), &wsadata); zeromemory( &hints, sizeof(hints) ); hints.ai_family = af_unspec; hints.ai_socktype = sock_stream; hints.ai_protocol = ipproto_tcp; iresult = getaddrinfo("127.0.0.1", default_port, &hints, &result); connectsocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol); iresult = connect( connectsocket, ptr->ai_addr, (int)ptr->ai_addrlen); freeaddrinfo(result); iresult = send( connectsocket, sendbuf, (int)strlen(sendbuf), 0 ); closesocket(connectsocket); wsacleanup(); }
i'm on windows 10, mingw. complete compile , error:
gcc -wall -o "test" "test.c" -lws2_32 (nel direttorio: c:\users\federicoponzi\google drive\programmazione\c\programmazionedisistema\federicoponzi-programmazionedisistema-40c332bcd001\federicoponzi-programmazionedisistema-40c332bcd001\prove) test.c:10:0: warning: ignoring #pragma comment [-wunknown-pragmas] #pragma comment (lib, "ws2_32.lib") ^ test.c: in function 'createsocket': test.c:46:2: warning: implicit declaration of function 'getaddrinfo' [-wimplicit-function-declaration] iresult = getaddrinfo("127.0.0.1", default_port, &hints, &result); ^ test.c:52:2: warning: implicit declaration of function 'freeaddrinfo' [-wimplicit-function-declaration] freeaddrinfo(result); ^ test.c:35:6: warning: variable 'iresult' set not used [-wunused-but-set-variable] int iresult; ^ c:\users\federi~1\appdata\local\temp\ccaw2c9i.o:test.c:(.text+0xbb): undefined reference `getaddrinfo' c:\users\federi~1\appdata\local\temp\ccaw2c9i.o:test.c:(.text+0x11b): undefined reference `freeaddrinfo' c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: c:\users\federi~1\appdata\local\temp\ccaw2c9i.o: bad reloc address 0x20 in section `.eh_frame' c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: invalid operation collect2.exe: error: ld returned 1 exit status compilazione fallita.
i'm using geany ide.
also: why "ignoring pragma" error? thanks.
about
warning: ignoring #pragma comment [-wunknown-pragmas] #pragma comment (lib, "ws2_32.lib")
#pragma comment(lib,"xxx.lib")
microsoft c/c++ compiler specific. gcc not support it.
about
implicit declaration of function
simply #define _win32_winnt 0x0501
before include, due
#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 /* fixme: need ws protocol-independent api helpers. */ #endif
in ws2tcpip.h
file.
Comments
Post a Comment