You can technically call anything from C if the function name is C-visible (prototypes, etc. are ignored at the ABI level). Of course, you cannot expect the correct results if C cannot generate the parameters as expected. Typically, the obvious solution is to simplify the interface to C. char ** is a great choice for the largest common denominator with vector<string> . Not only that, if you know what you intend to do with it, perhaps faster (and cleaner IMHO).
Regarding C visibility: the function name cannot be used in conjunction with any other visible C functions. If you want your C ++ function to be called from C, this might be a good prototype example:
extern "C" char **lots_of_strings();
If the parameter signature is different, C ++ allows you to overload functions that are visible only with C ++, and allows them to coexist with the C version:
vector<string> lots_of_strings(int); extern "C" char **lots_of_strings();
If you want to provide several ways to call it suitable for the calling language, you can try this (ignoring the late initialization flaws and the fact that bool exists in C):
bool lots_of_strings(vector<string> &); extern "C" int lots_of_strings(char ***); Whatever lots_of_strings(SomeArrayType &);
Remembering that in each case, C ++ will choose the definition with the best matching signature to the call site, and C will take whatever it can (which is always one function with the corresponding name).
It will be useful for you to hide C ++ - isms from C by combining #ifdef with the __cplusplus macro.
Matt joiner
source share