Connecting Tech Pros Worldwide Help | Site Map

Writing portable libraries

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 05:49 AM
aaronfude@gmail.com
Guest
 
Posts: n/a
Default Writing portable libraries

Hi,

Perhaps this is slightly offtopic.

Coming into the Windows world from Unix.

Is there a reference on writing C++ libraries in a portable way.

For example, right now I'm sticking "__declspec(dllexport)" in front of
each of my classes. I guess I can still make it work on Unix bydefing
__declspec(x) to be an empty macro, but that's embarrassing.

Is there a better solution?

Many thanks in advance!

Aaron


  #2  
Old July 23rd, 2005, 05:49 AM
Torsten Mueller
Guest
 
Posts: n/a
Default Re: Writing portable libraries

aaronfude@gmail.com schrieb:
[color=blue]
> For example, right now I'm sticking "__declspec(dllexport)" in front
> of each of my classes. I guess I can still make it work on Unix
> bydefing __declspec(x) to be an empty macro, but that's
> embarrassing.
>
> Is there a better solution?[/color]

Normally this __declspec() stuff is already in a global #define
because in most cases you use the same header for compiling the
library (this needs __declspec(dllexport)) and for using the library
(this needs __declspec(dllimport)). So the class definitions normally
contain something like this:

#ifdef BUILD_LIB
#define LIB_CLASS __declspec(dllexport)
#else
#define LIB_CLASS __declspec(dllimport)
#endif

class LIB_CLASS MyClass {
// ...
};

If you now want to have portable libraries you must just extend the
LIB_CLASS macro. For most Unix compilers you would leave it blank.
Note: you need to define this macro just on one single place in the
entire project, so it's not really much effort.

T.M.
  #3  
Old July 23rd, 2005, 05:50 AM
benben
Guest
 
Posts: n/a
Default Re: Writing portable libraries

Another way of writing portable code is not to export classes. Try to use
pure abstract classes and global functions.

ben

"Torsten Mueller" <dev-null@shared-files.de> wrote in message
news:u4qbbul5c.fsf@fastmail.fm...[color=blue]
> aaronfude@gmail.com schrieb:
>[color=green]
> > For example, right now I'm sticking "__declspec(dllexport)" in front
> > of each of my classes. I guess I can still make it work on Unix
> > bydefing __declspec(x) to be an empty macro, but that's
> > embarrassing.
> >
> > Is there a better solution?[/color]
>
> Normally this __declspec() stuff is already in a global #define
> because in most cases you use the same header for compiling the
> library (this needs __declspec(dllexport)) and for using the library
> (this needs __declspec(dllimport)). So the class definitions normally
> contain something like this:
>
> #ifdef BUILD_LIB
> #define LIB_CLASS __declspec(dllexport)
> #else
> #define LIB_CLASS __declspec(dllimport)
> #endif
>
> class LIB_CLASS MyClass {
> // ...
> };
>
> If you now want to have portable libraries you must just extend the
> LIB_CLASS macro. For most Unix compilers you would leave it blank.
> Note: you need to define this macro just on one single place in the
> entire project, so it's not really much effort.
>
> T.M.[/color]


  #4  
Old July 23rd, 2005, 05:50 AM
Torsten Mueller
Guest
 
Posts: n/a
Default Re: Writing portable libraries

"benben" <benhongh@hotmail.com> schrieb:
[color=blue]
> Another way of writing portable code is not to export classes. Try
> to use pure abstract classes and global functions.[/color]

Ah, you mean native classes in the module using the library, calling
exported library functions. This is exactly what import libraries do.

Nevertheless also exported functions require __declspec(dllexport) in
MSVC, otherwise nothing is exported. AFAIR the __declspec(dllimport)
is redundant on functions.

T.M.
  #5  
Old July 23rd, 2005, 05:50 AM
aaronfude@gmail.com
Guest
 
Posts: n/a
Default Re: Writing portable libraries

Really, there isn't a way to export stuff with a .def file?

I'm really trying to avoid "__declspec(dllexport)" (b/c it is so ugly).

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.