c-preprocessor

What type of content is allowed to be used as arguments for C preprocessor macro?

五迷三道 提交于 2020-01-24 17:26:07
问题 Honestly I know the syntax of the C programming language well, but know almost nothing about the syntax of the C preprocessor, although I use it in my programming practice sometimes. So the question. Suppose we have a simple macro that expands to nothing: #define macro(param) What is the restrictions for the syntax that can be put inside macro invoking construction? It is certainly impossible to use single or multiple comma when invoking the macro: macro(,); // won't compile However if we put

How to make a preprocessor macro greedy?

回眸只為那壹抹淺笑 提交于 2020-01-24 05:47:10
问题 We have the following preprocessor macro. Its used to help with Doxygen documentation because Doxygen has troubles with C++ and some template typedefs: #if defined(DOXYGEN_PROCESSING) # define DOCUMENTED_TYPEDEF(x, y) class y : public x {}; #else # define DOCUMENTED_TYPEDEF(x, y) typedef x y; #endif It works great when X is a non-template or has only one template parameter. However, if X is a template with multiple parameters: DOCUMENTED_TYPEDEF(Foo<R,S>,Bar); Then it results in compile

Variadic macros with zero arguments, and commas

房东的猫 提交于 2020-01-24 02:21:07
问题 Consider this macro: #define MAKE_TEMPLATE(...) template <typename T, __VA_ARGS__ > When used with zero arguments it produces bad code since the compiler expects an identifier after the comma. Actually, VC's preprocessor is smart enough to remove the comma, but GCC's isn't. Since macros can't be overloaded, it seems like it takes a separate macro for this special case to get it right, as in: #define MAKE_TEMPLATE_Z() template <typename T> Is there any way to make it work without introducing

multipass a source code to cpp

◇◆丶佛笑我妖孽 提交于 2020-01-24 00:23:11
问题 basically, i'm trying to pass source code twice to gcc's cpp, then directly to gcc's c compiler. as to why, i believe there are just things that can never be made inline within a macro. here's an example: /* say i want to make an inline definition that can be inserted to any other macros */ #include <stdio.h> #define DEF_X #define X 22 int main(void) { DEF_X printf("%u", X); return 1; } then simply compile: gcc $MY_DIR/test_multi_pass.c -o $MY_DIR/test.exe of course, this won't work as when

multipass a source code to cpp

南笙酒味 提交于 2020-01-24 00:22:59
问题 basically, i'm trying to pass source code twice to gcc's cpp, then directly to gcc's c compiler. as to why, i believe there are just things that can never be made inline within a macro. here's an example: /* say i want to make an inline definition that can be inserted to any other macros */ #include <stdio.h> #define DEF_X #define X 22 int main(void) { DEF_X printf("%u", X); return 1; } then simply compile: gcc $MY_DIR/test_multi_pass.c -o $MY_DIR/test.exe of course, this won't work as when

A macros in the C language (#define)

我怕爱的太早我们不能终老 提交于 2020-01-23 09:15:51
问题 I am reading source code of hoard memory allocator, and in the file of gnuwrapper.cpp, there is the following code #define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x) What's the meaning of CUSTOM_PREFIX(malloc)(x) ? is CUSTOM_PREFIX a function? But as a function it didn't defined anywhere. If it's variable, then how can we use variable like var(malloc)(x) ? More code: #ifndef __GNUC__ #error "This file requires the GNU compiler." #endif #include <string.h> #include <stdlib.h> #include <stdio.h>

A macros in the C language (#define)

只谈情不闲聊 提交于 2020-01-23 09:14:04
问题 I am reading source code of hoard memory allocator, and in the file of gnuwrapper.cpp, there is the following code #define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x) What's the meaning of CUSTOM_PREFIX(malloc)(x) ? is CUSTOM_PREFIX a function? But as a function it didn't defined anywhere. If it's variable, then how can we use variable like var(malloc)(x) ? More code: #ifndef __GNUC__ #error "This file requires the GNU compiler." #endif #include <string.h> #include <stdlib.h> #include <stdio.h>

How can I get a list of all preprocessor symbols used (or defined) within a file?

天大地大妈咪最大 提交于 2020-01-23 08:17:27
问题 I have a number of C /C++ project files. I'd like to know the full list of preprocessor symbols used by the files. Is there a flag to gcc, or is there some tool I can use to get this list. Optionally, if the tool also told me the list of symbols defined by the file, that would be great. 回答1: Use gcc -E -dM <file_list> - preprocess, then output #defines. My gcc is a tad rusty, so I'm not sure whether or not you explicitly need the -E, but try both? For further reference, see this 来源: https:/

How can I get a list of all preprocessor symbols used (or defined) within a file?

ⅰ亾dé卋堺 提交于 2020-01-23 08:16:13
问题 I have a number of C /C++ project files. I'd like to know the full list of preprocessor symbols used by the files. Is there a flag to gcc, or is there some tool I can use to get this list. Optionally, if the tool also told me the list of symbols defined by the file, that would be great. 回答1: Use gcc -E -dM <file_list> - preprocess, then output #defines. My gcc is a tad rusty, so I'm not sure whether or not you explicitly need the -E, but try both? For further reference, see this 来源: https:/

How to expand macro and delete comma

倖福魔咒の 提交于 2020-01-23 02:04:20
问题 For example I want to write my own printf() alternative, but I have to perform calculations on the variable arguments: #define log(fmt_string, ...) my_log(fmt_string, pack_args(__VA_ARGS__), __VA_ARGS__) where pack_args(...) - is a macro too. How should I change this code to handle the only fmt_string presence scenario? log("Some message here"); 回答1: In P99 I have two macros #define P00_ARG( \ _1, _2, _3, _4, _5, _6, _7, _8, \ _9, _10, _11, _12, _13, _14, _15, _16, \ ... etc ... \ _153, _154,