Sending C ++ dll using only C api

I am writing a piece of software whose API is only for C, because it is easy to associate C code with other software products / clients.

The actual program code, however, runs in C ++ using all the usual C ++ features such as exception, STL, etc.

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

What should I be afraid of if I intend to deliver this DLL to users who do not know about C ++ on their side? Usually they don’t have to worry about the actual code being in C ++, and only need to know how to link it to the C code through the header file. I was told that I should make sure that libstd is connected statically, which may not be possible on all platforms (there will be Linux and Windows). What about exceptions? etc.

+4
source share
1 answer

1) wrap your headline 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 do not need to specify class definitions in your public api. class definitions will appear in some private headers.

+1
source

Source: https://habr.com/ru/post/1584788/


All Articles