 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
news user Guest
|
Posted: Fri Aug 20, 2004 1:42 pm Post subject: __declspec(dllexport) |
|
|
Hi everybody...
I am triing to create shared libraries with MSC++.net from C++ sources
currently compiled with gcc on Linux.
So, I must use '__declspec(dllexport)' in my code in order to export
some classes/functions/variables
Then, I have some problems to export variables. I read on the web that,
to export a variable, it must be declared as extern (no problem !).
First, I wrote the following code in order to create the shared library
foo.dll :
class Foo {
public :
Foo() ;
~Foo() ;
void bar() ;
private :
int bar_ ;
}
__declspec(dllexport) Foo* e_foo ;
Foo::Foo(){ ... }
Foo::~Foo(){ ... }
void Foo::bar(){ ... }
Foo* e_foo = new Foo ;
Second, I wrote the main program which is linked with foo.dll :
int main( int argc, char* argv[] ) {
e_foo->bar() ;
return 0 ;
}
When I run MSCV.net, I have a link error during the main compilation :
'unresolved symbol e_foo'
To compile without link error, I tried the following solution which is
in the standard :
class Foo {
public :
Foo() ;
~Foo() ;
void bar() ;
private :
int bar_ ;
}
__declspec(dllexport) Foo* e_foo = new Foo ;
Foo::Foo(){ ... }
Foo::~Foo(){ ... }
void Foo::bar(){ ... }
int main( int argc, char* argv[] ) {
e_foo->bar() ;
return 0 ;
}
But, it is a Windows solution : even if my pointer is same in all
libraries, I can link this solution on Linux ('multiple definition of')
and the compilation give me a warning.
So, my question is : how can I export my extern variable (both on
Windows and Linux) with the declaration in the header file, and its
instanciation in a source file in order to have the same pointer in all
my programm ?
Thanks,
Mathieu.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sat Aug 21, 2004 3:29 am Post subject: Re: __declspec(dllexport) |
|
|
news user wrote:
| Quote: | [...] So, my question is : how can I export my extern variable (both on
Windows and Linux) with the declaration in the header file, and its
instanciation in a source file in order to have the same pointer in all
my programm ?
|
AFAIK, since "exporting a variable" is an OS-specific thing, you will
need to have conditional compilation sections in your header, something
like
#ifdef WIN32
#ifdef EXPORT_THIS // this is to keep the same header for DLL and EXE
// only define EXPORT_THIS when building the DLL
#define DLL_THING __declspec(dllexport)
#else
#define DLL_THING __declspec(dllimport)
#endif
#else
#define DLL_THING // you may not need to do anything here -- check
// with a Linux programming newsgroup
#endif
DLL_THING Foo* e_foo = new Foo;
I've seen people referring to a command-line option for g++ to export all
global symbols. Check with g++ newsgroup for recommendations how to do
that for individual symbols.
To ALL: sorry for the partially OT solution.
Victor
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|