0

I've started another program to run Winsock in C language on Windows7.

I found this help on msdn: http://msdn.microsoft.com/en-us/library/windows/desktop/ms737591%28v=vs.85%29.aspx

This program is not working and showing these errors:

$ gcc ak1.c -o ak1.exe -lwsock32 -lws2_32 -lAdvapi32 -lmswsock

C:\Users\rahul\AppData\Local\Temp\ccWqZC7C.o:ak1.c:(.text+0x109): undefined re ference to `getaddrinfo'

C:\Users\rahul\AppData\Local\Temp\ccWqZC7C.o:ak1.c:(.text+0x1fa): undefined re ference to `freeaddrinfo'

c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: C:\Users\a mit\AppData\Local\Temp\ccWqZC7C.o: bad reloc address 0x20 in section `.eh_frame'

c:/mingw/bin/../lib/gcc/mingw32/4.7.2/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation

collect2.exe: error: ld returned 1 exit status

Is this error related to MICROSOFT SDK for Windows7, means these libraries are not added on my system. I checked "Advapi32" is not under my micrsoft SDK.

Help please !!!

2 Answers 2

0

You're using GCC (presumably MinGW) which doesn't use Microsoft's SDK. getaddrinfo() and freeaddrinfo() are declared in MinGW's ws2tcpip.h implementation, but only if WinXP or later is targeted.

Add -D_WIN32_WINNT=0x0501 to your compiler command line to target XP.

5
  • It is still not working, I would like to mention that I've done the following changes to run that program (as sys.h etc never run on windows, if the following changes are not done):
    – Beth Coder
    Apr 16, 2013 at 5:50
  • #ifdef WIN32 #include <Winsock2.h> #include <Ws2tcpip.h> #else #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> #include <sys/wait.h> #endif #include <stdio.h> #include <stdlib.h> #include <errno.h>
    – Beth Coder
    Apr 16, 2013 at 5:52
  • All I know is that using MinGW 4.7.2 and the command line you posted, when I compile the program on the MSDN website you pointed to I get the same undefined reference errors (however I don't get the bad reloc address error). When I add -D_WIN32_WINNT=0x0501 to the compile command, I get no linker errors. The appropriate _WIN32_WINNT configuration is shown to be necessary by looking at the MinGW ws2tcpip.h header. You may need to reinstall MinGW and/or make sure there are no environment variables that are causing the compiler to pickup the wrong headers or libraries. Apr 16, 2013 at 6:29
  • ok sir, what should I add as under 1st and 2nd command line argument...and how to use command line in mingw, I tried this ------> $ gcc ak1.c 127.0.0.1 -o ak1.exe -lwsock32 -lws2_32 -lAdvapi32 -lmswsock......here 127.0.0.1 is command line i.e argv[1] (I think so)...help me and correct if I'm wrong
    – Beth Coder
    Apr 16, 2013 at 7:16
  • gcc only compiles the program - it doesn't run it and has nothing to do with the arguments passed to it. Compile the program using gcc -D_WIN32_WINNT=0x0501 ak1.c -o ak1 -lwsock32 -lws2_32 -ladvapi32 -lmswsock then run it using ak1 127.0.0.1. Of course, you'll need to have the server program from msdn.microsoft.com/en-us/library/windows/desktop/ms737593.aspx compiled and running for the ak1 127.0.0.1 command to actually do anything noteworthy. Apr 16, 2013 at 7:42
-2

the solution is to add "-lws2_32" at the end of the command to compile with gcc ;-)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.