c-preprocessor

Accessing the value of a Preprocessor Macro definition

安稳与你 提交于 2019-12-03 05:24:43
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? What you are doing is the way to stringize (or stringify ) macro values. The indirection is unavoidable. This is even mentioned in the GCC preprocessor manual section that Rob linked to. NSLog(@"%s",

How does the C preprocessor handle circular dependencies?

时间秒杀一切 提交于 2019-12-03 05:23:33
问题 I want to know how the C preprocessor handles circular dependencies (of #defines). This is my program: #define ONE TWO #define TWO THREE #define THREE ONE int main() { int ONE, TWO, THREE; ONE = 1; TWO = 2; THREE = 3; printf ("ONE, TWO, THREE = %d, %d, %d \n",ONE, TWO, THREE); } Here is the preprocessor output. I'm unable to figure out why the output is as such. I would like to know the various steps a preprocessor takes in this case to give the following output. # 1 "check_macro.c" # 1 "

Class Constants

ぐ巨炮叔叔 提交于 2019-12-03 05:15:56
问题 I have several obj-c classes, each of which require a number of constants that are used in switch statements. I had tried defining these numerical constants in the .m file using the #define preprocessor instruction. All these constants begin with 'kCell'. This seems to work well but Xcode's code sense is presenting me with every 'kCell' prefixed constant no matter where I am in the project. Do #define constants have global scope? If so, what is the best way to define purely local class

How do I show the value of a #define at compile time in gcc

泪湿孤枕 提交于 2019-12-03 04:52:32
问题 So far I've got as far as: #define ADEFINE "23" #pragma message ("ADEFINE" ADEFINE) Which works, but what if ADEFINE isn't a string? #define ADEFINE 23 #pragma message ("ADEFINE" ADEFINE) causes: warning: malformed ‘#pragma message’, ignored Ideally I'd like to be able to deal with any value, including undefined. 回答1: To display macros which aren't strings, stringify the macro: #define STRINGIFY(s) XSTRINGIFY(s) #define XSTRINGIFY(s) #s #define ADEFINE 23 #pragma message ("ADEFINE=" STRINGIFY

Does the C preprocessor remove instances of “&*”?

自古美人都是妖i 提交于 2019-12-03 04:31:21
I was playing around with gcc and tried the following bit of code: int A = 42; int *B = &A; int *C = &*B; And C == &A , as expected. But when I try: int *B = NULL; int *C = &*B; Turns out C == NULL , and no segfault. So &*B is not actually dereferencing B before taking its address. My guess is that the preprocessor is stripping out instances of &* and *& before they even get to the compiler since they negate each other, but I can't find any documentation to verify whether this is standard C or compiler-specific. Is the preprocessor stripping out &* and *& , and can I expect this behavior from

Visual studio __VA_ARGS__ issue

£可爱£侵袭症+ 提交于 2019-12-03 04:00:37
I run cl /P test.cpp, the file and result is as following. test.cpp #define FiltedLog( ...) \ if (logDetail) \ MP_LOG(LOG_INFO, __VA_ARGS__); #define MP_LOG(level,fmt,...) \ BOOAT::LOG("MP", level, fmt, ##__VA_ARGS__) #define LOG(tag,level,fmt,...) \ Log::log(tag, level, "%s: " fmt, __PRETTY_FUNCTION__, ##__VA_ARGS__) int main () { FiltedLog ( "abc", 1, 2); } Cl /P test.cpp : #line 1 "test.cpp" int main () { if (logDetail) BOOAT::Log::log("MP", LOG_INFO, "%s: " "abc", 1, 2, __PRETTY_FUNCTION__ );; } I wonder why the __PRETTY_FUNCTION__ are put as the last arguments in the result. I assume the

What is the purpose of a single pound/hash sign (#) on its own line in the C/C++ preprocessor?

女生的网名这么多〃 提交于 2019-12-03 03:21:52
问题 I have been looking at the Boost libraries source code, and I have noticed that often there are single pound signs without any preprocessor directives attached to them. I read through the GCC preprocessor manual and specification guide and can't find anything about it. (1) #ifndef BOOST_CONFIG_HPP (2) # include <boost/config.hpp> (3) #endif (4) # (5) #if defined(BOOST_HAS_PRAGMA_ONCE) (6) # pragma once (7) #endif On line 4, there is nothing after the pound sign. What effect does this have? Is

Do I need to #undef a local #define? Is there such a thing as a local define?

僤鯓⒐⒋嵵緔 提交于 2019-12-03 03:10:24
Sometimes to make things easier to write and read, I write some local #define macros within functions (for example, #define O_REAL Ogre::Real) . Do I need to #undef the local #define to ensure that it stays within a certain block of code? Or does it automatically #undef when it goes out of scope ? Does it even have a concept of scope ? I am unsure on how #define works in this case. Now, I have of course experimented with the code and reached certain conclusions but since I am unsure, I would like some expert opinion/advice. #define does not respect any C++ scope. There is no such thing as a

Difference between preprocessor directives #if and #ifdef

走远了吗. 提交于 2019-12-03 02:57:10
问题 What is the difference (if any) between the two following preprocessor control statements. #if and #ifdef 回答1: You can demonstrate the difference by doing: #define FOO 0 #if FOO // won't compile this #endif #ifdef FOO // will compile this #endif #if checks for the value of the symbol, while #ifdef checks the existence of the symbol (regardless of its value). 回答2: #ifdef FOO is a shortcut for: #if defined(FOO) #if can also be used for other tests or for more complex preprocessor conditions.

#include directive: relative to where?

血红的双手。 提交于 2019-12-03 02:03:01
I have looked in The C++ Programming Language to try to find the answer to this. When I #include "my_dir/my_header.hpp" in a header, where does it look for this file? Is it relative to the header, relative to the source file that included it, or something else? aib Implementation defined. See what is the difference between #include <filename> and #include “filename” . It is relative to both the current source file and to any search paths given (-I for gcc). It depends on what syntax you use in the #include directive: #include "path-spec" #include <path-spec> Quoted form : This form instructs