473,320 Members | 2,189 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Advice needed: splitting a project into modules

I'm having problems trying to split my first project into separate .C modules.

As I understand it, using #include has the effect of loading that file into the code at that point, whereas modules are compiled separately, is that right?

Below are the definitions and prototypes as at present. I want to separate this code (plus the associated function definitions) into 4 modules. Turning the included file [1] into a .C file, and each of [2], [3] and [4]. Note that there are function calls from one section to another: for example main() makes a call to tCal() in [2], and tCal() makes a call to testBtn() in [3].

Everything I have tried causes dozens of errors, can anyone tell me what needs to go where?



Expand|Select|Wrap|Line Numbers
  1. // mikroElektronica Libraries used: ADC, CONVERSIONS, EEPROM 
  2.  
  3. #include "nokia3310.h" // I want this to be a separate .C file [1] 
  4. #include <built_in.h> 
  5.  
  6. typedef unsigned short int byte; 
  7.  
  8. #define ModeControl PORTB        // Step = b0-b2, control = b3, Btn = b4 
  9. #define TCal_Ena ModeControl.F3  // Go to Tank Cal if b3=0 
  10. #define Btn_Key ModeControl.F4   // Button pressed if b4=0 
  11. #define FuelADC 0                // use ADC 0 for fuel sensor 
  12.  
  13. #define BufSize 128              // Size of array for EEPROM storage 
  14. #define MaxEntries (BufSize - 8) // maximum number of entries in table 
  15. #define MaxStore (BufSize - 3)   // EE address: max # data tabMax 
  16. #define StepStore (BufSize - 2)  // EE address: step switch data gaugeStep 
  17. #define ChkStore (BufSize - 1)   // EE address: checksum of table 
  18.  
  19. // Global variables 
  20.  
  21. byte volatile fuelLevel; // fuel level from ADC made 8 bit 
  22. byte volatile gaugeStep; // fuel step value, saved in EE StepStore 
  23. byte fuelCalTab[BufSize]; // lookup table for fuel sensor calibration 
  24. byte tabMax;              // index of last entry in table, saved in EE MaxStore 
  25.  
  26. char txt4[4], txt7[7];    // temporary variables to hold conversion strings 
  27. byte i, j;                  // misc loops 
  28.  
  29. // Tank Cal Function Prototypes [2] 
  30.  
  31. void tCal(void); // Calibrate Tank Gauge Step value and calibration points 
  32. void tCalModeMsg(void); // CLS, LCD line 0 = "TANK CAL SET" 
  33. void tCalBootMsg(void); // LCD line 1 = "REBOOT TO EXIT" 
  34. void tCalBtnMsg(void);  // LCD line 5 = "CONTINUE BTN ?" 
  35. void tCal3Msg(void);    // All 3 messages above to LCD 
  36. void tCalStMsg(void);   // LCD line 4 = "STORING DATA", 1 S delay, clear line 
  37.  
  38. // IO Function Prototypes [3] 
  39.  
  40. byte getFuel(void); // Get ADC for fuel level as 0-255 
  41. byte testBtn(void); // Return 1 if button pressed, 100ms delay x 2 
  42.  
  43. // EEPROM Function Prototypes [4] 
  44.  
  45. void loadEEPROM(void); // EEPROM data GSE: fuelCalTab[], tabMax, gaugeStep 
  46. void writeEE(byte addr, byte val);// write EEPROM address with val, delay 20mS 
  47. byte readEE(byte addr);           // return value at EEPROM address, delay 20mS 
  48.  
  49. // Tank Cal Function Definitions 
  50.  
  51. // [code snipped] 
  52.  
  53. /*----------------------------------------- 
  54.                 Main Program 
  55. -----------------------------------------*/ 
  56. void main( void ) 
  57. // [code snipped] 
  58.  
Jan 5 '10 #1

✓ answered by Banfa

Thats right although the normal term is function definition. Also variable definitions go in the C file. The header contains the declarations.

A declaration is a statement that says something, a function or variable, exists somewhere, a definition actually creates the function or variable.

Expand|Select|Wrap|Line Numbers
  1. // Function declaration, says func exists and 
  2. // declares how to call it but doesn't define the body
  3. extern int func(int x);
  4.  
  5. // Variable declaration says var exists and what its type is
  6. extern int var;
  7.  
  8. // Function declaration, says func exists and 
  9. // declares how to call it but doesn't define the body
  10. extern int func(int x);
  11.  
  12. // Variable declaration says var exists and what its type is
  13. extern int var;
  14.  
  15.  
  16. // Function definition defines the body of the function
  17. int func(int x)
  18. {
  19.     return x/2;
  20. }
  21.  
  22. // Variable definition causes the compiler and 
  23. // linker to actually allocate storage for var
  24. int var;
  25.  
As a rule of thumb definitions actual produce some output in the program, function definitions produce executable code, variable definitions produce storeage locations while declarations are just information for the compiler while it compiles the code.


And finally although I have talked about variable declarations and definitions it is actually bad practice to use global data and can cause maintenance and debug fixing issues. Best practice is to not use global data, that is variables accessable from all files in a project. Data declared statically at file scope, i.e. available to all the code in a single file is not much better some is sometimes unavoidable.

8 3125
Create a single Project. Create four .C files and four header files. Include the header files needed for particular .C file. Make sure your linker path is correct.
Jan 5 '10 #2
alexis4
113 100+
Say you have a header file and 2 source files.
Expand|Select|Wrap|Line Numbers
  1. //mydefines.h
  2.  
  3. #define  five  5
