我正在使用QT Creator構建一個C普通項目。我的項目包含套接字創建,我收到很多參考錯誤。如何在C中正確鏈接winsock2.h?
我的代碼很簡單:
#include <winsock2.h>
#include <stdio.h>
// Need to link with 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;
}
編譯錯誤:
undefined reference to `[email protected]'
undefined reference to `[email protected]'
undefined reference to `[email protected]'
undefined reference to `[email protected]'
話,我想這意味着不包括winsock2.h
庫。如何在沒有#pragma comment()
的情況下做到這一點?
@ a3f感謝提醒!我剛剛檢查了答案,它現在正在工作 –