Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure #includes in C

Tags:

c

include

header

Say I have a C program which is broken to a set of *.c and *.h files. If code from one file uses functions from another file, where should I include the header file? Inside the *.c file that used the function, or inside the header of that file?

E.g. file foo.c includes foo.h, which contains all declarations for foo.c; same for bar.c and bar.h. Function foo1() inside foo.c calls bar1(), which is declared in bar.h and defined in bar.c. Now the question is, should I include bar.h inside foo.h, or inside foo.c?

What would be a good set of rules-of-thumb for such issues?

like image 215
Paggas Avatar asked Nov 09 '08 19:11

Paggas


People also ask

How do you write a structure?

Structure in academic writingStart with more general and then move to the more specific ideas and points. Put more relevant/important information first. Everything is relevant to the main argument or point of the paragraph. Use cohesion to join ideas and points clearly - don't make the reader do the work.

How do you structure a good essay?

What is the structure of an essay? The structure of an essay is divided into an introduction that presents your topic and thesis statement, a body containing your in-depth analysis and arguments, and a conclusion wrapping up your ideas.

How do you structure a short essay?

The standard five-paragraph short essays have specific structure: introduction (1 paragraph), thesis, main body (3 paragraphs), and conclusion (1 paragraph). This helps your work be elaborately structured and easier to comprehend. First impression matters, even if you're writing a short essay.


2 Answers

You should include foo.h inside foo.c. This way other c files that include foo.h won't carry bar.h unnecessarily. This is my advice for including header files:

  • Add include definitions in the c files - this way the file dependencies are more obvious when reading the code.
  • Split the foo.h in two separate files, say foo_int.h and foo.h. The first one carries the type and forward declarations needed only by foo.c. The foo.h includes the functions and types needed by external modules. This is something like the private and public section of foo.
  • Avoid cross references, i.e. foo references bar and bar references foo. This may cause linking problems and is also a sign of bad design
like image 151
kgiannakakis Avatar answered Oct 12 '22 04:10

kgiannakakis


As others have noted, a header foo.h should declare the information necessary to be able to use the facilities provided by a source file foo.c. This would include the types, enumerations and functions provided by foo.c. (You don't use global variables, do you? If you do, then those are declared in foo.h too.)

The header foo.h should be self-contained and idempotent. Self-contained means that any user can include foo.h and not need to worry about which other headers may be needed (because foo.h includes those headers). Idempotent means that if the header is included more than once, there is no damage done. That is achieved by the classic technique:

 #ifndef FOO_H_INCLUDED
 #define FOO_H_INCLUDED
 ...rest of the contents of foo.h...
 #endif /* FOO_H_INCLUDED */

The question asked:

File foo.c includes foo.h, which contains all declarations for foo.c; same for bar.c and bar.h. Function foo1() inside foo.c calls bar1(), which is declared in bar.h and defined in bar.c. Now the question is, should I include bar.h inside foo.h, or inside foo.c?

It will depend on whether the services provided by foo.h depend on bar.h or not. If other files using foo.h will need one of the types or enumerations defined by bar.h in order to use the functionality of foo.h, then foo.h should ensure that bar.h is included (by including it). However, if the services of bar.h are only used in foo.c and are not needed by those who use foo.h, then foo.h should not include bar.h

like image 45
Jonathan Leffler Avatar answered Oct 12 '22 06:10

Jonathan Leffler