2005-01-19 02:28 AM
Include problems (Cosmic compiler)
2005-01-18 12:58 AM
Hello,
At the moment I am having some problems with compiling some c source. I use include ''user_var.h'' to include some global variables and defines, etc. But I can only call the include file from one c source-file. If I use the include statement in more than one c-file I get an error from the linker: ''symbol temp multiply defined''. So I don't use the include anymore, but every c file has the variable declared as an external variable on top of the file (eg. 'extern char temp;'). Can I use the include statement instead? The include file has the '#ifndef USER_VAR_H', '#define USER_VAR_H' & '#endif' statements. What's wrong? Thanks in advance for your help. Kind regards, Mark [ This message was edited by: MarkBng on 18-01-2005 14:52 ]2005-01-18 04:14 AM
One solution is to use the include file for defining the global variables but define each variable as EXTERN in my_include.h.
In the C file where you want the variables to defined (physical memory allocated to them), just before #include my_include.h, redefine EXTERN as nothing, the variable will then be allocated in the C file. In all the other C file where you want he global variables defined in my_include.h to be known, redefine EXTERN as extern before #include my_include.h, then only the references of the variables will be given by the include file. This will ensure that you have the same definitions of all gobal variables in all your project and there is no duplication of the global variables definitions. Regards, Alain [ This message was edited by: Alaini on 18-01-2005 17:47 ]2005-01-19 02:28 AM
Quote:
The include file has the '#ifndef USER_VAR_H', '#define USER_VAR_H' & '#endif' statements. What's wrong? If you want to understand that, you need to run the preprocessor only and take a look at the output file (which is normally fed directly to the compiler): cpst7 -i INCLUDE_PATH -e -o PREPROC_OUTFILE INPUTFILE.c This will run the preprocessor only and send its output to PREPROC_OUTFILE. Looking at output file for different input files you will probably see that your global variables are actually passed on to the compiler in every input file, hence the error you get. You can go on to debug from there. Hope it helps, Luca (Cosmic)