c-preprocessor

Preprocessor tomfoolery (stringifying a #include)

余生颓废 提交于 2019-12-03 11:21:56
问题 Note: This question has nothing to do with OpenCL per se... check the last paragraph for a succinct statement of my question. But to provide some background: I'm writing some C++ code that makes use of OpenCL. I like to keep the source for my OpenCL kernels in their own files, to keep coding and maintenance easy (as opposed to embedding the sources directly as string constants in associated C++ code). This inevitably leads to the question of how to load them into the OpenCL runtime once it

How can I detect if I'm compiling for a 64bits architecture in C++

喜欢而已 提交于 2019-12-03 11:00:53
问题 In a C++ function I need the compiler to choose a different block if it is compiling for a 64 bit architecture. I know a way to do it for MSVC++ and g++, so I'll post it as an answer. However I would like to know if there is a better way (more elegant that would work for all compilers/all 64 bits architectures). If there is not a better way, what other predefined macros should I look for in order to be compatible with other compiler/architectures? 回答1: Why are you choosing one block over the

Objective C “#if” syntax

牧云@^-^@ 提交于 2019-12-03 10:54:44
I'm a little confused by the "pound if" or #if syntax I see when I look at some classes. For example: #if someConstant == someNumber do something #elif etc versus: if (someConstant == someNumber) do something else if { do more stuff } what's the difference, and why use #if ? #if etc are preprocessor directives. This means that they are dealt with before compiling and not at runtime. This can be useful, for example, in defining debugging behaviour that only compiles when you build for debug and not release : #if DEBUG #define ISRelease(x) [x release] #else #define ISRelease(x) [x release], x =

error: pasting “.” and “red” does not give a valid preprocessing token

旧巷老猫 提交于 2019-12-03 10:22:26
I'm implementing The X macro, but I have a problem with a simple macro expansion. This macro (see below) is used into several macros usage examples, by including in this article. The compiler gives an error message, but I can see valid C code by using -E flag with the GCC compiler. The macro X-list is defined as the following: #define LIST \ X(red, "red") \ X(blue, "blue") \ X(yellow, "yellow") And then: #define X(a, b) foo.##a = -1; LIST; #undef X But the gcc given the following errors messages: lixo.c:42:1: error: pasting "." and "red" does not give a valid preprocessing token lixo.c:42:1:

Variadic UNUSED function/macro

我只是一个虾纸丫 提交于 2019-12-03 10:19:49
问题 A well-known and portable way to suppress C compiler warnings about unused variables is (see unused parameter warnings in C code): #define UNUSED(x) (void)(x) I'm looking for a way to generalize this to take multiple inputs (of different type): void foo(int a, long b, void* c){ /* Want this: */ ALL_UNUSED(a, b, c); /* instead of: */ UNUSED(a); UNUSED(b); UNUSED(c); } One way that seems to do the trick is to use a variadic function static inline void ALL_UNUSED(int dummy, ...) {} However, I

C++ Macros: manipulating a parameter (specific example)

岁酱吖の 提交于 2019-12-03 10:08:53
问题 I need to replace GET("any_name") with String str_any_name = getFunction("any_name"); The hard part is how to trim off the quote marks. Possible? Any ideas? 回答1: How about: #define UNSAFE_GET(X) String str_##X = getFunction(#X); Or, to safe guard against nested macro issues: #define STRINGIFY2(x) #x #define STRINGIFY(x) STRINGIFY2(x) #define PASTE2(a, b) a##b #define PASTE(a, b) PASTE2(a, b) #define SAFE_GET(X) String PASTE(str_, X) = getFunction(STRINGIFY(X)); Usage: SAFE_GET(foo) And this

#if check (preprocessor macro) to differentiate between iPhone and iPad

笑着哭i 提交于 2019-12-03 09:30:19
问题 Is there a build preprocessor macro I can check, with #if or # ifdef to determine if my current Xcode project is being built for iPhone or iPad? EDIT As several answers have pointed out, often apps are universal, and the same binary can run on both devices. Conditional behavior between these very similar devices should be solved at runtime rather than compile time. 回答1: There is no way to determine whether your app is built for iPhone or iPad. Preprocessor #if directives are resolved during

Including a header file from another directory

限于喜欢 提交于 2019-12-03 09:21:36
I have a main directory A with two sub directories B and C . Directory B contains a header file structures.c : #ifndef __STRUCTURES_H #define __STRUCTURES_H typedef struct __stud_ent__ { char name[20]; int roll_num; }stud; #endif Directory C contains main.c code: #include<stdio.h> #include<stdlib.h> #include <structures.h> int main() { stud *value; value = malloc(sizeof(stud)); free (value); printf("working \n"); return 0; } But I get an error: main.c:3:24: error: structures.h: No such file or directory main.c: In function ‘main’: main.c:6: error: ‘stud’ undeclared (first use in this function)

Understanding DEFER and OBSTRUCT macros

本小妞迷上赌 提交于 2019-12-03 08:56:46
问题 I created a small macro metaprogramming library that implements basic useful constructs such as REPEAT(times, x) , IF(value, true, false) , tuples, and more. Most of my implementations work by overloading macros based upon their variadic argument count or through a counter: // Example: #define REPEAT_0(x) #define REPEAT_1(x) x REPEAT_0(x) #define REPEAT_2(x) x REPEAT_1(x) #define REPEAT_3(x) x REPEAT_2(x) // ... // (these defines are generated using an external script) // ... #define REPEAT

How to verify a type in a C macro

匆匆过客 提交于 2019-12-03 08:26:28
问题 I have been thinking about ways to validate types in C macros and so far the best way that I have come up with is this: #define ASSERT_PTYPE(TYPE, VALUE) (0 && (*(int (*)(TYPE*))0)(VALUE)) This obviously expects a type name and a pointer to that type. A similar ASSERT_TYPE macro can be made as well. This seems to work quite well with GCC. It even gives a very helpful error message in the case that the types do not match. The problems are that I am not completely certain that this is valid C