Get cc constant value

I have a .h file in which hundreds of constants are defined as macros:

#define C_CONST_NAME Value 

I need a function that can dynamically get the value of one of these constants.

required function header:

 int getConstValue(char * constName); 

Is this possible in the C language?

---- EDIT

Thanks for the help, it was fast :)

since I thought there was no miraculous solution for my needs.

The actual header file is generated by the command "SCADE: http://www.esterel-technologies.com/products/scade-suite/ "

From the solution I got from @Chris, is to use some python to generate c-code that does the job.

Now I need to do some optimizations to find the constant name. I have over 5000 O constants (500 ^ 2)

I also look at "X-Macros." The first time I hear about it, it works in C home, because I am not allowed to use C ++.

thanks

+6
source share
6 answers

Here you go. You will need to add a line for each new constant, but it should give you an idea of ​​how macros work:

 #include <stdio.h> #define C_TEN 10 #define C_TWENTY 20 #define C_THIRTY 30 #define IFCONST(charstar, define) if(strcmp((charstar), #define) == 0) { \ return (define); \ } int getConstValue(const char* constName) { IFCONST(constName, C_TEN); IFCONST(constName, C_TWENTY); IFCONST(constName, C_THIRTY); // No match return -1; } int main(int argc, char **argv) { printf("C_TEN is %d\n", getConstValue("C_TEN")); return 0; } 

I suggest you run gcc -E filename.c to find out what gcc does with this code.

+4
source

C cannot do this for you. You will need to store them in a different structure or use a preprocessor to create the hundreds of if statements you need. Something like Cogflect might help.

+6
source

The C preprocessor macro (that is, something called the #define ) ceases to exist after the preprocessing completes. The program does not know the names of these macros and cannot refer to them.

If you tell us what task you are trying to accomplish, we can offer an alternative approach.

+3
source

This is what X Macros are used for:

https://secure.wikimedia.org/wikipedia/en/wiki/C_preprocessor#X-Macros

But if you need to map the string to a constant, you have to look for the string in an array of string representations that is O(n^2) .

+1
source

There is no such feature built into C. However, you can use a tool like doxygen to extract all #define from your source code into a data structure that can be read at run time (doxygen can store all macro definitions in XML).

+1
source

Perhaps you can do this with gperf , which generates a search function that uses the perfect hash function .

Create a file similar to the following and run gperf with the -t option:

 struct constant { char *name; int value; }; %% C_CONST_NAME1, 1 C_CONST_NAME2, 2 

gperf will output C (or C ++) code that performs a constant search, returning a pointer to a key / value pair or NULL.

If you find that your keyword set is too large for gperf, consider cmph instead.

+1
source

Source: https://habr.com/ru/post/892998/


All Articles