Static members of a class in a dllâ€

J

Josemi

Hello!

I have a curious case with a class that has a static member defined in
a DLL. Let me explain:

dll.h
class __declspec (dllexport) A
{
public:static int valor;
PrintOut ()
{
TRACE1(valor);
}
}

dll.cpp
A::valor=23;

main.h
class B:A
{
}

main.cpp
A::valor;
{
A objeto;

objeto.valor = 34;
objeto.PrintOut();
}

Well, it happens that the value given in "main.cpp" objeto.valor =
34, PrintOut print 23. Can you know what is wrong? although I have
some idea, I think the problem is that I have to declare A::valor in
the two routines and well, the compiler creates two instances of
A::valor, Is it posible declare only one in dll.cpp file? or any idea
how to solve this problem. I can think of set routines that fill this
gap, but I wanted something less complicated.



Thanks in advance

your sincerely
Josemi
 
I

Ian Collins

Hello!

I have a curious case with a class that has a static member defined in
a DLL. Let me explain:

dll.h
class __declspec (dllexport) A

You'll probably get a better answer on a windows programming group.
 
G

Goran

Hello!

I have a curious case with a class that has a static member defined in
a DLL. Let me explain:

dll.h
class __declspec (dllexport) A
{
   public:static int valor;
   PrintOut ()
   {
      TRACE1(valor);
   }

}

dll.cpp
A::valor=23;

main.h
class B:A
{

}

main.cpp
A::valor;
{
   A objeto;

   objeto.valor = 34;
   objeto.PrintOut();

}

Well, it happens that the value given in "main.cpp" objeto.valor =
34,  PrintOut print  23. Can you know what is wrong? although I have
some idea, I think the problem is that I have to declare A::valor in
the two routines and well, the compiler creates two instances of
A::valor, Is it posible declare only one in dll.cpp file? or any idea
how to solve this problem.  I can think of set routines that fill this
gap, but I wanted something less complicated.

(You're way off-topic here).

I don't think that code you posted can compile anywhere. (Next time,
please have the courtesy to post compilable code. You would like
people to help you, but you don't even bother giving them a nice basis
to do it, which is just rude). I can see that it doesn't compile on my
MS C compiler from VS 2008.

When building DLLs for windows with MS compiler, you typically do:

#ifdef BUILDING_MYDLL
#define MYDLL_CLASS __declspec(dllexport)
#else
#define MYDLL_CLASS __declspec(dllimport)
#endif

class MYDLL_CLASS A
{
// without MYDLL_CLASS, your exe with main doesn't link.
....
};

