externing a function in a header file

E

Elizabeth

When one does something like this in a header file?
extern something int doSomething(int x)
When would you want to do that in a header file?
Thanks
Elizabeth
 
E

Emmanuel Delahaye

Elizabeth wrote on 01/08/04 :
When one does something like this in a header file?
extern something int doSomething(int x)

ITYM

extern something int doSomething(int x);
When would you want to do that in a header file?

To declare a function that is defined somewhere else in some source
file (*.c).

Note that the 'extern' keyword is not required, but it is informative.
This form of declaration is called a separated prototype or simply a
prototype.
 
E

Elizabeth

To declare a function that is defined somewhere else in some source
file (*.c).
Note that the 'extern' keyword is not required, but it is informative.
This form of declaration is called a separated prototype or simply a
prototype.

why can't one use "include" instead of extern?
 
J

Joe Wright

Elizabeth said:
When one does something like this in a header file?
extern something int doSomething(int x)
When would you want to do that in a header file?
Thanks
Elizabeth

You wouldn't. In a header you might declare the prototype of your
function like this..

int doSomething(int);

Virtually everything declared or defined at file scope has external
linkage so extern is not useful here.

The extern keyword preceeds declaration of an object defined
elsewhere. A good C book (K&R2 comes to mind) explains this fairly well.
 
E

Emmanuel Delahaye

Elizabeth wrote on 01/08/04 :
why can't one use "include" instead of extern?

'include' has no meaning. '#include' is a preprocessor directive, but
it has a completely different meaning. Sounds that its time to reopen
your C-book.
 
K

Keith Thompson

Elizabeth said:
When one does something like this in a header file?
extern something int doSomething(int x)

Apart from the missing semicolon, what's the "something"? Show us an
actual example.
 
E

Elizabeth

Emmanuel Delahaye said:
Elizabeth wrote on 01/08/04 :

'include' has no meaning. '#include' is a preprocessor directive, but
it has a completely different meaning.

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);

The problem is unless you define multbytwo in some other file and put an
include directive in the header file the main file can't call the function
multbytwo in its main function. 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. My other question is do we
have an extern in this format: extern _something int multbytwo(int); 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.
 
J

Jack Klein

When one does something like this in a header file?
extern something int doSomething(int x)
When would you want to do that in a header file?
Thanks
Elizabeth

There is no standard "something" defined in the C standard. Some
compilers provide non-standard extensions of their own, most
particularly Microsoft for Windows, where they don't have a single ABI
but specify many different ways of calling a function.
 
K

Keith Thompson

Elizabeth said:
My other question is do we have an extern in this format: extern
_something int multbytwo(int); 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.

The _something could be any of a number of system-specific things. If
you don't remember what it is, it's unlikely that we can help you.
 
E

Elizabeth

There is no standard "something" defined in the C standard. Some
compilers provide non-standard extensions of their own, most
particularly Microsoft for Windows, where they don't have a single ABI
but specify many different ways of calling a function.
For me it is more important to know why someone would want to put an extern
in a header file. For example, why would anyone want to do this:
/**************
*FILE: main.c
********************/
#include "myheader.h"
int main()
{
printf("multiple by 2: 2 * 5= %d\n", multbytwo(5));
return 0;
}
/****************
*file: myheader.h
****************/
extern int multbytwo(int);

The problem is unless you define multbytwo in some other file and put an
include directive in the header file the main file can't call the function
multbytwo in its main function. 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.
 
G

Gordon Burditt

For me it is more important to know why someone would want to put an extern
in a header file. For example, why would anyone want to do this:
/**************
*FILE: main.c
********************/
#include "myheader.h"
int main()
{
printf("multiple by 2: 2 * 5= %d\n", multbytwo(5));
return 0;
}
/****************
*file: myheader.h
****************/
extern int multbytwo(int);

The problem is unless you define multbytwo in some other file and put an
include directive in the header file the main file can't call the function
multbytwo in its main function.

Yes, you can. Linking several compilation units together is a very
different thing from having one source file 'include' a bunch of
others. For one thing, with linking the source file need not have
any idea where to FIND the function it needs (it might not even be
written yet). That information isn't needed until you get to the
linking instructions (which are implementation-dependent).
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.

Leave out the 'extern'. It doesn't do anything in this context
that leaving it out doesn't.

