3 ms·
I wish someone would do a gentle introduction of header files. I sort of understand pointers, at least on a basic level. Headers files are apparently much easie
by mrweasel 6y ago
I wish someone would do a gentle introduction of header files. I sort of understand pointers, at least on a basic level. Headers files are apparently much easier, so they get the briefest of introduction in most "Learn C" tutorials and books, but I don't understand how they work or how to use them.
- gnulinux 6y agoThere is no magic in .h files, you can think them as plaintext. The actual magic happens in .c files. Since C requires objects to be declared before using them, you need to declare objects used in multiple units together. Let's say you have a function `int f(int)`. You want to use this function both in apple.c and pear.c. How would you do it. Well you need to write a f.h such as int f(int); then in apple.c #include "f.h" int apple() { int x = f(10); // rest of the code } You can think this as a copy paste of f.h such that, apple.c is equivalent to: int f(int); int apple() { int x = f(10); // rest of the code } but what "is" f. To "define" f you need another .cpp file that implements f, let's call it f.c: #include "f.h" int f(int x) { return x*x; } now you compile f.c, apple.c, pear.c. These will create f.o, apple.o and pear.o object files. Then you link these together to create your application. e.g. if f.h is in include/ dir: gcc -c apple.c -I include/ -o apple.o gcc main.c -I include/ apple.o pear.o f.o -o main Here, assume that main.c defines int main().
- umanwizard 6y agoImagine you have a function f in f.c: void f() { puts("hello, world!"); } and you want to call it in main.c: int main() { f(); } this is an error because the compiler treats each .c file separately, so when it’s compiling main.c, it isn’t looking at f.c, so it has no way to know the function f exists. So you need to “declare” it at the top of main.c. A declaration is similar to a definition, except it has no code, and just tells the compiler that the function exists: void f(); But now imagine f.c had many functions. It would be extremely cumbersome to repeat all those declarations in every file that wants to use them. So you can collect all the declarations in some file f.h, and include it from main.c or anywhere else you want to use functions from f.c. When the compiler sees #include "f.h", it operates as if the contents of f.h were copied and pasted at that point. You might ask “why not just include f.c from main.c instead?” Because then you would have two _definitions_ of f in your program (one in f.c and one in main.c), which is illegal, as opposed to two declarations, which is fine.
- clarry 6y ago> this is an error because the compiler treats each .c file separately, so when it’s compiling main.c, it isn’t looking at f.c, so it has no way to know the function f exists. But it's not an error and the compiler does not need to know the function exists. If there's no declaration, then the function's type is implicit and simply assumed to exist. If the function doesn't actually exist, then it's your linker that will complain.
- umanwizard 6y agoYou're right (even with `gcc --std=c99 --pedantic`, which surprised me!) I should have said that the compiler won't be able to type-check the arguments and return value.
- jpm48 6y agoAnother good trick for seeing what headers (and the pre-processor) does in general is using the -E flag (for gcc / clang) it will show the file with the headers included so you can see what happens. This is really useful when using macros and include guards to see what you are getting. clang -E myfile.c > pre.txt will give you an editable file to see what is going on.