You and half the forum. I think we've said that in every single thread NunoLava1999 has started, actually.
At least he finally listened about the cloud repo. That's an encouraging sign.
Oh, and just to make sure that what Lukand said was clear: the main issue is the presence (or absence) of what are called
function prototypes. A function prototype declares the existence of the function to the compiler, whether it is in another source file, an object file that is already assembled or compiled, or further down the page in the same source file. It gives the name of the function, and (at minimum) the types of the parameters it takes. So, for example, if you have a function
foo() that takes an unsigned int and a char array as its parameters and returns a char, and want to use it in your
main() function, you could declare is with the the function prototype
Note the semicolon at the end; this indicates that it is a prototype, not the start of the function definition itself.
Now, while you don't (in older versions of C, anyway, I haven't checked C11's rules yet) need to put the parameter names in the prototype, it is generally a good practice to do so, to make the prototype more self-documenting (that is, giving the names of the parameters
should help explain their purpose). Thus, you really should write it as
Code: Select all
char foo(unsigned int index, char foo_table[]);
which tells you that it is basically a wrapper around access to a table (which you might do in order to, say, provide some kind of validation on the table access).
You can put the prototypes anywhere in a source file so long as they appear before any place you use the functions, but since prototypes for library functions get used in several different places, you generally want to put those into a separate file which can be automatically inserted into the compiler's source code stream using the
#include preprocessor directive. These are called
header files, and are generally used to collect things like prototypes, type declarations, macro definitions,
extern declarations of global variables, and pretty much anything else - so long as there are no functions declared inside it. The reason that you don't put function declarations in headers is because that would lead to the same function getting compiled in several places, making it impossible for the linker to resolve which copy of the function is the correct one (regardless of whether they are defined as the same or not).
So, if you use the header file as Lukand decribed above, for his Basicarithmetic.cpp file:
Code: Select all
#include"myheader.h"
int add(int a, int b)
{
return a+b;
}
what the compiler actually sees is:
Code: Select all
int add(int a, int b);
int add(int a, int b)
{
return a+b;
}
(The
#pragma once line is a preprocessor directive telling it not to paste the code of a given header file into the current source stream more than once, even if it is included in another header file that is itself included
in the source stream.)
As you have probably guessed, you have already been using these include files all along; the standard headers such as <stdio.h> and so on are used to declare the standard library functions, such as
printf() and
pow(). These functions are
not part of the language itself, but are in library files - object files which hold archives of several related functions - that are defined in parallel to the core language as part of the language standard.
Core C is actually a very, very small language, and except for those things that have special syntax for them (that is, things that aren't function calls, such as if(), for(), while() and so forth) most of the things that are thought of as being part of C are actually library code, not core language syntax. This is part of the advantage it has as a systems-programming language - you can define your own versions of the I/O functions and things of that nature, without conflicting with the standard ones so long as you tell the compiler not to use the standard ones (disabling automatic linking to standard library is part of what the GCC option
-ffreestanding is for).
Anyway, as I said before,
this wiki page explains the whys and wherefores of file inclusion, though at this point I have just covered most of what it has to say.