With gcc, include strictly C99 symbols, specifical

2019-04-20 09:56发布

问题:

I am taking a course in C after many years programming. My instructor is not using a POSIX machine, and I was recently penalized for using the index function in an assignment. I did some research and I notice that while index was an early part of AT&T Unix and is POSIX-compliant, it is not part of the C99 standard. I'd like gcc to help me find use of similar symbols. I am compiling on OSX with gcc 4.2 (not llvm)

My first leg of research involved noticing that my string.h file wraps the prototype for index in the following check for feature test macros:

#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)

After a little digging, I found this (in my /usr/include/sys/cdefs.h):

 /* STRICT  Defining _POSIX_C_SOURCE or _XOPEN_SOURCE restricts the
 *      available APIs to exactly the set of APIs defined by the
 *      corresponding standard, based on the value defined.
 *
 *      A correct, portable definition for _POSIX_C_SOURCE is 200112L.
 *      A correct, portable definition for _XOPEN_SOURCE is 600L.
 */

And so with this gcc command:

gcc -D _POSIX_C_SOURCE=200112L -pedantic -Wall -std=c99 -o myExec ./mySource.c

I do get a decent warning:

warning: incompatible implicit declaration of built-in function ‘index’

This confuses me, and I want to make the program fail to link as well. The reason it confuses me is, I read at opengroup.org that defining that macro should expose POSIX symbols, not hide them. So here is my question in a succinct form:

How can I make gcc 4.2 (on OSX 10.6) compile and link my program in an environment which is strictly C99 with no additional symbols available?

Ideally, I want warnings during compile and failure to link. I'm hoping that the answer does not involve telling gcc that I am writing POSIX code, when I believe that's the opposite of what I want to tell it. What am I not understanding?

回答1:

  1. You're not going to get a link error, sorry. Libraries don't work that way. The index function is a part of the standard library. The standard library supports multiple versions of C (and POSIX) at the same time, and the macros select which prototypes get exposed.

  2. The -ansi flag is equivalent to -std=c89, so stop doing that.

  3. You are right that _POSIX_C_SOURCE exposes additional functionality, but it also specifies which version of the functionality to expose. Defining _POSIX_C_SOURCE=200112L means that index appears in <strings.h> instead of <string.h>. It's still there. Specifying the 2008 POSIX will get rid of index entirely, since it was removed from later versions of the POSIX standard.

  4. There is a macro which means "only ANSI C", and that is _ANSI_SOURCE.

  5. You can get a compilation error (but not a link error) by adding -Werror-implicit-function-declaration.

$ gcc -Werror-implicit-function-declaration \
    -D_ANSI_SOURCE -pedantic -Wall -std=c99 ...
error: implicit declaration of function 'index'

This seems to work just fine on all combinations OS X 10.5/10.8, GCC 4.0/4.2.



回答2:

To obtain a C99 environment, pass the flags -std=c99 -pedantic to gcc. This should not define any additional feature-test macros.

Note however that this is only omits the declarations, because those invade the C99 programmer-reserved namespace. The actual symbols are still likely to be available - those shouldn't affect the compilation of a correct C99 program because they tend to be "weak" symbols, but they won't force an error if you declare them yourself and use them.



回答3:

For the record: with icc I found it most helpful to do

icc -no-gcc -diag-enable port-win -fabi-version=2 -std=c99 ...

which will skip all gcc specific voodoo in the system header files and warn about POSIXisms that don't exist on windows.

As for throwing an error, I'd just use -Werror.

Unfortunately, gcc can't turn off gnu-specific attribute processing in system header files. And, even more unfortunate, the stated gcc version does not support the -Werror=<diagnostic> syntax to throw errors upon certain warnings only.

So in essence, the combination of compiler and version you state can't really help you with what you want.