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?