Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

dll

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

like image 607
lezebulon Avatar asked Nov 09 '22 15:11

lezebulon


1 Answers

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.

like image 105
fferri Avatar answered Nov 14 '22 23:11

fferri