Writing portable libraries

A

aaronfude

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
 
T

Torsten Mueller

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?

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

benben

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

ben
 
T

Torsten Mueller

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

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

aaronfude

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top