Domanda

I coded a program, that sends a HTTP request via sockets to google to read the response. This worked quite well, but since I overloaded it I get these compiler errors:

Error 1

error LNK2005: _initializeWinsock already defined in
Main.obj    workshop\SocketTest\WSAStart.obj    SocketTest

Error 2

error LNK1169: one or more multiply defined symbols
found   workshop\Debug\SocketTest.exe   1   1   SocketTest

Do you have a clue?

Main.c:

#include<stdio.h>
#include "WSAStart.c"


int main(int argc, char *argv[])
{
initializeWinsock();

    printf("Initialised.\n");
    return 0;
}

WSAStart.c:

#include <winsock2.h>
#include <stdio.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char *message, server_reply[2000];
int recv_size;

int initializeWinsock() {
    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
    {
        printf("Failed. Error Code : %d", WSAGetLastError());
        return 1;
    }

    printf("Initialised.\n");
    return 0;
}
È stato utile?

Soluzione

You should not #include source files in other source files. Especially if the included source file is also used to build the application through a separate object file.

Instead you make a header file with the declarations needed:

Like a WSAStart.h file:

#pragma once

int initializeWinsock();

Include the above header file Main.c and you can call the function as usual.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top