Reputation: 51
Should every function be wrapped by extern "C"
, or can I just write extern "C" {
at the beginning of my list-of-functions-definitions and end the block at the very end with }
?
Upvotes: 0
Views: 193
Reputation: 129504
In case you ever want to include the header in a C compiler (as opposed to C++ compiler), you may want to use:
#ifdef __cplusplus
extern "C" {
#endif
... list of functions ...
#ifdef __cplusplus
}
#endif
Otherwise, both will do the same thing - any function declared within an extern "C" { ... }
is the same as each function having extern "C"
as part of its declaration.
Upvotes: 4
Reputation: 967
You can do either. Doing a single extern "C" block is my recommendation. It makes it clear that you mean to be providing a C-style API and avoids the risk of missing it on one function. If you one day decide to provide a C++ linkage version (or a conditionally compiled version), you'll only have to make the change in one place.
Upvotes: 1