I have two projects written in C: a client side and a server side, both of which contain lots of common global variables. I want to write a class (clientbot and serverbot using a interface Transaction) which will call the main function of each project.
I have moved all the common global variables in a header file and included it in both project. But on building it is throwing me linking error.
Error 3 error LNK2005: “int g_nBytestoSend” (?g_nBytestoSend@@3HA) already defined in transimpl.obj agentBot.obj
Can anyone suggest what I should do?
You shouldn’t put the actual variables in the header, only
externdeclarations of those. Put the actual variables in a separate.cor.cppfile and link with that.In the header (lets call it
globals.h), you put this declaration:Then you create a new code file to hold the actual variables (say
global.c):Let’s imagine you had three code files
server.c,client.candbot.cand that you’re using gcc. You would build this like:(Try to keep your number of globals small.)