-2

(i have edited my original question to make it more understandable)

here is the prototype for the problem ....

//Txn.h ---this has a static variable, usable by the pgms including it.

class Txn
{
public:
static int i;
static void incr_int();
};
Txn::i=0;

//Txn.cpp

void Txn::incr_int() {i++;}

->produce LibTxn.so
//class1.cpp -> one of the pgm using the static var from Txn.h

#include Txn.h
Txn::incr_int()

-> produce class1.o, with LibTxn.so.
// class2.cpp ->another pgm using the static var from Txn.h

#include Txn.h
cout<<"Txn::i;

-> produce class2.o, by including LibTxn.so
-> .produce class3 (an exe) by using class1.o,class2.o. Since, both class1 and 2 has the statement "Txn::i=0" from "Txn.h", multiple declaration issue happens.
-> .If I remove the statement "Txn::i=0" from Txn.h, then "undefined reference" error appears.
-> .At high lvl, this problem is a kind of having a session variable, which should be assessible from any func in a exe. Those func can be in any obj files used to form the exe. I am fine for any sol, even without static. But I can't change the creation of different .o files (which are using this session var) and combining the .o to produce the exe.

Jagan
  • 15
  • 3
  • 4
    and we simply just cant assume your code and give you correct answer – exexzian Feb 11 '13 at 14:33
  • Where is the static variable *defined*? It should not be in the header, otherwise all translation units including that header will generate a definition for it – Andy Prowl Feb 11 '13 at 14:33
  • Please include some code reproducing the error. **Not the full code**, just the declarations and definitions of this variable from example in your various files; and how you include them. – Matthieu M. Feb 11 '13 at 14:33
  • 5
    `(because of the security policies, I am not able to provide the actual code)` We don't even want to see the actual code. We want your [abstract testcase](http://sscce.org) that you just spent three days working on and debugging before finally resorting (as a final step) to asking here. – Lightness Races in Orbit Feb 11 '13 at 14:42
  • 1
    Voting to close since without code that reproduces the problem it's not possible to give a real answer, only random guesses. And it's definitely possible to create code that has nothing to do with anything that has the same problem you're experiencing. And since it has nothing to do with anything, there then shouldn't be any security issue involved in posting it. – Omnifarious Feb 11 '13 at 15:04

3 Answers3

1

It is hard to figure out exactly what the problem is if you cannot provide the real code, or at least an example which has the same problem as the real code.

However, most likely the root cause of the problem is that you are not only declaring, but also defining your class's static variable in the header file that contains the class definition.

This means that all the translation units (i.e. .cpp files) which include that header will contain a definition for the static variable, and when merging all the corresponding object files, the linker will eventually complain about that symbol being defined multiple times.

If this is the case, what you should do is to take the initialization of the static variable out of the header file which contains your class's definition and put it in one (and only one) .cpp file.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
0

I tried to recreate the problem as you described, but it compiled just fine on my computer, and it is difficult to go further without seeing your code.

In the code below, the header tells (declares) every .cpp file that includes it about Foo::x, but Foo::x lives in (is defined in) Foo.cpp (and Foo.o)

foo.h:

class Foo {
public:
    static int x;
};

Foo.cpp:

#include "foo.h"

int Foo::x;

main.cpp:

#include <iostream>
#include "foo.h"

int main(int argc, char *argv[]) {
    Foo::x = 42;
    std::cout << "Foo::x is " << Foo::x; 
}
Wernsey
  • 5,411
  • 22
  • 38
0

Yes. it worked by defining the static variable in .cpp. Special thanks to Andy Prowl and iWerner.

Jagan
  • 15
  • 3