Reputation: 113
My unknown predecessor who has gone left codes. I try to use some of them, but his codes look unusual.
1.What he did is define variables in not a header but c file : variables.c
CHGR_CMD comand[MAX_COMANDS] __attribute__ ((section ("external")));
RAWDAT rawdat[MAX_RAWDAT] __attribute__ ((section ("external")));
SC_TEST *curr_test[NUMCHANS]; /* array of pointers to current testrec */
SC_TESTSETUP chparam[NUMCHANS] __attribute__ ((section ("external")));
SC_HOLDSTAT stats[NUMCHANS] __attribute__ ((section ("external")));
RAWDAT *bufs[NUMCHANS];
//FACTS control[NUMCHANS];
short ntest;
short new_dbuff;
short start_err;
short update_status;
long status_sent;
short load_done;
char h_lok; ....
2 init_vars() in another c file initializes above variables : init_vars.c
memset((char *) rawdat,0 ,sizeof(rawdat));
memset((char *) curr_test,0 ,sizeof(curr_test));
memset((char *) chparam,0 ,sizeof(chparam));
memset((char *) stats,0 ,sizeof(stats));
memset((char *) bufs,0 ,sizeof(bufs));
memset((char *) comand,0 ,sizeof(comand));
ntest = 0;
new_dbuff = 0;
start_err = 0;
3.call init_vars() in main.c
The first time I tried to include "variables.c" directly in init_vars.c, but it gave me below errors:
#10056 symbol "ad_lok" redefined: first defined in "./variables.obj"; redefined in "./init_vars.obj" sCharger_Test_v1 C/C++ Problem
Not only ad_lok variable but also every variables defined in variables.c hit errors.
Changing variables.c to variables.h still gives me same errors.
How can I handles variables defined c file not header file?
Thanks
Upvotes: 1
Views: 885
Reputation: 3701
Because variables.c
does not declare variables or preprocessor macros, but actually defines these variables, you will need to make them extern
in the "include" phase, and define them in a single file (say, externs_definitions.c
).
For example:
variables.h
#ifndef VARIABLES_H
#define VARIABLES_H
extern CHGR_CMD comand[MAX_COMANDS] __attribute__ ((section ("external")));
extern RAWDAT rawdat[MAX_RAWDAT] __attribute__ ((section ("external")));
extern SC_TEST *curr_test[NUMCHANS]; /* array of pointers to current testrec */
...
#endif /* VARIABLES_H_ */
Then leave variables.c as is.
Then in init_vars.c
#include "variables.h"
Further questions though; how does the code work currently? If init_vars.c
doesn't directly include variables.c
and there's no header file declaring them as extern
, how does the existing code not fail the preprocessor trying to access undeclared variables?
Upvotes: 1