So I have nearly got my head around multiple source files but I'm still stuck with this problem despite reading a few tutorials.
I have a source file called 'initialise.cpp' which initialises winsock with a function called 'Init'. This should mean in 'main.cpp' I should be able to just call Init and it will do that, which it does. However, both 'main.cpp' and 'initialise.cpp' need to use the SOCKET Socket. So I thought I should put it in a header along with a few other declarations called 'declarations.h' and then #include declarations.h at the start of both the source files this has brought up the errors
Error 1 error LNK2005: "unsigned int Socket" (?Socket@@3IA) already defined in initialise.obj
Error 2 error LNK2005: "int x" (?x@@3HA) already defined in initialise.obj
Error 3 error LNK1169: one or more multiply defined symbols found
if it helps here is 'declarations.h'
1 2 3 4 5 6 7 8 9 10 11 12 13
#ifndef __DECLARATIONS_H_INCLUDED__
#define __DECLARATIONS_H_INCLUDED__
#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
SOCKET Socket;
int x = 1;
bool Init();
#endif
The declarations may be in every program module but the definitions of variables with external linkage should be only in one module that is One Definition rule shall be kept.