#include in header files in embedded [duplicate] - c

This question already has answers here:
Should I use #include in headers?
(9 answers)
Closed 6 years ago.
I am reading through an embedded C standard book and I noticed the following:
No header file shall contain an #include statement
What should I do with function declarations that have non-standard types?
example: void function(some_funky_type x);

Throw that book away; it is absolute garbage. In fact, you should burn it, to make sure no other poor soul ever picks it up.
Header files should absolutely include all of the header files necessary for them to be self-sufficient. There is nothing worse than trying to carefully massage the order of your #include statements to be sure that the types needed by one are already defined before it is included.

This is a stupid and counterproductive rule for precisely the reason you identified. The alternative is for every .c file to include all of the .h files that a subsequently included header will require. You can imagine that if you introduce a new dependency in a commonly included header that you will now have to update every C file that includes that header.

Related

Why header files are needed in c [duplicate]

This question already has answers here:
What is the point of header files in C? [duplicate]
(4 answers)
Closed 3 years ago.
When I program in c there are .h files .
Header files contain the signatures of functions that are implement in c files.
Why are header files are needed as I can include the c file to use the function there instead of using headers.
Is this to make the compiler's job easier?
TLDR answer is organization and code reuse.
Header files contain common standard code usable across multiple code bases - think of them as libraries to bring in.
This code is kept separate for maintenance issues. You only want one copy, so when a fix needs making it happens in one place. All of the various code bases you have that reference that one included header file suddenly have the updates.
You can also do project specific headers. For example, my SQL statements are all static strings, since they are long and complex I keep them in a different file and just include it. This way, I don't have a large long blob of complex SQL as a string in the middle of my code.

How to find definition of library functions in C [duplicate]

This question already has answers here:
Where are the functions in the C standard library defined?
(5 answers)
Closed 7 years ago.
Functions like printf() , scanf() , memset() , puts() etc have their declaration in header files but is there any mechanism to see the definition of these function..?
This might not be a new question but i could not find the appropriate solution for this.
Find your compilers include path (e.g. GCC solution)
Search for the header you are interested in (e.g. printf should be in stdio.h or more likely another header included by stdio.h)
Correctly configured, some IDEs will help you with that, e.g. Eclipse
The method has its limits though, because at some point the include files will get less and less Standard-C, but more and more compiler dependent. The C-standard does not prescribe the contents of standard headers. It merely states that if you write #include <stdio.h>, you can use printf(). That does not necessarily mean that stdio.h has some form you might expect.

Why including a header file and not the implementation? [duplicate]

This question already has answers here:
Header per source file
(7 answers)
Closed 8 years ago.
On most tutorial I could find on the web I have notice that everyone is creating header files for everything and never include a .c file.
I couldn't find any good explanation on the web about why you need the header file.
I have read that including the header file allows you to not repeat yourself which doesn't make sense to me. The header file IS a repetition of all the declaration of your implementation, if you include your implementation directly, then you avoid this overhead right!?!
Don't get me wrong, I can understand the use of header file when you are doing libraries : Several project can include only the header file and then linking to the same library (e.g. the standard library) thus ending up with a smaller executable. I just don't see the benefit of header file when you include something which is completly specific to your project...
Can you explain me the real benefit of header files?
Suppose you have a program built from 10 source files. If each one included all the code that was needed (including, presumably, the implementations of the standard C library functions it uses), you'd have lots and lots of multiple definition errors when you link all the bits together.
So, a header (normally) includes just the declarations. The object code for the corresponding source code is linked with the program, either as explicit object files or as libraries. This stops you getting multiple definition errors.

importance of header files [duplicate]

This question already has answers here:
What is the point of header files in C? [duplicate]
(4 answers)
Closed 9 years ago.
Is there a need to create a header file after making a source file? What are the things included in a header file? is it necessary or is it good for only source files containing a function definition?
I'm really confused since what you put in header files, such as function portotypes, can also be placed in a source file. Are header files only good for declaring global variables? Is creating a header file with the same name as your source file necessary.
I already got the answer of "What are header files for". Apparently a lot of questions similar to this have been posted that's why adding some follow up questions.
I guess you could search on Google and find whatever your need to know but to make it short:
In C (and C++), every source (.c or .cpp) file is compiled on its own translation unit. This means that every file is compiled on its own to produce an object file. Once all object files are compiled, they are linked together to create your final binary file.
This means that a source file has no clue of what's defined in other source files, the header file is needed to fill this gap: it provides the declarations of variables, functions and whatever that are implemented in one or many source files, so that, when you are including it inside another source file, the compiler can verify that all these external things are correctly used. This is not its only purpose, it can aid in other taks (like reuse of code or partitioning) but this is why it exists.
The header file is literally included when you use the #include directive.

What is a good reference documenting patterns of use of ".h" files in C? [duplicate]

This question already has answers here:
Should I use #include in headers?
(9 answers)
Closed 7 years ago.
"C Interfaces and Implementations" shows some interesting usage patterns for data structures, but I am sure there are others out there.
http://www.amazon.com/Interfaces-Implementations-Techniques-Addison-Wesley-Professional/dp/0201498413
Look at the Goddard Space Flight Center (NASA) C coding standard (at this URL). It has some good and interesting guidelines.
One specific guideline, which I've adopted for my own code, is that headers should be self-contained. That is, you should be able to write:
#include "header.h"
and the code should compile correctly, with any other necessary headers included, regardless of what has gone before. The simple way to ensure this is to include the header in the implementation source -- as the first header. If that compiles, the header is self-contained. If it doesn't compile, fix things so that it does. Of course, this also requires you to ensure that headers are idempotent - work the same regardless of how often they are included. There's a standard idiom for that, too:
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
...operational body of header.h...
#endif /* HEADER_H_INCLUDED */
It is imperative, of course, to have the #define at the top of the file, not at the bottom. Otherwise, if a header included by this also includes header.h, then you end up with an infinite loop - not healthy. Even if you decide to go with a strategy of:
#ifndef HEADER_H_INCLUDED
#include "header.h"
#endif /* HEADER_H_INCLUDED */
in the code that include the header - a practice which is not recommended - it is important to include the guards in the header itself too.
Update 2011-05-01
The GSFC URL above no longer works. You can find more information in the answers for the question Should I use #include in headers, which also contains a cross-reference to this question.
Update 2012-03-24
The referenced NASA C coding standard can be accessed and downloaded via the Internet archive:
http://web.archive.org/web/20090412090730/http://software.gsfc.nasa.gov/assetsbytype.cfm?TypeAsset=Standard
Makeheaders is an interesting approach: use a tool to generate the headers. Makeheaders is used in D. R. Hipp's cvstrac and fossil.
You might want to take a look at Large-Scale C++ Software Design by John Lakos.

Resources