c-preprocessor

How to concatenate, evaluate and stringify macros?

偶尔善良 提交于 2019-12-13 01:47:52
问题 I am trying to stringify the substitution (evaluation) of a macro concatenation. For example: #include <stdio.h> #define FOO_ONE 12 #define FOO_TWO 34 #define BAR_ONE 56 #define BAR_TWO 78 #define MAKE_MAC(mac) // ... what to do here? void main(int argc, char *argv[]) { printf("FOO: " MAKE_MAC(FOO) "\n"); printf("BAR: " MAKE_MAC(BAR) "\n"); } The result I am seeking is: FOO: 1234 BAR: 5678 I tried a few forms, I think the best attempt is this: #define STRINGIFY(mac) #mac #define CONCAT(mac1,

Convert simple C #define's into Rust constants

拥有回忆 提交于 2019-12-13 01:05:41
问题 I have a proprietary library with a long story and 30KLoC header. I'd like to generate bindings for Rust to it. And I've used bindgen crate almost successfully. Except bindgen can't transform macro constants into normal constants. Because those constants are defined in a fuzzy way, like #define CONSTANT ((const_type)SOME_OTHER_CONSTANT) So, is there some way to translate such half-constants into normal Rust ones: const Type name = value; UPDATE Apparently, crate bindgen uses clang as its

Detecting null parameter in preprocessor macro

☆樱花仙子☆ 提交于 2019-12-12 17:44:06
问题 I have the following macro function in vanilla C : #define GLOG(format_string, ...) { \ const char *file = strrchr(__FILE__, '/'); \ char format[256] = "%s:%s!%d\t"; \ strncat(format, format_string, 248); \ strcat(format, "\n"); \ printf(format, __FUNCTION__, file ? file : __FILE__, __LINE__, ##__VA_ARGS__); \ } which lets me print a debug message containing the current function, file and line number, e.g. GLOG("count=%d", count); might print do_count:counter.c!123 count=456 How can I modify

C Preprocessor concatenation with variable [duplicate]

余生颓废 提交于 2019-12-12 17:11:38
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: C preprocessor and concatenation Is it possible to concatenate a C preprocessor with a variable name? #define WIDTH 32 int dataWIDTH; // dataWIDTH should be interpreted as 'data32' printf("%d",dataWIDTH); 回答1: Your use case requires a double-unescaping; using the token pasting (##) operator by itself will just append the name of the preprocessor directive. #define WIDTH 32 #define _MAKEDATA(n) data##n #define

Can the C preprocessor perform simple string manipulation?

筅森魡賤 提交于 2019-12-12 15:26:44
问题 This is C macro weirdness question. Is it possible to write a macro that takes string constant X ("...") as argument and evaluates to sting Y of same length such that each character of Y is [constant] arithmetic expression of corresponding character of X. This is not possible, right ? 回答1: No, the C preprocessor considers string literals to be a single token and therefore it cannot perform any such manipulation. What you are asking for should be done in actual C code. If you are worried about

Objective C - How to write a macro

不羁的心 提交于 2019-12-12 13:12:12
问题 I need to write a multi-line macro. I need this because I must have partial code that has been "paste" into specific points in my code. How can I do this? 回答1: It's not really clear from your question what you actually want, but a plain multi-line macro is defined as follows: #define FOO something; \ something_else; Of course, beware of the if .. else problem. If this is applicable to your case, you could wrap things into a do { ... } while(0) construct. This will ensure that it's treated as

__FUNCTION__ and friends act weird in Xcode

半腔热情 提交于 2019-12-12 13:03:32
问题 This works printf("%s body\n",__PRETTY_FUNCTION__); But this does not (Error Expected ')' ): printf(__PRETTY_FUNCTION__" body\n"); I can't get the IDE to show me what __PRETTY_FUNCTION__ evaluates to to determine why it does not work. 回答1: __PRETTY_FUNCTION__ is not a macro. It behaves like a static variable created on the fly scoped in that function. The last paragraph in the link above reads: These identifiers are not preprocessor macros. In GCC 3.3 and earlier, in C only, __FUNCTION__ and

Are C preprocessor statements a part of the C language?

北战南征 提交于 2019-12-12 11:54:43
问题 I recall a claim made by one of my professors in an introductory C course. He stated that the #define preprocessor command enables a programmer to create a constant for use in later code, and that the command was a part of the C language . /* Is this truly C code? */ #define FOO 42 Since this was in an introductory programming class, I suspect that he was merely simplifying the relationship between the source file and the compiler, but nevertheless I wish to verify my understanding. Are

How to display a defined value

一曲冷凌霜 提交于 2019-12-12 11:38:44
问题 In some doxygen documentation I'd like to display the content of a #define , not the tag itself. For instance, in a C file I have #define REPEAT_N_TIMES 10 Now in my documentation I want to display: The action is done 10 times. If I use \ref REPEAT_N_TIMES , it displays: The action is done REPEAT_N_TIMES times Is there a way to display the content of a link, not the link itself, for example like \ValueOf(\ref REPEAT_N_TIMES) or \contentOf(\ref REPEAT_N_TIMES) ? Update: My Doxygen's config is:

C++ throwing compilation error on sizeof() comparison in preprocessor #if

筅森魡賤 提交于 2019-12-12 11:32:49
问题 I have this which does not compile with the error "fatal error C1017: invalid integer constant expression" from visual studio. How would I do this? template <class B> A *Create() { #if sizeof(B) > sizeof(A) #error sizeof(B) > sizeof(A)! #endif ... } 回答1: The preprocessor does not understand sizeof() (or data types, or identifiers, or templates, or class definitions, and it would need to understand all of those things to implement sizeof). What you're looking for is a static assertion