Error LNK2005 ... already defined in

closed account (2NywAqkS)
I would have thought these error could have only come from not using header guards but here they are.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Warning	14	warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library	E:\Programming\Projects\C++\RS Client\RS Client\LINK
Warning	13	warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:ICF' specification	E:\Programming\Projects\C++\RS Client\RS Client\SOIL.lib(SOIL.obj)
Error	5	error LNK2005: "unsigned int Socket" (?Socket@@3IA) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	3	error LNK2005: "unsigned int playerTex" (?playerTex@@3IA) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	7	error LNK2005: "unsigned int groundTex" (?groundTex@@3IA) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	12	error LNK2005: "struct HWND__ * hWnd" (?hWnd@@3PAUHWND__@@A) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	11	error LNK2005: "struct HINSTANCE__ * hInstance" (?hInstance@@3PAUHINSTANCE__@@A) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	6	error LNK2005: "struct HGLRC__ * hrc" (?hrc@@3PAUHGLRC__@@A) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	8	error LNK2005: "struct HDC__ * hdc" (?hdc@@3PAUHDC__@@A) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	1	error LNK2005: "int nPort" (?nPort@@3HA) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	10	error LNK2005: "int lfTime" (?lfTime@@3HA) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	9	error LNK2005: "int currentTime" (?currentTime@@3HA) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	2	error LNK2005: "char * szServer" (?szServer@@3PADA) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	4	error LNK2005: "bool * keys" (?keys@@3PA_NA) already defined in main.obj	E:\Programming\Projects\C++\RS Client\RS Client\socket.obj
Error	15	error LNK1169: one or more multiply defined symbols found	E:\Programming\Projects\C++\RS Client\Release\RS Client.exe


I'm really not sure why I'm getting these errors. here are the header files

main.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef MAIN_H
#define MAIN_H

#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glaux.lib")
#pragma comment(lib, "SOIL.lib")
#pragma comment(linker, "/subsystem:windows")

#include "socket.h"
#include <Windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include "SOIL.h"

/*************************** Constants and Macros ***************************/
#define WND_CLASS_NAME  "OpenGL Window Class"

/******************************** Globals ********************************/
HDC			hdc;
HGLRC		hrc;
HWND		hWnd;
HINSTANCE	hInstance;

int lfTime, currentTime;
bool keys[256];

/******************************** Prototypes ********************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

BOOL		SetupWindow(const char *title, int width, int height, int bits);
void		ResizeScene(GLsizei width, GLsizei height);
BOOL		InitializeScene();
void		LoadTextures();
void		Calculate();
void		DisplayScene();
void		KillWindow();

/******************************** Load Textures ********************************/
GLuint playerTex, groundTex;

#endif MAIN_H 


socket.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef SOCKET_H
#define SOCKET_H

#pragma comment(lib,"ws2_32.lib")
#include <winsock2.h>
#include "main.h"

#define WM_SOCKET	104

char *szServer="localhost";
int nPort=5555;
SOCKET Socket=NULL;

void SetUpWinsock();

#endif SOCKET_H 


I'm quite new to using multiple header and cpp files so it's probably a simple mistake.
The error messages are very clear. You defined objects in header file socket.h and this header is included in several modules.

closed account (2NywAqkS)
so I should rename it?
You should not define objects in headers. Define them in modules.

in a header

extern char *szServer;
extern int nPort;
extern SOCKET Socket;

in a module

char *szServer="localhost";
int nPort=5555;
SOCKET Socket=NULL;

closed account (2NywAqkS)
ah, thanks vlad, didn't think of that.
Topic archived. No new replies allowed.