When should I quote CMake variable references?

Two principles of CMake you have to keep in mind: CMake is a script language and arguments are evaluated after the variables are expanded CMake differentiates between normal strings and list variables (strings with semicolon delimiters) Examples set(_my_text “A B C”) with message(“${_my_text}”) would give A B C set(_my_list A B C) with message(“${_my_list}”) would … Read more

How to view C preprocessor output?

gcc -E file.c or g++ -E file.cpp will do this for you. The -E switch forces the compiler to stop after the preprocessing phase, spitting all it’s got at the moment to standard output. Note: Surely you must have some #include directives. The included files get preprocessed, too, so you might get lots of output. … Read more

How can I define a string literal on the GCC command line?

Two options. First, escape the quotation marks so the shell doesn’t eat them: gcc -Dname=\”Mary\” Or, if you really want -Dname=Mary, you can stringize it, though it’s a bit hacky. #include <stdio.h> #define STRINGIZE(x) #x #define STRINGIZE_VALUE_OF(x) STRINGIZE(x) int main(int argc, char *argv[]) { printf(“%s”, STRINGIZE_VALUE_OF(name)); } Note that STRINGIZE_VALUE_OF will happily evaluate down to … Read more

What is ‘:-!!’ in C?

This is, in effect, a way to check whether the expression e can be evaluated to be 0, and if not, to fail the build. The macro is somewhat misnamed; it should be something more like BUILD_BUG_OR_ZERO, rather than …ON_ZERO. (There have been occasional discussions about whether this is a confusing name.) You should read … Read more

Python-like C++ decorators

std::function provides most of the building blocks for my proposed solution. Here is my proposed solution. #include <iostream> #include <functional> //——————————- // BEGIN decorator implementation //——————————- template <class> struct Decorator; template <class R, class… Args> struct Decorator<R(Args …)> { Decorator(std::function<R(Args …)> f) : f_(f) {} R operator()(Args … args) { std::cout << “Calling the decorated … Read more

When was the NULL macro not 0?

The C FAQ has some examples of historical machines with non-0 NULL representations. From The C FAQ List, question 5.17: Q: Seriously, have any actual machines really used nonzero null pointers, or different representations for pointers to different types? A: The Prime 50 series used segment 07777, offset 0 for the null pointer, at least … Read more

What’s the difference between #if and #ifdef Objective-C preprocessor macro?

/#if works as usual if: #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { return YES; } #endif return NO; } /#ifdef means “if defined – some value or macros”: #ifdef RKL_APPEND_TO_ICU_FUNCTIONS #define RKL_ICU_FUNCTION_APPEND(x) _RKL_CONCAT(x, RKL_APPEND_TO_ICU_FUNCTIONS) #else // RKL_APPEND_TO_ICU_FUNCTIONS #define RKL_ICU_FUNCTION_APPEND(x) x #endif // RKL_APPEND_TO_ICU_FUNCTIONS or: #ifdef __OBJC__ #import <Foundation/Foundation.h> #endif Use this link for … Read more

Generating documentation in macros

It is possible to capture doc comments in macro invocations. It is not widely-known, but Rust documentation is actually represented as a special kind of attribute on an item. For example: /// Some documentation comment pub fn function() {} // is equivalent to #[doc=”Some documentation comment”] pub fn function() {} And it is possible to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)