ARM Technical Support Knowledge Articles

MODULAR PROGRAMMING

Applies to: General Topics

Answer


Information in this article applies to:


QUESTION

I want to declare a global variable that can be addressed from various modules. My program is as follows:

File1.C:

unsigned int val = 0;

void main (void) {
  while (1) {
    myfunc ();      // a function in a different module
  }
}

File2.C:

void myfunc (void)  {
  val = 1;         // does not work
}

How do I need to structure my project?

ANSWER

Good modular programming uses a header file that defines the resources of the application. In complex applications you may have a header file for each module.

Example:

stuff.h:

extern void myfunc (void);    // function in File2.C
extern unsigned int val;      // variable in File1.C

Now you use this header file in all your C modules to define the global resources of your application.

File1.C:

#include "stuff.h"

unsigned int val = 0;

void main (void) {
  while (1) {
    myfunc ();      // a function in a different module
  }
}

File2.C:

#include "stuff.h"

void myfunc (void)  {
  val = 1;         // uses the extern definition in stuff.h
}

For another example of this, review the Measure example included in all Keil development tool packages.

MORE INFORMATION

SEE ALSO

Article last edited on: 2007-01-09 14:37:25

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential