08-16-2010 03:39 AM
Within in a main.c i am having a CallBack function , where get the current value of a control.
GetCtrlVal (panelHandle, PANEL_RINGSLIDE_1, &p);
I am having another file ( test.c) in the same folder. I need to use the value of &p in that test.c file. How is that possible??
I though of including #include main.c in the test.c file but did not work.
Any ideas?
Solved! Go to Solution.
08-16-2010 05:46 AM
In applications that use more than one source file, it is good practice to have a common set of definitions that covers your entire project. So, for example, you could have a file "common.h", which is #include'd by both your main.c and test.c files. In that common.h file, you could have a statement:
extern int p;
This tells the compiler, for each of your c file compilation steps, that the variable p is defined somewhere else and not to worry about it. Now in just one of your .c files (it does not really matter which one) you then need to declare the actual variable p:
int p;
So now when you refer to p in any of your modules, you are referring to the same variable across the entire project. (It is probably best to use a longer, more descriptive name for the variable instead of just p - this will avoid possible confusion as your program grows in complexity.)
JR
08-16-2010 06:23 AM - edited 08-16-2010 06:33 AM
I created a seperate file named "common.h" with following codes:
extern int period; extern int amplitude;
I also included this common.h in both main.c and test.c
#include "common.h"
But theb i get these errors:
2 Project link errors:
Undefined symbol '_amplitude' referenced in "generate.c".
Undefined symbol '_period' referenced in "generate.c".
--------------------------------------------
oops.. now i found out theproblem. I had defined the variables period and amplitude inside the main. Now when i took them outside the main, it worked. Yes i guess it is better to create a common file like "common.h" when you work with several variables several times.
thank ypu very much jr_2005..