c-preprocessor

How to trigger C-preprocessor error about undefined symbol at #if? (LLVM/CLang/Xcode)

佐手、 提交于 2020-01-03 21:48:23
问题 How to trigger C-preprocessor error about missing definition at #if? I'm using LLVM/Clang/Xcode. This code works. #define AAAAA 1 #if AAAAA #endif And I expected this code will be an error for undefined symbol. //#define AAAAA 1 Removed definition. #if AAAAA #endif But it did not. Is this standard/regular behavior? And is there other way to triggering preprocessor error? 回答1: If you want to let the compiler check this for you, without rewriting your code, you can use the -Wundef switch as

Skipping function call if not defined

夙愿已清 提交于 2020-01-03 20:46:12
问题 I have a program consisting out of different modules. The modules are interconnected via function calls. E.g. State Init calls the init function of every module. It shall be possible to disable modules (exclude from compilation). The easiest way would be using preprocessor defines. But this generates massive amount of code: #IF MODULE_XXX_COMPILE ret = module_func(...); #ELSE ret = 0; #ENDIF I would like to get a macro like this: ret = MOD_COMPILE_CALL(module_func(...)); So the macro checks

Are Preprocessor Definitions compiled into a library?

痴心易碎 提交于 2020-01-03 15:36:34
问题 Are they (preprocessor definitions) compiled into a static/dynamic library? For example, the FBX SDK needs KFBX_DLLINFO . A library that makes use of FBX SDK must include that. Now the client application, as far as I can tell from my limited experimentation, does not need to declare the definition again. Now I can't think of a more practical scenario, but what if the client application 'needs' the definition to excluded (for example _CRT_SECURE_NO_WARNINGS compiled with a library, but what if

Redefining enum enumerators with #define

巧了我就是萌 提交于 2020-01-03 12:33:13
问题 I have spotted something in C header files what I can't figure out what is for. For example in file bits/socket.h there is an enumeration type enum __socket_type , but after every enumerator there is a define macro which defines the same. Example: enum __socket_type { SOCK_STREAM = 1, #define SOCK_STREAM SOCK_STREAM ... }; I have been unable to find out what this is for. Please enlighten me. I don't even know how to form right question for querying google nor this site search box. 回答1: A

Why so many parentheses in SUCCEEDED macro?

蹲街弑〆低调 提交于 2020-01-03 11:51:11
问题 Windows SDK features SUCCEEDED macro: #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0) -----------------------^-------------^----- clearly as with other macros there're parentheses to ensure right interpretation of the intent by compiler. What I don't get is why there are parentheses around (HRESULT)(hr) (I marked them with ^ character). hr is parenthesized so that some complex construct can be there, HRESULT is parenthesized to form a C-style cast, then the whole >= construct is parenthesized as

How do you perform macro expansion within #ifdef?

喜你入骨 提交于 2020-01-03 11:29:42
问题 I have some fairly generic code which uses preprocessor macros to add a certain prefix onto other macros. This is a much simplified example of what happens: #define MY_VAR(x) prefix_##x "prefix_" is actually defined elsewhere, so it will be different each time the file is included. It works well, but now I have some code I would like to skip if one of the tokens doesn't exist, but this doesn't work: #if defined MY_VAR(hello) What I want it to expand to is this: #ifdef prefix_hello But I can't

How do you perform macro expansion within #ifdef?

白昼怎懂夜的黑 提交于 2020-01-03 11:29:32
问题 I have some fairly generic code which uses preprocessor macros to add a certain prefix onto other macros. This is a much simplified example of what happens: #define MY_VAR(x) prefix_##x "prefix_" is actually defined elsewhere, so it will be different each time the file is included. It works well, but now I have some code I would like to skip if one of the tokens doesn't exist, but this doesn't work: #if defined MY_VAR(hello) What I want it to expand to is this: #ifdef prefix_hello But I can't

#ifdef WIN32 #elif WIN64 #endif

半世苍凉 提交于 2020-01-03 10:58:17
问题 I have come across some example code that goes like this: #ifdef WIN32 ... #elif WIN64 ... #endif In an #ifdef block, is it actually legal to use #elif to mean #elif defined ? 回答1: No, it shouldn't be. That's not to say that some obscure C compiler wouldn't accept it as such, but it isn't part of the C standard. Normally, for something like this you would use either #elifdef FOO (which I've never actually seen in production code) or #elif defined(FOO) (like you mentioned). This code appears

Can #define preprocessor directive contain if and else?

我的未来我决定 提交于 2020-01-03 05:06:07
问题 I was trying the logger code from this link, but it gives me error. How to implement a good debug/logging feature in a project #ifndef _LOGGER_HPP_ #define _LOGGER_HPP_ #include <iostream> #include <sstream> /* consider adding boost thread id since we'll want to know whose writting and * won't want to repeat it for every single call */ /* consider adding policy class to allow users to redirect logging to specific * files via the command line */ enum loglevel_e {logERROR, logWARNING, logINFO,

How to print C-preprocessor variables like __LINE__ with mexErrMsgTxt() In Matlab MEX

ぃ、小莉子 提交于 2020-01-03 00:55:31
问题 For debugging Matlab-MEX, which can be quite a hassle, it would be nice to have better assertion capabilities. Following this question about mex-assertions, it is possible to define a preprocessor makro, that throws an error to Matlab and prints a string (can mostly replace mxAssert , which unfortunately crashes Matlab2011b). #define myassert( isOK,astr ) ( (isOK) ? (void)0 : (void) mexErrMsgTxt(astr) ) It would be much nicer to print the file, line number and caller function, from where