No, the defined function *DOES NOT* have to be included from another
file. You may end up linking with a vendor-supplied library,
where you don't have the source code to the function at all.

Gordon L. Burditt
 
E

Elizabeth

/**************
No, the defined function *DOES NOT* have to be included from another
file. You may end up linking with a vendor-supplied library,
where you don't have the source code to the function at all.

Gordon L. Burditt
Burditt,
Thanks so much for responding but I get the following error when I put
main.c and myheader.h in one folder and try to compile main.c with cygwin:
/cygdrive/c/DOCUME~1/Elizabeth/LOCALS~1/Temp/ccvNPFDM.o(.text+0x5a):main.c:
undefined reference to `_multbytwo'
collect2: ld returned 1 exit status. I guess that means I have to define
"multbytwo" in another file and include that file in order to make the two
files compile and run.
 
E

Emmanuel Delahaye

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...
 
E

Emmanuel Delahaye

Elizabeth wrote on 02/08/04 :
I get the following error when I put
main.c and myheader.h in one folder and try to compile main.c with cygwin:
/cygdrive/c/DOCUME~1/Elizabeth/LOCALS~1/Temp/ccvNPFDM.o(.text+0x5a):main.c:
undefined reference to `_multbytwo'
collect2: ld returned 1 exit status.

Of course, you have a linker error (ld is the linker of the GCC suite).
I guess that means I have to define
"multbytwo" in another file and include that file in order to make the two
files compile and run.

Yes. The definition of the multbytwo() function is missing. *But you
don't have to include it*. You have to add it to the project. Details
belong to your implementation (makefile, IDE etc.)

See my response in this thread.

And you should not have asked twice the same question in the thread, it
make it hard to follow...
 
M

Michael Mair

Hi Elizabeth,


the others have already explained it fairly well, so I will
just try to give you another point of view which might be
helpful:

You have files main.c, myheader.h, mbt.c where
mbt.c contains the definition of the function multbytwo().
(Adjust the names as fits your purposes. Hint: Calling
myheader.h mbt.h is more suggestive.)


Now, what happens when you compile main.c?

Preprocessor: The header files are included, the comments are removed
and some other stuff.
Compiler: Get the C code into assembler code/machine code. The
compiler ensures that all function names are known -- that is
the sense of the prototype you gave in myheader.h -- that you
produced syntactically correct C code and so on. -> main.o

In order to get an executable program, you need something
"before main()" and something "after main()" which is your
system-specific initialization (you need to jump to main() from
somewhere, for example) and cleanup code (get rid of the memory
needed for the program etc). This stuff is provided by the
linker. That is, you do something like
init.o + main.o + cleanup.o -> executable program
In addition, you are using the printf() function(*) -- this
has to come from somewhere (the standard library). The linker
resolves the symbol "printf" from main.o by saying: You can
find this function here (in the library).
That is, we have
init.o + main.o + standard library + cleanup.o -> executable program
Now, you also have a call to multbytwo(). The linker wants
to resolve the symbol "multbytwo" (in your case _multbytwo)
but does not find it in any of the above files.
So, you have to provide it and the function.

If you compile mbt.c and arrive at mbt.o, then the symbol
can be provided if you tell the linker where to look. That is,
init.o + main.o + mbt.o + standard library + cleanup.o
-> executable program

As you are using cygwin, I guess you are using gcc as compiler:
#compile using C99 standard and warnings. Otherwise std=c89
gcc -Wall -std=c99 -pedantic main.c
-> main.o
gcc -Wall -std=c99 -pedantic mbt.c
-> mbt.o
#link
gcc -o main main.o mbt.o
-> executable program called main


(*) You forgot to provide a prototype for printf. Some compilers
do not tell you because they "know" it is in the standard library,
nonetheless it is a mistake.
You can provide a prototype by
#include <stdio.h>
or directly writing
int printf(const char *format, ...);
before main()


This is not a complete overview so please look it up.
I just wanted to give an impression of what is happening.


Cheers
Michael
 
A

Alan Balmer

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.

Not usually. Usually, the other file is compiled separately and the
linker includes the appropriate binary. The header then tells the
compiler what the function looks like.

It's certainly possible (but bad practice, imo) to #include the
source for the defined function, and if you #include it before it's
used, you don't need the prototype.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top