Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is the best place to put the #ifdef __cplusplus extern "C" { #endif

Tags:

c++

c

include

I would like to know where is better to put the

#ifdef __cplusplus
extern "C" {
#endif

in a C header file.

At the beginning or after all the other includes. why ?

like image 879
Vincent Avatar asked Apr 18 '13 15:04

Vincent


People also ask

Where is the best place to put a router?

The best place to set up your router is in a central, unobstructed location to ensure your home Wi-Fi network has a strong signal anywhere in your house. Moving your router even just a few feet might save you from endless connection problems and allow you to get the most out of your internet connection.


1 Answers

There are no strict rules on this, but note the following.

  1. The general principle is that each header file takes care of itself (and is self sufficient). So, by this principle, there would be no need to wrap the header files in a extern "C", because the header files would have an extern "C" in them (if they need one). So, in the current file, you would place it after the other includes.
  2. But if you do a have a whole bunch of headers, that you don't want to add an extern "C" to, and want to make available through a single include, by all means, go ahead and wrap them up in a file wide extern "C".

Just know that the idea behind extern "C" is that it makes the compiler generate C friendly linkage. Otherwise, code compiled with a C++ compiler looks for mangled names to link against in archives compiled with a C compiler, and can't find them.

like image 106
Ziffusion Avatar answered Oct 19 '22 23:10

Ziffusion