Pass single character to gcc as definition
I have version.c file with following line:
const uint8_t ID_SoftwareId[] = { 'A', 'B', '0', '1', '0', '\0'};
I want to pass the A
and B
into this file through a definition.
I tired开发者_Python百科 few thigs but I usually end up with Too many initializers
warning from gcc.
Any ideas?
Have you tried simply:
const uint8_t ID_SoftwareId[] = { DEFA, 'B', '0', '1', '0', '\0'};
Then:
gcc -DDEFA=\'A\' ...
WorksForMeTM.
Proof of concept:
#define TOKENIZE(x) #x
#define SINGLECHAR(x) (*(TOKENIZE(x)))
int main()
{
char c = SINGLECHAR(DEFINITION);
return 0;
}
Compiled and run:
cpp -DDEFINTION=Q test.c
Output
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "test.c"
int main()
{
char c = (*("Q"));
return 0;
}
精彩评论