[clug] C programming question: What goes in 'include' files??

Ben Nizette bn at niasdigital.com
Tue Aug 23 16:45:16 MDT 2011


On 24/08/2011, at 12:46 AM, steve jenkin wrote:

> ==> Question:
> Is the practice of putting executable code (not hidden with #ifdefs) in
> include files, usual or normal???

As Martijn said, in header files it's common so long as they're static so you /want/ to get multiple copies around the code that are independent from each other.  I'd suggest you get started by sticking the 'static' keyword in there yourself, it'll work so long as the functions don't rely on shared internal state.  Otherwise you /might/ be able to declare them as __attribute__((weak)); I can't remember whether weak symbols can be overridden by themselves though..

> 
> Is it considered 'good practice' in some C-style?

It's excellent form to use static inlines instead of long macros as you then get better readability, type checking and so long as you use any optimisation, it's pretty much free.

As for 'includes' in general, rather than 'headers', it's rare but interesting when the #include directive is used to combine multiple, readable, C source files in to a single compilation unit, generally for performance reasons.  See, for example, kernel/sched.c - that's a 10000 line file by itself but also has, around line 2k,

#include "sched_idletask.c"
#include "sched_fair.c"
#include "sched_rt.c"
#include "sched_autogroup.c"
#include "sched_stoptask.c"

#ifdef CONFIG_SCHED_DEBUG
# include "sched_debug.c"
#endif

pulling in another few 10's of thousand lines, all that has to be compiled together.

	--Ben.

> 
> ==> If so, could you point me there?!
>    I'd like to understand how these guys think.
> 
> cheers
> steve jenkin
> 
> 
> Example:
> 
> $ grep fpuctl.h */*.c */*/*.c # that's a cheat, but enough for here
> 
> emu/MacOSX/os.c:#include <fpuctl.h>
> libmath/FPcontrol-MacOSX.c:#include "fpuctl.h"
> 
> ---------------------------
> 
> MacOSX/power/include/fpuctl.h:
> 
> ulong
> getfsr(void)
> {
>        ppc_fp_scr_t fpscr = get_fp_scr();
>        return ((ulong *)&fpscr)[1];
> }
> 
> void
> setfsr(ulong fsr)
> {
>        ppc_fp_scr_t fpscr;
>        // fpscr = get_fp_scr();
>        (((ulong *)&fpscr)[1]) = fsr;
>        set_fp_scr(fpscr);
> }
> 
> ... snip the rest ...
> 
> ---------------------------
> 
> 
> -- 
> Steve Jenkin, Info Tech, Systems and Design Specialist.
> 0412 786 915 (+61 412 786 915)
> PO Box 48, Kippax ACT 2615, AUSTRALIA
> 
> sjenkin at canb.auug.org.au http://members.tip.net.au/~sjenkin
> -- 
> linux mailing list
> linux at lists.samba.org
> https://lists.samba.org/mailman/listinfo/linux



More information about the linux mailing list