Linking libraries with gcc: order of arguments

With gcc but also other compilers (e.g. clang), the order of linker command arguments does matter. As a rule of thumb, I would use the following order when composing the linker command: Object files (*.o) Static libraries (*.a) Shared libraries (*.so) The order of shared libraries does matter as well. If libfoo.so depends on libbar.so, … Read more

Custom gcc preprocessor

Warning: dangerous and ugly hack. Close your eyes now You can hook your own preprocessor by adding the ‘-no-integrated-cpp’ and ‘-B’ switches to the gcc command line. ‘-no-integrated-cpp’ means that gcc does search in the ‘-B’ path for its preprocessors before it uses its internal search path. The invocations of the preprocessor can be identified … Read more

Selectively remove a warning message using GCC

If you need that code to work portable then you should cast the argument to unsigned int, as the int type may have a different size than Int32 on some platforms. To answer your question about disabling specific warnings in GCC, you can enable specific warnings in GCC with -Wxxxx and disable them with -Wno-xxxx. … Read more

How do I force cmake to include “-pthread” option during compilation?

The Threads module in the latest versions (>= 3.1) of CMake generates the Threads::Threads imported target. Linking your target against Threads::Threads adds all the necessary compilation and linking flags. It can be done like this: set(CMAKE_THREAD_PREFER_PTHREAD TRUE) set(THREADS_PREFER_PTHREAD_FLAG TRUE) find_package(Threads REQUIRED) add_executable(test test.cpp) target_link_libraries(test Threads::Threads) Use of the imported target is highly recommended for new … Read more

What does the compiler error “missing binary operator before token” mean?

This is not a compiler error, it is a preprocessor error. It occurs when the preprocessor encounters invalid syntax while trying to evaluate an expression in a #if or #elif directive. One common cause is the sizeof operator in an #if directive: For example: #define NBITS (sizeof(TYPE)*8) //later #if (NBITS>16) //ERROR This is an error … Read more