windows - How to correctly link winsock2.h in C? -
this question has answer here:
- qt (creator) winsocks (ws2_32) 4 answers
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
Post a Comment