[Courses] C Programming For Absolute Beginners, Lesson 5: All About Functions, part 1

Akkana Peck akkana at shallowsky.com
Wed Mar 14 03:24:31 UTC 2012


Sachin Divekar writes:
> What is the difference between using #include and linking together
> using gcc as shown in above example?

Generally, include files will have function prototypes, but no actual
code that does anything. So you might have a file add.h that has:

int myadd(int a, int b);

And then you'd have a different file, myadd.c, that had the code
implementing that function:

int myadd(int a, int b)
{
    return a + b;
}

Then more than one file can #include "myadd.c" without getting
duplicate copies of the code for the myadd() function.

Suppose you had a whole bunch of files to link together, and all
of them needed to call myadd():

gcc main.c something.c else.c add.c -o myprogname

If each of them includes myadd.h, and you put the whole function
definition inside myadd.h, then each of those files would have its
own copy of the function, and the linker would complain about the
duplicates. But if there's only one copy, in add.c, then all the
other files can use it without there being any duplication.

Does that help?

> > Though, as I said, it is usually more sensible to put the prototype in a
> > separate file and #include it among the various files that need it. With
> > shared libraries (utilizing dynamic linking), you also #include a
> > prototype from a header, but the real function is not provided at all
> > during compiling! Rather, it is provided later by the system at the
> > beginning of the program's execution. This allows people to code their
> > programs for a particular library interface (i.e., set of function
> > prototypes) while the library functions themselves may differ somewhat
> > from system to system. (For example, depending on the special needs of
> > the system. This is how OpenGL still works, even though we all have
> > different graphics cards.)
> 
> Can somebody please elaborate this paragraph?

The prototype is just something giving the name of the function,
what kind of arguments it takes and what it returns (no
implementation code).

The implementation could be compiled together with the other files
in the project, as in the example above. Or you could take add.c
and make it part of a whole separate library, mymath.so (so stands
for "shared object" and is one common kind of library). Then all
sorts of programs could use your mymath library. You'd have an
include file for your library (you'd probably want to call it
mymath.h rather than add.h, but it doesn't really matter) and anyone
who wanted to use your myadd() function could include that file.
Then when they link their program, they'd link against your library:

cc main.c -lmymath -o myprogram

If you wanted to, you could even have different types of mymath
libraries -- you could have one built to work on wimpy 8-bit
processors called mymath8.so and another built to work on exotic
supercomputers called mymathsuper.so, and of course the Linux
mymath.so would be different from the solaris mymath.so. Then they
could link against the appropriate version of the library, but the
mymath.h header would be the same no matter what so the source code
in main.c doesn't have to change.

	...Akkana


More information about the Courses mailing list