Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#include inside the main () function

Tags:

c

cell

I would like to know if it's possible that inside the main() function from C to include something.

For instance, in a Cell program i define the parameters for cache-api.h that later in the main() function i want to change .

I understood that what was defined with #define can be undefined with #undef anywhere in the program, but after redefining my needed parameters I have to include cache-api.h again . Is that possible?

How can I solve this problem more elegant ? Supposing I want to read from the main storage with cache_rd(...) but the types would differ during the execution of a SPU, how can i use both #define CACHED_TYPE struct x and #define CACHED_TYPE struct y in the same program?

Thanks in advance for the answer, i hope i am clear in expression.

like image 363
Madrugada Avatar asked Oct 17 '25 08:10

Madrugada


1 Answers

#define and #include are just textual operations that take place during the 'preprocessing' phase of compilation, which is technically an optional phase. So you can mix and match them in all sorts of ways and as long as your preprocessor syntax is correct it will work.

However if you do redefine macros with #undef your code will be hard to follow because the same text could have different meanings in different places in the code.

For custom types typedef is much preferred where possible because you can still benefit from the type checking mechanism of the compiler and it is less error-prone because it is much less likely than #define macros to have unexpected side-effects on surrounding code.

like image 143
Jim Blackler Avatar answered Oct 19 '25 00:10

Jim Blackler