Frage

I know similar questions are asked so many time. But I am still in planning stage, so I hope I can get the right direction before start.

My case is like this: I need to write a general purpose library in C which need to make use of another C++ library. They are all written with C99 and posix standard.

This C library in turn will be called by any kind of program (C, C++) and run on multi Unix-like platform, like AIX, HP-UX, Linux, Solaris.

Someone told me it is not possible. Is it true?

War es hilfreich?

Lösung

Yes it is possible, observing the following:

1) Any exported functions in the C++ library will need to be marked extern "C".

2) Only ever pass plain old data types (PODs) between C and C++ libraries. You might get away with structures but make sure you pack the elements consistently.

3) Never allocate memory in a C library and free it in the C++ library and vice-versa. String allocation in particular can be a pain: the normal thing to do is pass a buffer with a size and the function returns the amount of data that could have been written to that buffer.

4) Never throw exceptions across the library boundaries. Use an error information type object in the C++ library bound to each thread (easy in C++11 as you have std::this_thread etc.)

Andere Tipps

You want to create a shared object or so (dll for linux to my understanding) that several clients must access to while they use different operating systems...

As soon as you clients (AIX, HP-UX, Linux, Solaris) interface to the C library through the C++ interface (shall we call it server??), I don't see the problem because it is in fact your C++ interface that accesses the .so; If they must use directly the shared object, I'm scared you will have to compile it several times, for each incompatible OS (or let them compile it maybe) and send them the correct version but I don't think that's what you want.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top