c-preprocessor

How to get rid of ifdef's in a large c project

梦想的初衷 提交于 2019-12-07 05:53:43
问题 I got my hands on a opensource project coded in C . It uses #ifdef's for cross-compiling. There are a lot of ifdef's all over the source code. I want just to modify it for one platform. I was thinking to run it through compiler's pre-processor (Visual C++) but it will write the preprocessed result to a single file, which I don't need. Anybody knows a way to pre-process a project leaving it's structure intact (all files intact)? No grep, please. edit: I found a potential solution (it's amazing

What is the # for when formatting using %s

淺唱寂寞╮ 提交于 2019-12-07 05:30:10
问题 I came across this example of an assertion and was wondering what the # is for: #define ASSERT( x ) if ( !( x ) ) { \ int *p = NULL; \ DBGPRINTF("Assert failed: [%s]\r\n Halting.", #x); \ *p=1; \ } 回答1: It is the "stringize" preprocessing operator. It takes the tokens passed as the argument to the macro parameter x and turns them into a string literal. #define ASSERT(x) #x ASSERT(a b c d) // is replaced by "a b c d" 回答2: #x is the stringification directive #define Stringify(x) #x means

List of all Objective-C preprocessor directives in Clang

半世苍凉 提交于 2019-12-07 05:13:34
问题 Is there some list of all Objective-C preprocessor directives in Clang? I'm talking about #pragma mark - Section I or #pragma unused (variableName) or #warning - message . I would like to know more of them to improve my code, but Googling "llvm preprocessor directives" gave me this link: http://clang.llvm.org/docs/UsersManual.html, which does not list all the directives. 回答1: Most of them have not been introduced in LLVM/Clang, but were inherited from GCC. Therefore, searching for gcc

#elif defined without parentheses

别等时光非礼了梦想. 提交于 2019-12-07 04:19:04
问题 Using VS2005 with BLAH_BLAH defined the following preprocessor conditional is false: #elif defined BLAH_BLAH but if I change it to #elif defined(BLAH_BLAH) it is true. Why do the parentheses make a difference here? 回答1: It shouldn't make any difference, unless BLAH_BLAH is defined as something funny. 回答2: The header file was created with a linux editor and added to the project with "Add Existing", after creating the file in the VS editor it evaluates to true. Must be a LF/CR issue? 来源: https:

C++ `ifdef` with concatenation of macros values

淺唱寂寞╮ 提交于 2019-12-07 04:17:59
问题 Can I achieve something similar to following code: #define MODULE base #if defined (MODULE ## _dll) <-- this should do `#ifdef base_dll` ... #else ... #endif second line is obviously wrong. Can I do this somehow? Thanks 回答1: I don't think it is possible to check the definition of token-pasted macro like that (at least I don't know the way) but you can do this: #define JOIN_INTERNAL(a,b) a ## b #define JOIN(a,b) JOIN_INTERNAL(a,b) // switch 1/0 #define base_dll 1 #define MODULE base #if JOIN

Will using a preprocessor directive to define what a dollar sign represents cause any conflicts?

风格不统一 提交于 2019-12-07 03:30:41
问题 Can I use the following in C++ ?: #define $ cout int main(){ $<<"Hello World!\n"; return 0; } I'm wondering whether it will cause any conflicts. 回答1: It's not definitively legal , but your implementation is allowed to accept it. Consider: [C++11: 2.5/1]: Each preprocessing token that is converted to a token (2.7) shall have the lexical form of a keyword, an identifier, a literal, an operator, or a punctuator. Here, your $ is obviously not a keyword, operator or punctuator (as these are

What use cases necessitate #define without a token-string?

只谈情不闲聊 提交于 2019-12-07 03:13:29
问题 I have encountered the #define pre-processor directive before while learning C, and then also encountered it in some code I read. But apart from using it to definite substitutions for constants and to define macros, I've not really understook the special case where it is used without a "body" or token-string. Take for example this line: #define OCSTR(X) Just like that! What could be the use of this or better, when is this use of #define necessary? 回答1: This is used in two cases. The first and

Function like Macros in C

南笙酒味 提交于 2019-12-07 02:32:37
问题 I am trying to understand the idea of function like Macros however there are a few points that befuddle me. For example say we have: #define Max(a,b) ((a)>(b)) ? (a):(b)) and I call it like such int i = Max(4,5); This will evaluate a conditional expression equivalent to a>b ? If yes then a, else b. But I'm confused as to how the Max function knows what to do with the arguments. Unlike an actual function, the implementation isn't written in code in the calling program. is the statement to the

How to get unique values at preprocessing across files

余生颓废 提交于 2019-12-07 02:10:44
问题 PROBLEM I need a way to generate unique values using a preprocessor directive. The aim is that each time the macro is called, it will have a unique integral identifier. But it should retain it's value across files. Kind of like a preprocessor counter for the number of times a call is made to the function. FURTHER INFO The macro I am using is: #define LOG_MSG(a) log_msg(?) 'a' is a string that the user wants to print. log_msg is a custom function used to print a message on the UART The '?' if

Partially preprocess a C or C++ source file?

旧时模样 提交于 2019-12-07 01:28:02
问题 Is there a way to partially pre-process a C or C++ source file? By "partially preprocess" I mean expanding some but not all of the #include directives. For example, I would like to expand #includes pointing to my project headers, but not #includes pointing to other libraries' headers. I tried to do this by running gcc -E with only the -I flags for my project headers and not the -I flags for the libraries, but that doesn't work because gcc gives an error when it encounters an #include it