Error using VSCODE and MinGW and Coderunner extension on VSCODE
Getting error : collect2.exe: error: ld returned 1 exit status
The code is
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <time.h>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
int main() {
WSADATA wsa;
SOCKET sockfd;
struct sockaddr_in server_addr, client_addr;
int portno;
int client_len = sizeof(client_addr); // Change socklen_t to int
char buffer[1024];
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0) {
printf("Failed to initialize Winsock\n");
return 1;
}
// Server configuration
portno = 12346;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(portno);
// Create socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) {
printf("Error opening socket\n");
return 1;
}
// Bind socket
if (bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("Error binding socket\n");
closesocket(sockfd);
WSACleanup();
return 1;
}
while (1) {
ssize_t bytes_received = recvfrom(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&client_addr, &client_len);
if (bytes_received < 0) {
printf("Error receiving data\n");
break;
}
// Display received data
buffer[bytes_received] = '\0'; // Null-terminate the received data
time_t current_time = time(NULL);
printf("%ld %s %s\n", current_time, ctime(¤t_time), buffer);
}
closesocket(sockfd);
WSACleanup();
return 0;
}
Its purpose :
Overall Functionality
This code creates a simple UDP server in C that listens for incoming messages on port 12346. When a message arrives, it timestamps the message and prints it to the console.
Header Files and Setup
#include <stdio.h> : Includes standard input/output functions like printf and scanf.
#include <stdlib.h> : Provides general utilities like memory allocation.
#include <string.h>: Includes string manipulation functions (e.g., memset).
#include <winsock2.h> and #include <winsock.h>: Brings in libraries for network programming on Windows.
#pragma comment(lib, "ws2_32.lib"): Instructs the linker to automatically link the Winsock library.
Main Function (main)
Winsock Initialization:
WSADATA wsa: A structure to hold Winsock initialization data. WSAStartup(): Initializes the Winsock library. Socket Creation:
SOCKET sockfd: A file descriptor representing the socket. socket(): Creates a UDP socket (SOCK_DGRAM) Server Address Setup:
struct sockaddr_in server_addr, client_addr: Structures for holding internet addresses. portno = 12346: The port number where the server will listen. memset(): Clears the memory of the server address structure. server_addr.sin_family = AF_INET: Specifies IPv4. server_addr.sin_addr.s_addr = INADDR_ANY: Listen on all available interfaces. server_addr.sin_port = htons(portno): Port number in network byte order. Binding the Socket:
bind(): Assigns the prepared address to the socket. Receiving Data Loop:
while (1): Runs indefinitely. recvfrom(): Receives a datagram (message) from the socket and stores sender address. buffer[bytes_received] = '\0': Ensures the received data is null-terminated for use as a string. time() and ctime(): Get the current time and format it. printf(): Prints the timestamp, formatted time, and received message. Cleanup:
closesocket(): Closes the socket. WSACleanup(): Releases Winsock resources.
[Running] cd "c:\Users\gseid\Documents\.vscode\" && gcc openbcisocket.c -o openbcisocket && "c:\Users\gseid\Documents\.vscode\"openbcisocket
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0x35): undefined reference to `WSAStartup@8'
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0x98): undefined reference to `htons@4'
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0xbe): undefined reference to `socket@12'
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0xfd): undefined reference to `bind@12'
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0x11c): undefined reference to `closesocket@4'
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0x124): undefined reference to `WSACleanup@0'
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0x167): undefined reference to `recvfrom@24'
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0x18a): undefined reference to `closesocket@4'
C:\Users\gseid\AppData\Local\Temp\cc533lEm.o:openbcisocket.c:(.text+0x192): undefined reference to `WSACleanup@0'
collect2.exe: error: ld returned 1 exit status