c-preprocessor

Using C Preprocessor to Determine Compilation Environment

五迷三道 提交于 2019-12-10 23:29:31
问题 I am building an application that consists of both a windows driver written in C and a user mode executable in C++. They both use a shared header file to define several macros, constants, enums, etc. In the C++ version, I want to include everything within a namespace, which a feature not supported by the C compiler. Is there certain variable I can check for to use as a preprocessor directive with Visual Studio like in the following example? #ifdef USING_CPLUSPLUS namespace mynamespace{ #endif

Macro perform integer arithmetic

徘徊边缘 提交于 2019-12-10 23:12:57
问题 Is it possible to perform arithmetic (multiplication for my case) using macros? I understand that writing a macro like: #define mult(a,b) ((a)*(b)) Will substitute the product everywhere. But I have both my parameters as constants. I know the compiler will also perform the multiplication statically, but the issue is that I have to convert the result to a string using #. Any idea how it can be done? Also if it can be done with C++ macros, I am still okay. I can write those particular modules

Is there any standard requirement for C++ #include lookup?

a 夏天 提交于 2019-12-10 23:09:49
问题 Recently I learned that gnu cpp and msvc cl treat #include files differently. The common behavior is to look inside the directory of next to the includ_ing_ file, and then iterate over the include path (which is obviously set using -I or /I compiler arguments.) But now I wonder: did I learn correctly, or is there actually a standard for the preprocessor, too? 回答1: According to the standard, both #include <...> and #include "..." shall search an implementation defined set of places. There is

Preprocessor Stringizing Operator with String Literal Prefixes

孤人 提交于 2019-12-10 21:49:45
问题 So I want to do the traditional thing to do with the stringizing operator in a macro: #define FOO(x) foo(#x, (x)) However I need to use a string literal prefix: http://en.cppreference.com/w/cpp/language/string_literal Which is a problem because if I need a UTF-32 string literal I try to do this: #define FOO(x) foo(U#x, (x)) But gcc 4.9.2 complains: error: 'U' was not declared in this scope Is there a way to make the compiler treat the U as a prefix to the stringized macro variable? 回答1: Yes,

Get subsets of macro values using offset

老子叫甜甜 提交于 2019-12-10 21:05:33
问题 I have a list defined as a preprocessor value #define LIST 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 . I want to write a macro that gets an index 0 or 1 and evaluates to a subset of the LIST such that for index 0 it will evaluate to 0, 2, 4, 6, 8 and for index 1 it will evaluate to 1, 3, 5, 7, 9 . It is guaranteed that LIST 's length is even but I don't know the content in advance (it is auto generated by the users of the library I supply). This question is a follow up on this question #define LIST 0, 1,

Create dispatch table registering functions across multiple source files in C

断了今生、忘了曾经 提交于 2019-12-10 19:55:14
问题 How can I implement a dynamic dispatch table in C It's essentially the same question as the linked issue, so ... As your Strategy.c obviously already knows about the strategy instances by name ( #include "XYstrategy.h" ) you could go the whole mile and use the header files instead of the implementation files to communicate your strategy to the central dispatcher: This is contrary to the clear intent in the question. This was an example of how he could do it statically, but wanted to have

Print numeric value of a define that's based on other macros via pragma message?

大憨熊 提交于 2019-12-10 19:46:42
问题 This is similar to How do I show the value of a #define at compile-time?. Chris Barry's answer is not working for me: #ifdef __GNUC__ #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) #endif ... #define XSTR(x) STR(x) #define STR(x) #x #pragma message "The value of GCC_VERSION: " XSTR(GCC_VERSION) Results in: $ rm -f dlltest.o && make dlltest.o g++ -DNDEBUG -g2 -O2 -march=native -pipe -c dlltest.cpp dlltest.cpp:13:80: note: #pragma message: The value of GCC

How to distinguish version of compiler using preprocessor directive

北战南征 提交于 2019-12-10 19:43:26
问题 Is there a way to distinguish which version of compiler using preprocessor directives? I'm having trouble with the new Roslyn compiler, which generates different code than the old compiler and I need to keep the code compatible for the team, which is using both Visual Studio 2013 and 2015. 来源: https://stackoverflow.com/questions/32987410/how-to-distinguish-version-of-compiler-using-preprocessor-directive

'Freezing' an expression

拈花ヽ惹草 提交于 2019-12-10 19:07:49
问题 I have a C++ expression that I wish to 'freeze'. By this, I mean I have syntax like the following: take x*x with x in container ... where the ... indicates further (non-useful to this problem) syntax. However, if I attempt to compile this, no matter what preprocessor translations I've used to make 'take' an 'operator' (in inverted commas because it's technically not an operator, but the translation phase turns it into a class with, say, operator* available to it), the compiler still attempts

Macro evaluation in c preprocessor

a 夏天 提交于 2019-12-10 18:49:48
问题 I'd like to do something like this: #define NUM_ARGS() 2 #define MYMACRO0(...) "no args" #define MYMACRO1(...) "one arg" #define MYMACRO2(...) "two args" #define MYMACRO(num,...) MYMACRO##num(__VA_ARGS__) #define GENERATE(...) MYMACRO(NUM_ARGS(),__VA_ARGS__) And I expected it to evaluate to "two args". But instead I have MYMACRONUM_ARGS()(1,1) Is there a way to do what I want (using visual c++) ? P.S. Eventually I want to implement logger that dumps all of variables. The next code int myInt =