Connecting Tech Pros Worldwide Help | Site Map

Writing portable libraries

aaronfude@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Torsten Mueller
Guest
 
Posts: n/a
#2: Jul 23 '05

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.
benben
Guest
 
Posts: n/a
#3: Jul 23 '05

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]


Torsten Mueller
Guest
 
Posts: n/a
#4: Jul 23 '05

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.
aaronfude@gmail.com
Guest
 
Posts: n/a
#5: Jul 23 '05

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