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? - // mikroElektronica Libraries used: ADC, CONVERSIONS, EEPROM
-
-
#include "nokia3310.h" // I want this to be a separate .C file [1]
-
#include <built_in.h>
-
-
typedef unsigned short int byte;
-
-
#define ModeControl PORTB // Step = b0-b2, control = b3, Btn = b4
-
#define TCal_Ena ModeControl.F3 // Go to Tank Cal if b3=0
-
#define Btn_Key ModeControl.F4 // Button pressed if b4=0
-
#define FuelADC 0 // use ADC 0 for fuel sensor
-
-
#define BufSize 128 // Size of array for EEPROM storage
-
#define MaxEntries (BufSize - 8) // maximum number of entries in table
-
#define MaxStore (BufSize - 3) // EE address: max # data tabMax
-
#define StepStore (BufSize - 2) // EE address: step switch data gaugeStep
-
#define ChkStore (BufSize - 1) // EE address: checksum of table
-
-
// Global variables
-
-
byte volatile fuelLevel; // fuel level from ADC made 8 bit
-
byte volatile gaugeStep; // fuel step value, saved in EE StepStore
-
byte fuelCalTab[BufSize]; // lookup table for fuel sensor calibration
-
byte tabMax; // index of last entry in table, saved in EE MaxStore
-
-
char txt4[4], txt7[7]; // temporary variables to hold conversion strings
-
byte i, j; // misc loops
-
-
// Tank Cal Function Prototypes [2]
-
-
void tCal(void); // Calibrate Tank Gauge Step value and calibration points
-
void tCalModeMsg(void); // CLS, LCD line 0 = "TANK CAL SET"
-
void tCalBootMsg(void); // LCD line 1 = "REBOOT TO EXIT"
-
void tCalBtnMsg(void); // LCD line 5 = "CONTINUE BTN ?"
-
void tCal3Msg(void); // All 3 messages above to LCD
-
void tCalStMsg(void); // LCD line 4 = "STORING DATA", 1 S delay, clear line
-
-
// IO Function Prototypes [3]
-
-
byte getFuel(void); // Get ADC for fuel level as 0-255
-
byte testBtn(void); // Return 1 if button pressed, 100ms delay x 2
-
-
// EEPROM Function Prototypes [4]
-
-
void loadEEPROM(void); // EEPROM data GSE: fuelCalTab[], tabMax, gaugeStep
-
void writeEE(byte addr, byte val);// write EEPROM address with val, delay 20mS
-
byte readEE(byte addr); // return value at EEPROM address, delay 20mS
-
-
// Tank Cal Function Definitions
-
-
// [code snipped]
-
-
/*-----------------------------------------
-
Main Program
-
-----------------------------------------*/
-
void main( void )
-
{
-
// [code snipped]
-
}
-
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. -
// Function declaration, says func exists and
-
// declares how to call it but doesn't define the body
-
extern int func(int x);
-
-
// Variable declaration says var exists and what its type is
-
extern int var;
-
-
// Function declaration, says func exists and
-
// declares how to call it but doesn't define the body
-
extern int func(int x);
-
-
// Variable declaration says var exists and what its type is
-
extern int var;
-
-
-
// Function definition defines the body of the function
-
int func(int x)
-
{
-
return x/2;
-
}
-
-
// Variable definition causes the compiler and
-
// linker to actually allocate storage for var
-
int var;
-
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.
Say you have a header file and 2 source files. -
//mydefines.h
-
-
#define five 5
-
//file1.c
-
-
#include "mydefines.h" //declare the whole path if
-
//header and source are not in the same folder
-
-
int b; //global variable, declared outside of function
-
extern void func1 (int a); //external function
-
-
void main (void)
-
{
-
int x = five; //"five" is from mydefines.h
-
func1(x); //external call, makes b=20
-
b++; //b = 21
-
}
-
//file2.c
-
-
#include "mydefines.h"
-
-
extern int b; //must be global in file1 to use it in file2
-
-
void func1 (int a)
-
{
-
a = 2 * five; //2*5=10
-
b = a + 10; //b=20. It shouldn't be returned
-
//because it is a global variable
-
}
This is dummy code, but it contains example for global variable, external function call and custom header file, so this is everything you need.
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
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!
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.
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!
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?
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. -
// Function declaration, says func exists and
-
// declares how to call it but doesn't define the body
-
extern int func(int x);
-
-
// Variable declaration says var exists and what its type is
-
extern int var;
-
-
// Function declaration, says func exists and
-
// declares how to call it but doesn't define the body
-
extern int func(int x);
-
-
// Variable declaration says var exists and what its type is
-
extern int var;
-
-
-
// Function definition defines the body of the function
-
int func(int x)
-
{
-
return x/2;
-
}
-
-
// Variable definition causes the compiler and
-
// linker to actually allocate storage for var
-
int var;
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
| |