Home
Developer Resources
QNX RTOS v4
QNX RTOS v4 Knowledge Base

QNX RTOS v4 Knowledge Base

Foundry27
Foundry27
QNX RTOS v4 project
Resources

QNX RTOS v4 Knowledge Base

Title Using global variables in multiple source files.
Ref. No. QNX.000010338
Category(ies) Development
Issue Instead of having to re-declare your global variables within each source file you use them in, you can simply declare them once in your  header file.
Solution There is an easy way to save time when dealing with global variables in multiple source files.  You can do the following instead of defining your global variables within your header file, and then re-defining them as "extern" in each source file that uses them.

=========================
= Within your main code =
=========================
#define DEFINE_GLOBALS
#include "globals.h"
/* This allows the test in the header file to know to initialise all the variables normally */


========================================
Within any other source using the global Variables 
========================================
#include "globals.h"
/* This allows the test in the header file to know that the variables have been initialised and to use them as "extern" */

============================
=  Within your header file =
============================
#ifdef DEFINE_GLOBALS
#define GLOBAL
#else
#define GLOBAL extern

/* List your global variables starting with GLOBAL */
GLOBAL int x09myglobalvar;
/* If the DEFINE_GLOBALS has been defined this example would be interpreted as "int myglobalvar;" otherwise it will be interpreted as "extern int myglobalvar;" */