That works correctly. (TRACE1(valor) also doesn't compile.)

If not, is it possible that you are loading two versions of this DLL
in your process? I suggest that you look into the module list while
debugging (Visual studio 2008, that's Debug->Windows->Modules). Sort
that list by name, find your DLL to verify that.

Goran.
 
J

Josemi

(You're way off-topic here).

I don't think that code you posted can compile anywhere. (Next time,
please have the courtesy to post compilable code. You would like
people to help you, but you don't even bother giving them a nice basis
to do it, which is just rude). I can see that it doesn't compile on my
MS C compiler from VS 2008.

When building DLLs for windows with MS compiler, you typically do:

#ifdef BUILDING_MYDLL
#define MYDLL_CLASS __declspec(dllexport)
#else
#define MYDLL_CLASS __declspec(dllimport)
#endif

class MYDLL_CLASS A
{
// without MYDLL_CLASS, your exe with main doesn't link.
...

};

That works correctly. (TRACE1(valor) also doesn't compile.)

If not, is it possible that you are loading two versions of this DLL
in your process? I suggest that you look into the module list while
debugging (Visual studio 2008, that's Debug->Windows->Modules). Sort
that list by name, find your DLL to verify that.

Goran.- Ocultar texto de la cita -

- Mostrar texto de la cita -

Many thanks for your answer.

First. I don't think that my question is off-topic. This group talks
about C++ language or not?. My question is about the use the directive
static in class member, and this is part C++'lenguage.
Second. yes and I sorry for my code, but my intention was to present
my problem more clearly

Finally, indeed, thank you very much for your help

yours sincerely
Josemi
 
J

Josemi

You'll probably get a better answer on a windows programming group.

Thank you for your help.

Why in windows programming group. I ask about C++ language, basic C++
language, not about MFC or another herbs :)

Josemi
 
G

Goran

First. I don't think that my question is off-topic. This group talks
about C++ language or not?. My question is about the use the directive
static in class member, and this is part C++'lenguage.

Well... "static" is on topic, you are right about that.

However, you are still off-topic because you have things in your post
that have no relation to C++ language:

* DLLs (neither C nor C++ language standard know anything about DLLs;
indeed, both languages are supposed to run on computers that have no
DLLs, or any sort of module system, or even any sort of operating
system).

* __declspec(dllexport) is not a part of C nor C++. Note: when you see
things that start with an underscore, it's normally something that is
implementation-specific (so e.g. in Microsoft's implementation of STL,
there's _Xran, a function that you might have seen mentioned here
recently; poster that asked didn't realize that _Xran is part of
Microsoft's implementation of STL, not part of STL as prescribed by
the standard; see
http://groups.google.com/group/comp...899f3ba430/3deb49dea243a8f7?#3deb49dea243a8f7).
Also, I think that two underscores (as in "__declspec") are reserved
for vendor specific extensions of the language. But the truth is that
MS, gcc, and Borland's compiler all use __declspec(dllexport/
dllimport), so that is de facto standard for C++ DLLs on Windows.

* TRACE1 is not defined by the standard.

Due to these three, you should have gone to e.g.
http://social.msdn.microsoft.com/Forums/en-US/category/visualc.

Hope this helps,
Goran.
 
J

Josemi

Well... "static" is on topic, you are right about that.

However, you are still off-topic because you have things in your post
that have no relation to C++ language:

* DLLs (neither C nor C++ language standard know anything about DLLs;
indeed, both languages are supposed to run on computers that have no
DLLs, or any sort of module system, or even any sort of operating
system).

* __declspec(dllexport) is not a part of C nor C++. Note: when you see
things that start with an underscore, it's normally something that is
implementation-specific (so e.g. in Microsoft's implementation of STL,
there's _Xran, a function that you might have seen mentioned here
recently; poster that asked didn't realize that _Xran is part of
Microsoft's implementation of STL, not part of STL as prescribed by
the standard; seehttp://groups.google.com/group/comp.lang.c++/browse_thread/thread/601...).
Also, I think that two underscores (as in "__declspec") are reserved
for vendor specific extensions of the language. But the truth is that
MS, gcc, and Borland's compiler all use __declspec(dllexport/
dllimport), so that is de facto standard for C++ DLLs on Windows.

* TRACE1 is not defined by the standard.

Due to these three, you should have gone to e.g.http://social.msdn.microsoft.com/Forums/en-US/category/visualc.

Hope this helps,
Goran.

Thank you.

Josemi
 
J

James Kanze

I have a curious case with a class that has a static member
defined in a DLL. Let me explain:
dll.h
class __declspec (dllexport) A
{
public:static int valor;
PrintOut ()
{
TRACE1(valor);
}
}

main.h
class B:A
{

main.cpp
A::valor;
{
A objeto;

objeto.valor = 34;
objeto.PrintOut();
}
Well, it happens that the value given in "main.cpp"
objeto.valor = 34, PrintOut print 23. Can you know what is
wrong? although I have some idea, I think the problem is that
I have to declare A::valor in the two routines and well, the
compiler creates two instances of A::valor, Is it posible
declare only one in dll.cpp file? or any idea how to solve
this problem. I can think of set routines that fill this gap,
but I wanted something less complicated.

Well, this is very off-topic. (C++ doesn't have a __declspec
command---that's pure VC++.) But since I've had the same
problem...

The declaration __declspec(dllexport) should only be used in the
dll in which the class will be defined. In the other dll's, it
should be __declspec(dllimport). For some strange reason,
__declspec(dllexport) works for importing as well, *except* for
static variables (and maybe some other rare cases). The usual
solution here is to prefix the dll.h header with something like:
#ifndef DLLNAME_EXPORT
#define DLLNAME_EXPORT __declspec(dllimport)
#endif
and then compile the sources for the dll itself with
/DDLLNAME_EXPORT=__declspec(dllexport)

(And yes, it's an ugly hack, but that's what Microsoft requires.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top