Expand|Select|Wrap|Line Numbers
  1. //file1.c
  2.  
  3. #include "mydefines.h"  //declare the whole path if
  4. //header and source are not in the same folder
  5.  
  6. int b;  //global variable, declared outside of function
  7. extern void func1 (int a);  //external function
  8.  
  9. void main (void)
  10. {
  11.   int x = five;  //"five" is from mydefines.h
  12.   func1(x);  //external call, makes b=20
  13.   b++;  //b = 21
  14. }
Expand|Select|Wrap|Line Numbers
  1. //file2.c
  2.  
  3. #include "mydefines.h"
  4.  
  5. extern int b;  //must be global in file1 to use it in file2
  6.  
  7. void func1 (int a)
  8. {
  9.   a = 2 * five;  //2*5=10
  10.   b = a + 10;  //b=20. It shouldn't be returned
  11. //because it is a global variable
  12. }

This is dummy code, but it contains example for global variable, external function call and custom header file, so this is everything you need.
Jan 5 '10 #3
Banfa
9,065 Expert Mod 8TB
Actually with the file structure you propose alexis the line

extern void func1 (int a); //external function

should not be in file1.c but should be in mydefines.h

This ensures that file1.c that calls the function uses the same prototype as file2.c where func1 is defined.

The basic principle is that for any function you should declare it once in a place that is visible to everywhere that calls the function as well the place it is defined.

if you don't do this you run the risk that the function is declared and defined differently where as if you do do this and there is a mismatch between the declaration and definition the compiler will produce an error.

The same applies to the declaration of b in file2.c

extern int b; //must be global in file1 to use it in file2

it should be in mydefines.h
Jan 5 '10 #4
alexis4
113 100+
Yeah you are right!!! I always use header files just for the defines and I call functions and variables from source files. That seemed more logical to me, because I split my source files (with their functions and variables) according to their logical operations.
My solution also works (really, my projects are compiled just fine), but yours is much more structured. Thanks, I will definitely try it starting from the current project I am working!
Jan 5 '10 #5
Banfa
9,065 Expert Mod 8TB
If you would like a suggestion what I do is have a header file for every C file often. Then you declare everything that is defined in a give C file (file1.c for example) in the correspondingly named header file (file1.h for example).

I normally only do it this way after there are a handful of C files otherwise I use a single header file.
Jan 5 '10 #6
alexis4
113 100+
Thanks Banfa, I have seen what you are saying in C-based Real Time Operating Systems, but never thought to try it my self! I concentrated at RTOS's fancy stuff like callbacks and function pointers and missed a simple but yet so important thing like that!
Thanks again!
Jan 5 '10 #7
extern void func1 (int a); //external function

should not be in file1.c but should be in mydefines.h


Hi Banfa,

So this is a mydefines.h file with all the function prototypes and variable declarations (all declared as external) for the whole project?

If so, then what goes in the individual .C files, just the function bodies?
Jan 6 '10 #8
Banfa
9,065 Expert Mod 8TB
Thats right although the normal term is function definition. Also variable definitions go in the C file. The header contains the declarations.

A declaration is a statement that says something, a function or variable, exists somewhere, a definition actually creates the function or variable.

Expand|Select|Wrap|Line Numbers
  1. // Function declaration, says func exists and 
  2. // declares how to call it but doesn't define the body
  3. extern int func(int x);
  4.  
  5. // Variable declaration says var exists and what its type is
  6. extern int var;
  7.  
  8. // Function declaration, says func exists and 
  9. // declares how to call it but doesn't define the body
  10. extern int func(int x);
  11.  
  12. // Variable declaration says var exists and what its type is
  13. extern int var;
  14.  
  15.  
  16. // Function definition defines the body of the function
  17. int func(int x)
  18. {
  19.     return x/2;
  20. }
  21.  
  22. // Variable definition causes the compiler and 
  23. // linker to actually allocate storage for var
  24. int var;
  25.  
As a rule of thumb definitions actual produce some output in the program, function definitions produce executable code, variable definitions produce storeage locations while declarations are just information for the compiler while it compiles the code.


And finally although I have talked about variable declarations and definitions it is actually bad practice to use global data and can cause maintenance and debug fixing issues. Best practice is to not use global data, that is variables accessable from all files in a project. Data declared statically at file scope, i.e. available to all the code in a single file is not much better some is sometimes unavoidable.
Jan 6 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
9
by: Rick Muller | last post by:
I have a problem that I would like to get some advice on from other Pythonistas. I currently manage a (soon to be) open source project for displaying molecular graphics for a variety of different...
8
by: M O J O | last post by:
Hi, I'm creating an CRM solution for my company. I want to split up the solution into several classlibraries, so I dont need to build the entire solution every time I run my project. First...
24
by: sundew | last post by:
Hi all, I am developing an open source DHTML project named Wednus Window. http://wednus.com Wednus Window is a DHTML Web-Application Windowing System. It shell websites/web-applications with...
3
by: dlesandrini | last post by:
I need advice about my decision to go with Replication in general. This post was placed on the Microsoft Replication newsgroup, but I really value the feedback that comes from this group as well. ...
3
by: swingingming | last post by:
Hi, for 5 weeks, I finished my mdb project. Thanks to all you guys. Now, I would like to put it on a server then 5-6 people can share it. I heard about the splitting back-end database, put it on a...
3
by: Corobori | last post by:
I developed a vb.net application which has about 90 forms an 70 reports. I have got the following question: my customer asked me to split up my application in several application. One containing...
5
by: Steve | last post by:
Hi, I am sitting down to design our next set of internal apps. I would like to approach this in a way that would allow me to break logical parts of the application that handle specific tasks...
2
by: oadaniel3 | last post by:
I have an accounting application written in BASIC. Needless to say I'm am losing customers to "windows" apps. Several years ago, we made the decision to rewrite all the modules to run as web apps. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.