I normally wouldn’t do this, but the project I’m currently working on requires a couple functions that are in a c source file.
extern "C" {
int words(char sentence[]);
int match(char str[], char sentence[], int n);
}
I simply want to know, where is the best place to add these prototypes to the linking functions in c?
Should it be added to the c++ source file (in my case, command.cpp), or the c/c++ header? (command.h)
The least error-prone way is to put the declarations in a shared C & C++ header file, and
#ifdeftheextern "C" {to only be used by the C++ compiler (it’s a syntax error in C):I personally also like this variant of pre-#defining a macro for the unscoped
externsyntax:This keeps the messy #ifdef stuff localised. You can place it in one of your project-wide header files and use the
CFUNspecifier anywhere. You may want to prefix the name of theCFUNmacro if it’s likely to collide with an existing definition.