#### Archived

This topic is now archived and is closed to further replies.

# Simple problem

This topic is 5547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts


int x = 2;

main.cpp

#include <iostream.h>

int main()
{
cout << x;
return 0;
}

main2.cpp

#include <iostream.h>

int func()
{
x = 4;
cout << x;
return 0;
}

And of course I get this error: Linking... main2.obj : error LNK2005: "int x" (?x@@3HA) already defined in main.obj Debug/TestProg.exe : fatal error LNK1169: one or more multiply defined symbols found Error executing link.exe. I know it must be very easy to fix, but how? [edited by - dta1 on November 9, 2002 7:57:55 AM] [edited by - dta1 on November 9, 2002 8:39:37 AM]

##### Share on other sites
try int x; instead of int x=2;

##### Share on other sites
 extern int x;

main.cpp:
#include "header.h"int x = 2;...

main2.cpp:
#include "header.h"...

##### Share on other sites
None of that works (and I want x to be 2 in header.h).

But it works if I declare it as static (static int x = 2). But that doesn''t seem to be a good the way.

##### Share on other sites

extern int x = 2;

main.cpp:

int x;
// x should be 2?

##### Share on other sites
quote:
Original post by Pipo DeClown

extern int x = 2;

main.cpp:

int x;
// x should be 2?

Sorry, can''t get that to work.
And yes, x should be two in main.cpp.

int x=5;
#endif

listen to fruny

##### Share on other sites
quote:
None of that works (and I want x to be 2 in header.h)

which begs the questions: why do you want x to be 2 in header.h? what does it mean? why do you want a new variable x made in every file that header.h is included?

the explanation of oluseyi''s answer is this:

you want one copy of the variable x to be available to your program. by declaring x in a header you can include that header in places that need to know about x. the statement x = 2 is not a declaration though. it is a definition. whats that? there''s a difference?

the difference:
the declaration will tell your program that somewhere there exists a variable x. it will be able to find it when the program finally links.

the definition will actually declare and define the variable. that variable is the object. you can only have one of them. that''s why it''s a problem to put the definition in the header file.

the solution:
put a definition of the variable in one cpp file.
put a declaration of the variable as ''extern''. this means it will look for it elsewhere at link time (and hopefully find it defined in your cpp file)

##### Share on other sites
quote:
Original post by petewood
[quote]None of that works (and I want x to be 2 in header.h)

which begs the questions: why do you want x to be 2 in header.h? what does it mean? why do you want a new variable x made in every file that header.h is included?

the explanation of oluseyi''s answer is this:

you want one copy of the variable x to be available to your program. by declaring x in a header you can include that header in places that need to know about x. the statement x = 2 is not a declaration though. it is a definition. whats that? there''s a difference?

the difference:
the declaration will tell your program that somewhere there exists a variable x. it will be able to find it when the program finally links.

the definition will actually declare and define the variable. that variable is the object. you can only have one of them. that''s why it''s a problem to put the definition in the header file.

the solution:
put a definition of the variable in one cpp file.
put a declaration of the variable as ''extern''. this means it will look for it elsewhere at link time (and hopefully find it defined in your cpp file)

Hehe. You took in consistently =)

I wan''t only one x, every file that includes header.h must refer to that header''s x when using and changing it.
This works good if I declare x as a static but when I use extern it doesn''t work. I''ve tried most combinations of it but without luck.

Is anyone perfectly sure of how this should be done?
It''s not a hard question, really. =)

##### Share on other sites
cough

have you tried what we''ve said?

if you want x in a header then it can''t be the ''real'' x. the real x has to be in one and only one cpp file.

put int x = 2; in a .cpp
put extern int x; in a .h

make sure the .cpp is in your project.
include the .h wherever you need to know about x.
quote:
Is anyone perfectly sure of how this should be done?
It''s not a hard question, really. =)
perfectly sure. are you?

##### Share on other sites
static at file scope to indicate internal linking is deprecated. Use an unnamed namespace.

namespace{  int x = 2;}

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

[edited by - Fruny on November 12, 2002 5:46:46 AM]

##### Share on other sites
quote:
Original post by dta1
I wan''t only one x, every file that includes header.h must refer to that header''s x when using and changing it.

Right, you''ve been shown how to do that.
quote:

This works good if I declare x as a static

No it doesn''t. If you declare it as static (which is deprecated as Fruny points out), then you don''t have "only one x", you have an x for each translation unit it appears in. In fact, your requirements are self-contradictory: you want a single "x", but want that x to have different values at the same time.
quote:

but when I use extern it doesn''t work.

What do you mean "it doesn''t work"? If you want "only one x", then the only way to achieve that is to use the extern keyword, which tells the linker to fix-up each symbol export to the same offset in the stack. If you actually want to have a separate x for each translation unit (which I think is what you want), then you''ve also been shown how to do that.
quote:

Is anyone perfectly sure of how this should be done?

Not whilst you continue to be vague and ambiguous about what you want to achieve.
quote:

It''s not a hard question, really. =)

We''ll be the judges of that. Why don''t you describe the outer problem that makes you want to do this?

##### Share on other sites
To summarize:

  extern int x;

  int x = 2;

main.cpp

  #include <iostream.h>#include "header.h"int main(){   cout << x;   return 0;}

main2.cpp

  #include <iostream.h>#include "header.h"int func(){   x = 4;   cout << x;   return 0;}