开发者

#include inside the main () function

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开发者_Go百科 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.


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


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