lezebulon
lezebulon

Reputation: 7994

Shipping a C++ dll with only a C api exposed

I'm writing a piece of software whose API is intended to be C only , because it is easy to link C code against other softwares/clients.

The actual program code however is done in C++, using all the usual C++ features like exception, STL, etc.

The exported API / headers themselves will be written in pure C, with the export "C" keywords.

What should I be wary of if I intend to deliver this dll to users who have no knowledge of C++ on their side? Normally they should not be concerned about the fact that the actual code is in C++, and only be required to know how to link against C code via a header file. I've been told that I should ensure that libstd is linked statically, which may be not be possible on all platforms (there will be a linux and a windows build). How about exceptions ? etc

Upvotes: 5

Views: 745

Answers (1)

fferri
fferri

Reputation: 18950

1) wrap your header in:

#ifdef __cplusplus
extern "C" {
#endif

2) make opaque pointers:

struct myInternalStructFOO; // incomplete type
typedef struct myInternalStructFOO *cFOO; // public type

class myInternalClassBAR; // incomplete type
typedef class myInternalClassBAR *cBAR; // public type

you need not to give class definitions in your public api. class definitions will go in some private headers.

Upvotes: 1

Related Questions