Connecting Tech Pros Worldwide Help | Site Map

Writing portable libraries

  #1  
Old July 23rd, 2005, 06:49 AM
aaronfude@gmail.com
Guest
 
Posts: n/a
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, 06:49 AM
Torsten Mueller
Guest
 
Posts: n/a

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, 06:50 AM
benben
Guest
 
Posts: n/a

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, 06:50 AM
Torsten Mueller
Guest
 
Posts: n/a

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, 06:50 AM
aaronfude@gmail.com
Guest
 
Posts: n/a

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).

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Advice on writing portable C++ application Ramon F Herrera answers 13 November 28th, 2007 07:35 PM
Jargons of Info Tech industry Xah Lee answers 336 November 15th, 2005 04:33 AM
Writing portable software Jason Curl answers 10 November 14th, 2005 08:16 PM
Jargons of Info Tech industry Xah Lee answers 385 October 22nd, 2005 09:15 PM