In file AAA.c
const char CMD_Fire[] = "FIRE";
In file BBB.c
extern const char CMD_Fire[];
It seems that, the compiler is not able to know the size of CMD_Fire[], when compiling BBB.c
So, I have to hard code the size of CMD_Fire[] in BBB.c
Then, if I change the command to "Fire it!", I will need to change the size of CMD_Fire[] everywhere.
Is there any skill to handle this?
Of course it isn't - because you have not provided it with that information!
This has nothing to do with ARM or Keil - this is standard 'C' behaviour!
A 'C' compiler "sees" only one "compilation unit" at a time. In this case, the only information available in the "compilation unit" is your extern declaration:
which, clearly, contains no length information!
In this particular case, since it's a string, the best approach would probably be to use string functions - which will automatically detect the end of the string (and, hence, its length) at run-time.
Alternatively, proceed as follows:
cmd.h
#define LEN_CMD_FIRE 5 // Remember to count the NUL! extern const char CMD_Fire[LEN_CMD_FIRE];
AAA.c
#include "cmd.h" const char CMD_Fire[LEN_CMD_FIRE] = "FIRE";
BBB.c
#include "cmd.h" // now you can use CMD_Fire and LEN_CMD_FIRE...
#define TXT_CMD_FIRE "FIRE" // The text #define LEN_CMD_FIRE 5 // Remember to count the NUL! extern const char CMD_Fire[LEN_CMD_FIRE];
#include "cmd.h" const char CMD_Fire[LEN_CMD_FIRE] = TXT_CMD_FIRE ;
This means that only 1 file - cmd.h - has to be edited to update the string(s)...
Thanks for all the replies.
Many thanks to Andy's helps and the very good examples.
I know that I still lack a lot of fundamental knowledge; sorry for that.
If I do this globally:
extern const char TEST_COMMAND[]; const char TEST_COMMAND[] = "TEST";
It is OK.
If I do this locally:
void CMD_Handle(void) { extern const char TEST_COMMAND[]; const char TEST_COMMAND[] = "TEST"; // Line 517 UART3sbWRITE( (BYTE *)TEST_COMMAND, 4 ); UART3sendKICK(); }
I get the error.
source\RS485.c(517): error: #101: "TEST_COMMAND" has already been declared in the current scope
I think that, I don't really understand "extern". I am confused.
The stability of our internet connection is very bad. So my Firefox was just frozen, then some error occured.
View all questions in Keil forum