windows - How to correctly link winsock2.h in C? -


this question has answer here:

i'm using qt creator build c plain project. project inlcudes socket creation , i'm getting many errors of reference.

my code simple:

#include <winsock2.h> #include <stdio.h>  // need link ws2_32.lib #pragma comment (lib, "ws2_32.lib")  int main(int argc , char *argv[]) {     wsadata wsa;     socket s;      printf("\ninitialising winsock...");     if (wsastartup(makeword(2,2),&wsa) != 0)     {         printf("failed. error code : %d",wsagetlasterror());         return 1;     }      printf("initialised.\n");      if((s = socket(af_inet , sock_stream , 0 )) == invalid_socket)     {         printf("could not create socket : %d" , wsagetlasterror());     }      printf("socket created.\n");      return 0; } 

compilation errors:

undefined reference `_imp__wsastartup@8' undefined reference `_imp__wsagetlasterror@0' undefined reference `_imp__socket@12' undefined reference `_imp__wsagetlasterror@0' 

then suppose means winsock2.h lib not included. how without #pragma comment()?

your #include <winsock2.h> usage fine. it's need update project settings , add ws2_32.lib link library.

for qt, add line .pro file. assumes using microsoft compiler:

libs += ws2_32.lib 

note: had delete "build" directory command line , "clean build" qt creator change take effect.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -