简体   繁体   中英

restrict qualifier compilation error

I am using Code::Blocks 10.05, and mingw. It seems the compiler does not recognized restrict qualifier and return "error: expected ';', ',' or ')' before 'src'". Do I need to pass any compiler option in order to compile it correctly?

int inet_pton4 (const char *restrict src, unsigned char *restrict dst)

p/s: it seems mingw does not support inet_pton4, so i tried to integrate an open-source version into my code.

If your compiler does not support the restrict keyword, just take that keyword out (a) .

It's used to indicate to the compiler that you (the developer) promise that the pointers follow certain properties involving aliasing, and this, in turn, allows the compiler to perform certain optimisations that would otherwise not necessarily be safe.

If you leave off that keyword in a compiler that supports it, it prevents those optimisations (slight downside).

If you leave it off for compilers that don't support that keyword, the downside is nil (since they don't support those optimisations anyway) and the upside is considerable, as in "it will compile for you" :-)


(a) You may want to ensure you're compiling in C99 mode first. While it may be true that you're using an older gcc that doesn't understand restrict , it's equally possible that you're not compiling in C99 mode, such as with -std=c99 ( gcc docs seem to indicate that restrict has been supported even back to version 3.0).

If, for some reason you cannot activate C99 mode, I think gcc has an extension that allows you to use __restrict .

Since restrict is new in C99, and since, as @paxdiablo points out, omitting the restrict keyword doesn't really hurt anything, you can do this:

#if __STDC_VERSION__ < 199901L
#define restrict /* nothing */
#endif

Put this in a header that's #include d by everything in your project (or at least by everything that uses restrict ).

This should let your code compile with any C compiler, whether it supports C99 or not. It should even work for a compiler that doesn't define __STDC_VERSION__ .

But , since you're using MinGW, which uses gcc, using gcc --std=c99 should also solve the problem (as @paxdiablo also points out).

You can safely do both. (And the #if solution is likely to be useful to others.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM