c++cgccc-preprocessorpreprocessor

GCC preprocessor macro and "#pragma GCC unroll"


Is there another mechanism to get the GCC preprocessor to do this:

#define LIMIT  16
#pragma GCC unroll LIMIT
for (size_t ii = 0; ii < LIMIT; ++ii) {
  ...

That code hits an error:

/path/to/my/file.c:100:20 error: 'LIMIT' undeclared (first use in this function)
  100 | #pragma GCC unroll LIMIT
      |                    ^~~~~

gcc documentation says that this wants an integer constant expression specifying the unrolling factor. I believe that my macro is an "integer constant expression", but...

My compiler is: riscv64-unknown-elf-gcc (g2ee5e430018) 12.2.0.


Solution

  • const and constexpr (in case you can use C++) seems to work (tested with gcc 13.2 on https://godbolt.org/).

    const int x = 5;
    #pragma GCC unroll x
    for (std::size_t ii = 0; ii < x; ++ii) {