使用gcc时,严格包含C99符号,特别是不包括POSIX [英] With gcc, include strictly C99 symbols, specifically excluding POSIX

查看:271
本文介绍了使用gcc时,严格包含C99符号,特别是不包括POSIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过多年编程,我正在参加C课程。我的导师没有使用POSIX机器,而且最近因为在作业中使用索引函数而受到惩罚。我做了一些研究,我注意到尽管 index 是AT& T Unix的早期部分,并且符合POSIX标准,但它不是C99标准的一部分。 我希望gcc帮助我找到类似的符号。我正在使用gcc 4.2编译OSX( llvm)



我的第一次研究涉及注意到我的string.h文件包装原型对于 index ,请参阅以下检查功能测试宏:

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

经过一点挖掘,我发现了这个(在 /usr/include/sys/cdefs.h ):

  / * STRICT定义_POSIX_C_SOURCE或_XOPEN_SOURCE根据定义的值,将
*可用的API限制为由
*相应标准定义的一组API。
*
* _POSIX_C_SOURCE的正确可移植定义是200112L。
* _XOPEN_SOURCE的正确便携式定义是600L。
* /

对于这个gcc命令:

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

I do 会得到一个体面的警告:

  warning:内置函数'index'的隐式声明不兼容

我想让程序无法链接。它令我困惑的原因是,我阅读了在opengroup.org 定义该宏应暴露 POSIX符号,而不是隐藏它们。所以这里是我的问题简洁的形式:

如何使gcc 4.2(在OSX 10.6上)编译链接我的程序的环境是严格 C99与没有附加符号可用



理想情况下,我希望在编译期间出现警告并未能链接。我希望答案不涉及告诉gcc我正在编写POSIX代码,当我相信这与我想告诉它的情况相反时。我不理解什么?

你不会得到一个链接错误,抱歉。图书馆不这样工作。 索引函数标准库的一部分。标准库同时支持C(和POSIX)的多个版本,并且宏选择哪些原型被暴露。

-ansi 标志相当于 -std = c89 ,所以停止这样做。 li>

_POSIX_C_SOURCE 暴露了额外的功能,但它也指定了要公开的功能的哪个版本。定义 _POSIX_C_SOURCE = 200112L 表示 index 出现在< strings.h> 而不是< string.h> 。它仍然在那里。指定2008 POSIX将完全清除 index ,因为它已从POSIX标准的后续版本中删除。


  • There 是一个宏,意思是只有ANSI C,并且它是 _ANSI_SOURCE


  • 通过添加 -Werror-implicit-function-declaration ,可以获得编译错误(但不是链接错误)。




  •  
    $ gcc -Werror-implicit-function-declaration \
    -D_ANSI_SOURCE -pedantic -Wall -std = c99 ...
    错误:函数'index'的隐式声明

    这似乎工作得很好所有组合OS X 10.5 / 10.8,GCC 4.0 / 4.2。

    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. 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.

    这篇关于使用gcc时,严格包含C99符号,特别是不包括POSIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