c-preprocessor

How do I create my own defined constants based on the “Configuration Manager”?

Deadly 提交于 2019-12-03 22:29:59
When I select the "Debug" configuration, the DEBUG constant is active. When I select the "Release" configuration, the DEBUG constant is inactive. How can I create my own configurations so that they include my own defined constants. Basically, I want it so that if I select the configuration "FOOBAR" that there is a constant FOO and BAR in my project active. I'm basically trying to avoid putting in a bunch of #define FOO in my projects, then commenting/uncommenting them out when I need/don't need them. GôTô According to this article you can define compilation constants in the build tab of your

Why only define a macro if it's not already defined?

依然范特西╮ 提交于 2019-12-03 22:01:18
All across our C code base, I see every macro defined the following way: #ifndef BEEPTRIM_PITCH_RATE_DEGPS #define BEEPTRIM_PITCH_RATE_DEGPS 0.2f #endif #ifndef BEEPTRIM_ROLL_RATE_DEGPS #define BEEPTRIM_ROLL_RATE_DEGPS 0.2f #endif #ifndef FORCETRIMRELEASE_HOLD_TIME_MS #define FORCETRIMRELEASE_HOLD_TIME_MS 1000.0f #endif #ifndef TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS #define TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS 50.0f #endif What is the rationale of doing these define checks instead of just defining the macros? #define BEEPTRIM_PITCH_RATE_DEGPS 0.2f #define BEEPTRIM_ROLL_RATE_DEGPS 0.2f #define

Is there a list of preprocessor defines for various operating systems (and versions)?

流过昼夜 提交于 2019-12-03 17:31:52
e.g. a mapping for Mac OS 10.6.3 aka Snow Leopard => __APPLE__ && __LP64__ ? Windows 7, Windows XP => __WIN32__ Linux => __LINUX__ Here you go: http://predef.sourceforge.net/ 来源: https://stackoverflow.com/questions/2990172/is-there-a-list-of-preprocessor-defines-for-various-operating-systems-and-versi

Why is it not advised to define macros in header files?

邮差的信 提交于 2019-12-03 17:26:11
The Google C++ Style Guide guide advises that macros must not be defined in a .h (header) file. What are the cons of doing it? The preprocessor concatenates all included source files together in order. If you don't undefine a macro, it can apply to any source following where it was first defined. Since headers are often the public API of a library, any macros you define in your headers could end up in someone else's code, doing unexpected things. Since unexpected things are the antithesis of good software, you should either: Not use macros (idiomatic C++ really shouldn't) Define them in a

Multiple preprocessor directives on one line in C++

僤鯓⒐⒋嵵緔 提交于 2019-12-03 16:38:57
问题 A hypothetical question: Is it possible to have a C++ program, which includes preprocessor directives, entirely on one line? Such a line would look like this: #define foo #ifdef foo #define bar #endif What are the semantics of such a line? Further, are there any combinations of directives which are impossible to construct on one line? If this is compiler-specific then both VC++ and GCC answers are welcome. 回答1: A preprocessing directive must be terminated by a newline, so this is actually a

Conditional #include in C

允我心安 提交于 2019-12-03 16:27:01
Is there a way to make a conditional include with the c preprocessor? I have a "library" of tools (Tools.c, Tools.h), shared by different teams. This library depends on a second one, providing XML-capabilities. Lets call that one XML.h Now one team uses a plain version of the second library (XML.h), while another team uses an extended version (XMLEx.h) in their project. The second team doesn't want to include the XML.h because they already have XMLEx.h included, providing all functions of XML.h. Is there a mechanism to implement something like: #ifdef XML_EX #include "XMLEx.h" #else #include

Accessing the value of a Preprocessor Macro definition

天涯浪子 提交于 2019-12-03 16:18:13
问题 If I add a macro "FOO=bar" under GCC_PREPROCESSOR_DEFINITIONS (or Preprocessor Macros if you use XCode"), what would be the best way to access the value of "FOO"? Currently, I use the clumsy: #define MACRO_NAME(f) #f #define MACRO_VALUE(f) MACRO_NAME(f) #ifdef FOO NSLog(@"%s", MACRO_VALUE(FOO)); #else NSLog(@"undefined"); #endif This will output "bar" Surely, there must be a better/cleaner way? 回答1: What you are doing is the way to stringize (or stringify ) macro values. The indirection is

C find static array size (preventing mistakes) [duplicate]

随声附和 提交于 2019-12-03 16:13:26
问题 This question already has answers here : Array-size macro that rejects pointers (9 answers) Closed last year . Finding the size of a static array is a common operation. see: C find static array size - sizeof(a) / sizeof((a)[0]) This can be wrapped into a macro, eg: #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) however its possible to accidentally pass in a regular pointer. eg: void func(SomeArray **foo) { int i = ARRAY_SIZE(foo); } While its valid C, but often ends up being a logical

“with” macro in C

故事扮演 提交于 2019-12-03 15:35:31
I was looking for a macro that will resemble the with-construct. The usage should be something like: with (lock(&x), unlock(&x)) { ... } It might be useful for some other purposes. I came up with this macro: #define __with(_onenter, _onexit, v) \ for (int __with_uniq##v=1; __with_uniq##v > 0; )\ for (_onenter; __with_uniq##v > 0; _onexit) \ while (__with_uniq##v-- > 0) #define _with(x, y, z) __with(x, y, z) #define with(_onenter, _onexit) _with(_onenter, _onexit, __COUNTER__) It has 3 nested loops because it should: Initialize loop counter (C99 only, of course) Possibly initialize variable

How does this C code work?

自古美人都是妖i 提交于 2019-12-03 14:54:51
What is a##b & #a ? #define f(a,b) a##b #define g(a) #a #define h(a) g(a) main() { printf("%s\n",h(f(1,2))); //how should I interpret this?? [line 1] printf("%s\n",g(f(1,2))); //and this? [line 2] } How does this program work? The output is 12 f(1, 2) now I understand how a##b & #a work. But why is the result different in the two cases (line 1 and line 2)? The ## concatenates two tokens together. It can only be used in the preprocessor. f(1,2) becomes 1 ## 2 becomes 12 . The # operator by itself stringifies tokens: #a becomes "a" . Therefore, g(f(1,2)) becomes "f(1,2)" when the preprocessor is