c-preprocessor

C preprocessor Macro defining Macro

删除回忆录丶 提交于 2019-12-18 12:04:56
问题 Can you do something like this with a macro in C ? #define SUPERMACRO(X,Y) #define X Y then SUPERMACRO(A,B) expands to #define A B I have a feeling not because the preprocessor only does one pass. Official gcc only. No third-party tools please. 回答1: Macros can't expand into preprocessing directives. From C99 6.10.3.4/3 "Rescanning and further replacement": The resulting completely macro-replaced preprocessing token sequence is not processed as a preprocessing directive even if it resembles

C/C++ need a clever way to track function calls

老子叫甜甜 提交于 2019-12-18 11:36:04
问题 I am looking for a clever way to track function calls and returns. I know I can use the debugger, but I would like a way to just have it print something out to the terminal when calling a function vs having to step through code. I am thinking that I might be able to use the preprocessor, but I am not sure what would be the best way to go about this. Or is there a way to use gdb to print out the information that would be useful, while not having to step through the code. 回答1: Most compiler's

Why is assert a macro and not a function?

北城余情 提交于 2019-12-18 10:31:34
问题 My lecturer has asked me that in class, and I was wondering why is it a macro instead of a function? 回答1: The simple explanation would be that the standard requires assert to be a macro, if we look at the draft C99 standard( as far as I can tell the sections are the same in draft C11 standard as well ) section 7.2 Diagnostics paragraph 2 says: The assert macro shall be implemented as a macro, not as an actual function. If the macro definition is suppressed in order to access an actual

#define Square(x) (x*(x)) [duplicate]

Deadly 提交于 2019-12-18 07:09:25
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: square of a number being defined using #define Can you please explain why the following code outputs "29"? #define Square(x) (x*(x)) void main() { int x = 5; printf("%d", Square(x+3)); } 回答1: Since macros only do textual replacement you end up with: x + 3 * (x + 3) which is 29. You should absolutely always put macro arguments between parentheses. #define Square(x) ((x)*(x)) Better yet, use a function and trust

Preprocessor concatenation for include path

青春壹個敷衍的年華 提交于 2019-12-18 07:08:48
问题 I have a set of includes that reside in a far off directory meaning that including them requires a long include, such as: #include "../../Path/to/my/file.h" Where I have multiple of these it becomes a bit inconvenient so I am thinking I may be able to use a #define for the directory path and then concat the file name that I need, i.e. #define DIR "../../Path/to/my/" #define FILE1 "file.h" #define FILE2 "anotherFile.h" #include DIR FILE1 // should end up same as line in first example after pre

Is there a way to automatically have a #define reproduced in each source file

故事扮演 提交于 2019-12-18 07:05:10
问题 I'd like the following to appear in every source file in my Visual C++ 2005 solution: #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) #define new DEBUG_NEW Is there a way of doing this without manually copying it in? Compiler option? 回答1: The command line option /D can be used to define preprocessor symbols. I don't know, though, whether it can also be used to define macros with arguments, but it should be an easy matter to test that. Edit: Failing that, the /FI option ("force

How to disable NSLog all over the app?

心已入冬 提交于 2019-12-18 05:47:32
问题 I want to disable NSLog() across all instances in an app. I found some code that does that: #ifndef DEBUG #define NSLog // #endif But adding this code to each file isn't good idea. How can I make it easier? 回答1: Xcode has a precompiled header file ( {project-name}-Prefix.pch in the Supporting Files group by default) that is a great place to put code that will be used across every file in the project. For going a step further and improving the log message itself, see Is it true that one should

No type named 'unique_ptr' in namespace 'std' when compiling under LLVM/Clang

本小妞迷上赌 提交于 2019-12-18 05:18:12
问题 I'm catching a compile error when attempting to use unique_ptr on Apple platforms with -std=c++11 : $ make c++ -std=c++11 -DNDEBUG -g2 -O3 -fPIC -march=native -Wall -Wextra -pipe -c 3way.cpp In file included ... ./smartptr.h:23:27: error: no type named 'unique_ptr' in namespace 'std' using auto_ptr = std::unique_ptr<T>; ~~~~~^ ./smartptr.h:23:37: error: expected ';' after alias declaration using auto_ptr = std::unique_ptr<T>; According to Marshall Clow, who I consider an expert on the C++

static_if in C99's preprocessor

和自甴很熟 提交于 2019-12-18 05:16:28
问题 Is it possible to implement static_if in C99? #define STATIC_IF(COND, ...) \ if (COND) MACRO1(__VA_ARGS__); \ else MACRO2(__VA_ARGS__); How can I properly implement STATIC_IF(…) in here? Depending on COND the arguments either should be passed to MACRO1 or MACRO2 , but the arguments for both macros look differently. COND is statically testable, something like sizeof (…) > 42 . #if COND then #define STATIC_IF MACRO1 … wouldn't work for my use case. I cannot use compiler specific solutions. 回答1:

How to single-quote an argument in a macro?

依然范特西╮ 提交于 2019-12-18 04:42:42
问题 I would like to create a C pre-processor macro that will single-quote the argument. Just like the common used #X . I want Q(A) to be expanded to 'A' . I am using gcc on Linux. Does any one have an idea? I know # double-quotes. I am looking for a similar mechanism for single-quoting. 回答1: The best you can do is #define Q(x) ((#x)[0]) or #define SINGLEQUOTED_A 'A' #define SINGLEQUOTED_B 'B' ... #define SINGLEQUOTED_z 'z' #define Q(x) SINGLEQUOTED_##x This only works for a - z , A - Z , 0 - 9