C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

__declspec(dllexport)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
news user
Guest





PostPosted: Fri Aug 20, 2004 1:42 pm    Post subject: __declspec(dllexport) Reply with quote



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 :

Quote:
foo.hpp

class Foo {
public :
Foo() ;
~Foo() ;
void bar() ;
private :
int bar_ ;
}

__declspec(dllexport) Foo* e_foo ;

Quote:
foo.cpp

Foo::Foo(){ ... }
Foo::~Foo(){ ... }
void Foo::bar(){ ... }

Foo* e_foo = new Foo ;

Second, I wrote the main program which is linked with foo.dll :

Quote:
main.cpp

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 :

Quote:
foo.hpp

class Foo {
public :
Foo() ;
~Foo() ;
void bar() ;
private :
int bar_ ;
}

__declspec(dllexport) Foo* e_foo = new Foo ;

Quote:
foo.cpp

Foo::Foo(){ ... }
Foo::~Foo(){ ... }
void Foo::bar(){ ... }

Quote:
main.cpp

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





PostPosted: Sat Aug 21, 2004 3:29 am    Post subject: Re: __declspec(dllexport) Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.