473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

Jul 23 '05 #1
4 2445
aa*******@gmail.com schrieb:
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.
Jul 23 '05 #2
Another way of writing portable code is not to export classes. Try to use
pure abstract classes and global functions.

ben

"Torsten Mueller" <de******@shared-files.de> wrote in message
news:u4***********@fastmail.fm...
aa*******@gmail.com schrieb:
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.

Jul 23 '05 #3
"benben" <be******@hotmail.com> schrieb:
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.
Jul 23 '05 #4
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).

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christian Decker | last post by:
I was just wondering if there is a site which has a good list of libraries that can be used to develop portable code on Mac, Linux and Winblows. Any ideas? Sorry if I'm Offtopic, but I didn't...
385
by: Xah Lee | last post by:
Jargons of Info Tech industry (A Love of Jargons) Xah Lee, 2002 Feb People in the computing field like to spur the use of spurious jargons. The less educated they are, the more they like...
13
by: James Harris | last post by:
Hi, Can someone recommend a book that will teach me how to approach C programming so that code is modularised, will compile for different environments (such as variations of Unix and Windows),...
10
by: Jason Curl | last post by:
Dear C group, I'm very interested in writing portable C, but I only have GNU, Sparc and Cygwin to compile on. What I find is the biggest problem to writing portable C is what headers to...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
6
by: Abubakar | last post by:
Hi, we are finding out ways in which we could develop libraries that could be written in c++ in a way that we can use them in windows, linux, and mac os. We want to write portable code, so that it...
1
by: Alexandre Guimond | last post by:
Hi. I want to create a portable setup.py file for windows / linux for an extension package that i need to link with external libraries (gsl and boost). on windows i do something like this: ...
13
by: Ramon F Herrera | last post by:
My goal is to write a studio-type application which will initially run on Windows, but I would like it to run on other platforms with minimum efforts. I have made the decision to go with C++. ...
14
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I need a Big Number library. I've been considering switching my project to C++ but at the moment I'm exploring the avenue of keeping it in C. What's the best Big Number library for C? I need to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.