After reading these posts I thought I might add my two cents and discuss 
the extern keyword.

Before addressing the extern keyword specifically let me take a moment to 
talk about scope in C.  You have several levels of scope:
        Local;  only visible inside of the function, value lost on exit from function
        Local Static;   only visible inside of the function, value maintained on 
exit from function
        Module Global;  a variable defined inside of the source module with no 
extern declaration in its .h file
        Global; a variable defined inside a source module with an extern 
declaration in its .h file

If you want a global variable that can be accessed by all of your C Modules 
then you need to have an extern declaration in your project .h file which 
gets included in all of your project .c files.

Here is an example:
---------------------------------------------------------------------------------------------------------------
// utility module
#include "project.h"

UInt16 f(int x)
{
        return x*2;
}
---------------------------------------------------------------------------------------------------------------
// utility.h
#ifndef UTILITY
#define UTILITY

extern UInt16 f(int x);

#endif
---------------------------------------------------------------------------------------------------------------
// project.h
#ifndef PROJECT
#define PROJECT

#include <PalmOS.h>
#include "utility.h"

extern DmOpenRef gdbMain;
#endif

---------------------------------------------------------------------------------------------------------------
// project.c
#include "project.h"

DmOpenRef gdbMain;

void main()
{
        UInt16 i;
        i = f(5);
}

---------------------------------------------------------------------------------------------------------------

I know its a trivial example but it shows the basic structure of a multi 
module C program.
The #ifndef ------ line is used so that even if a header file is included 
twice it will not create a compiler error.  All system includes should be 
in the project.h and all other .c files should just include the project.h 
file, unless the module is specifically being made to be reusable in other 
projects then it should only include its .h file.  This is all fairly 
standard C stuff and you can find it in any C reference (or even C++).

I hope this helps.
Richard


-- 
For information on using the Palm Developer Forums, or to unsubscribe, please see 
http://www.palmos.com/dev/support/forums/

Reply via email to