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?
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.
The -ansi
flag is equivalent to -std=c89
, so stop doing that.
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.
There is a macro which means "only ANSI C", and that is _ANSI_SOURCE
.
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.