Elizabeth wrote on 02/08/04 :
Ok, suposse we have the following files:
/**************
*FILE: main.c
********************/
#include "myheader.h"
int main()
{
printf("This is main\n");
printf("multiple by 2: 2 * 5= %d\n", multbytwo(5));
return 0;
}
/****************
*file: myheader.h
****************/
extern int multbytwo(int);
As I told you before:
"To declare a function that is defined somewhere else in some source
file (*.c)."
this function is only declared here. You need a definition elswere:
/****************
*file: multbytwo.c
****************/
#include "myheader.h"
/* Actually, bad name... */
int multbytwo (int val)
{
return val * 2;
}
Now, you have to add this file to the project and to leave the linker
make its job.
The problem is unless you define multbytwo in some other file
Yes, this is exactly what have to be done.
and put an
include directive in the header file the main file can't call the function
multbytwo in its main function.
Huh! No. The prototype is enough for a call. The declaration holds the
information required by the compiler to generate a correct function
call:
- The name of the function
- The number, type and order of parameters
- The type of the return
What is missing is the exact address of the called function, but this
is exactly the aim of the linker to solve these kind of missing
information.
My question is why should someone want to do
"extern int multbytwo(int);" in a header file when ultimately the defined
function has to be included from another file.
You have it wrong. The purpose of the #include directive is not to
include definitions of code or objects, but to include declarations of
them.
The C language (like many others) supports the separated compile
feature. You should reread your C-book for details.
My other question is do we
have an extern in this format: extern _something int multbytwo(int);
What is this '_something' ? Give an actual example.
I don't
seem to be able to compile my code with _something before the return int
type but I remember seeing a code that had _something in between extern and
the return type of the function. I don't remember what that _something was.
Neither do I! You must be more accurate. May be you are thinking of
some compiler extension like 'pascal' or 'EXPORT' or 'far' or 'WINAPI',
I dunno...