4

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.

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
Madrugada
  • 1,261
  • 8
  • 24
  • 44
  • If you want to solve that elegantly, switch to an object oriented language and use polymorphism to pick between the two at run time. – Blindy May 03 '11 at 18:34
  • unfortunately i cant do that, i am in Cell and i use C – Madrugada May 03 '11 at 18:46
  • 1
    Separate your program into different files and localise the different definitions on a file by file basis. – Paul R May 03 '11 at 18:49
  • I dont know how to do that in Cell, using SPU and PPU.... in PPU i have this line: extern spe_program_handle_t spu; if i put spu1, spu2, would it know when to send to spu1 and when to spu2 ? – Madrugada May 03 '11 at 18:54
  • in fact excellent idea... thank you Paul! – Madrugada May 03 '11 at 18:58

4 Answers4

3

#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.

Jim Blackler
  • 22,946
  • 12
  • 85
  • 101
  • I thought it's a problem with that since it points error to my #include inside the main function, followed by: invalid storage class for function 'CACHE_cache_lock' .... (more functions) .. what can i understand from that? – Madrugada May 03 '11 at 18:47
2

Yes, that's fine (may not be the clearest design decision) but a #include is just like a copy-and-paste of that file into the code right where the #include is.

miked
  • 3,458
  • 1
  • 22
  • 25
1

#define and #include are pre-processor macros: http://en.wikipedia.org/wiki/C_preprocessor

They are converted / inlined before compilation.

To answer your question ... no, you really wouldn't want do do that, at least for the sake of the next guy that has to try and unscramble that mess.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0

You can #include any file in any file. Whether it is then valid depends on the content of the file; specifically whether that content would be valid if it were entered directly as text.

Header files generally contain declarations and constructs that are normally only valid outside of a function definition (or outside any kind of encoding construct) - the clue is in the name header file. Otherwise you may change the scope of the declarations, or more likley render the compilation unit syntactically invalid.

An include file written specially for the purpose may be fine, but not just any arbitrary header file.

General purpose header files should have include guards to prevent multiple declaration, so unless you undefine the guard macro, re-including a header file will have no effect in any case.

One possible solution to your problem is to create separately compiled modules (compilation units) containing wrapper functions to the API you need to call. Each compilation unit can then include the API header file after defining the appropriate configuration macros. You will then have two separate and independent interfaces provided by these wrapper functions.

Clifford
  • 88,407
  • 13
  • 85
  • 165