Dividing the code in various .c files for PowerPC program

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Dividing the code in various .c files for PowerPC program

4,441 Views
xainub
Contributor I
I am new to code warrior.
 
I am building a C application on powerpc,
Since my main code is very big with lots of fuctions, there our some global structures which these functions update after recieving values from UARTs.
 
I want to divide my code in various .C files, as in the global structures stay in main and the functions are grouped in various .c files. They update the data structures they way they do it in main.
 
Please help me how this can be done.
 
 
~Cheers


Message Edited by J2MEJediMaster on 2007-06-11 10:16 AM
Labels (1)
0 Kudos
7 Replies

437 Views
xainub
Contributor I
Actually I don't know how to implement code division in various C files in Code Warrior
0 Kudos

437 Views
sjmelnikoff
Contributor III
Dividing up a C program into separate files is a fundamental part of programming in C. I strongly suggest you find a good book or website on C, and look up how to structure code in this way.
 
Steve.
0 Kudos

437 Views
xainub
Contributor I
I have done it before,
but Actually I was having problems with extern this time
0 Kudos

437 Views
sjmelnikoff
Contributor III
Well, globals are best avoided. But if you must:
 
Header.h:
 
extern unsigned int MyVar; /* Declaration */
 
File1.c
 
#include "Header.h"
...
unsigned int MyVar; /* Definition */
...
MyVar = 3; /* Use */
 
File2.c
 
#include "Header.h"
...
MyVar = 3; /* Use */
 
 
Steve.
0 Kudos

437 Views
xainub
Contributor I
Thanx alot, I will try keeping all in one header file and adding it in all,
 
Would it be a okay even if i declare them in the common header file rather than main.c?
0 Kudos

437 Views
sjmelnikoff
Contributor III
The declaration (the bit with the extern) must be visible to any source (.c) file which uses the variable, so the common practise is to put it in a header (.h) file, and #include that header wherever the variable is required. The definition acts just like a function prototype.
 
The definition can only exist in one place, as it allocates the space for the variable, so must be in a source (.c) file. In that respect, it behaves like a function body.
 
Again, I strongly recommend that you find a good C reference, and read up on this subject.
 
Steve.
0 Kudos

437 Views
xainub
Contributor I
Hey, thanx alot, I tried it and its working now
I was reading few tutorials, but i had this query and which clearly wasn't answered anywhere.
 
I will do more studyin on this.
 
 
Thanx again.
 
 
0 Kudos