Define a preprocessor macro through CMake

For a long time, CMake had the add_definitions command for this purpose. However, recently the command has been superseded by a more fine grained approach (separate commands for compile definitions, include directories, and compiler options). An example using the new add_compile_definitions: add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION}) add_compile_definitions(WITH_OPENCV2) Or: add_compile_definitions(OPENCV_VERSION=${OpenCV_VERSION} WITH_OPENCV2) The good part about this is that it circumvents … Read more

What do the numbers mean in the preprocessed .i files when compiling C with gcc?

The numbers following the filename are flags: 1: This indicates the start of a new file. 2: This indicates returning to a file (after having included another file). 3: This indicates that the following text comes from a system header file, so certain warnings should be suppressed. 4: This indicates that the following text should … Read more

Detecting Endianness

As stated earlier, the only “real” way to detect Big Endian is to use runtime tests. However, sometimes, a macro might be preferred. Unfortunately, I’ve not found a single “test” to detect this situation, rather a collection of them. For example, GCC recommends : __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ . However, this only works with latest versions, … Read more

Is it possible for C preprocessor macros to contain preprocessor directives?

The Boost Preprocessor (which works for C as well as C++, even though Boost as a whole is a C++ library) library can help with this kind of task. Instead of using an #ifdef within a macro (which isn’t permitted), it helps you include a file multiple times, with different macros defined each time, so … Read more

Is a C++ preprocessor identical to a C preprocessor?

The C++03 preprocessor is (at least intended to be) similar to the C preprocessor before C99. Although the wording and paragraph numbers are slightly different, the only technical differences I’m aware of between the two are that the C++ preprocessor handles digraphs (two-letter alternative tokens) and universal character names, which are not present in C. … Read more