Maybe we should slow down a bit...
dsyleixa123 states in another thread that he or she is a beginner. We in this forum should point out things that are really unusual or bad practice. They may serve to make an academic point, but people tend to dismiss that code and we should point that out. Of course people are battling over style issues when it comes to code a lot, but a few things I guess are not controversial.
So we should say that this (including a .c file in another) is
really bad practice:
/mymainprogram.c
#include "mylib.c"
int main() {
myfunction();
return 0;
}
I'd say you should
never include another .c file this way.
The way it was shown with a header file (mylib.h) and two source files (mylib.c and mymainprogram.c) is (for larger files at least) the way to go, especially if there is a chance that whatever mylib.c provides is useful with other programs (then it is usually made into a library, as the name already implies).
Also, no one (I may have missed it) has commented on the use of the "extern" in
that is not needed. I teach polemically that "extern is evil" - ok, what I want to get across is that if you need to use "extern", you'd better have a good reason to do so. In this case, the function is not "extern". It is defined right in the code we have.
In this context, let me take another stab at the explanation with header files. We want to separate the description from implementation. All we need to know is how something can be called, and our program doesn't really care at how it is done and what happens under the hood. I like to call a declaration like
Code: Select all
//mylib.h
#pragma once
void myfunction(void);
a "contract" - the header file tells the program what to expect, and my program uses that service.
Look at a version of the classic "hello world" program:
Code: Select all
#include <stdio.h>
int main()
{
printf ("Hello, World!\n");
return 0;
}
Ok, easy enough. Just like your mylib.h, stdio.h tells the compiler what "printf" is, how it can be called, and what it returns, if anything - the contract part. Then the compiler knows what to do, and the linker then goes and puts the call together with the printf function's implementation.
Now where does the linker find that implementation? The system provides a bunch of libraries, usually called system libraries, with this installation. A library that provides that heavily OS-dependent "printf" is referenced by the linker by default, and that's why, different from your example "mylib.c" implementation, you don't have to explicitly specify this.
Again, my "contract" paradigm - you can imagine that the specifics of actually printing "Hello, World" to your screen (going through the formatting, then talking to your window manager, your graphics card, and on and on, to make those letters appear on the screen) is highly system- and hardware dependent (and also differs between a Linux system, a Mac, a Windows system). Yet by just exercising that service, described by the contract, yields the same result across different hardware, OS flavours, and so on.
Does this make sense? Hope it helps.
- mlp