-std=c99 可以阻止我的 #includes 正常工作吗?
我正在尝试在Linux系统上编译C程序。我有一个 stdlib.h
的 #include
语句。
当我使用 gcc
编译程序时,如下所示:
gcc -std=c99 -g -o progfoo progfoo.c progbar.c
我收到有关函数隐式声明 [srand48、drand48、bzero 或 close]
的警告。
编译为:
gcc -g -o progfoo progfoo.c progbar.c
不会给我警告,但它确实会大喊我使用 for
循环(这是在第一个中添加 -std=c99
的基本原理)地方)。
鉴于 man srand48
提到包括我拥有的
,我不确定还有什么问题。 for 循环对任何事情来说都不是必需的(它们只是为了节省初始化数组的时间),因此删除它们没有问题,但在删除它们之前,我想确认一下是否>c99 标准正在取代我的 #include
语句的某些方面。
我正在使用 gcc 4.1.2-50 (Red Hat)
。
I am trying to compile a C program on a Linux system. I have an #include
statement for stdlib.h
.
When I compile the program with gcc
as follows:
gcc -std=c99 -g -o progfoo progfoo.c progbar.c
I get warnings about Implicit declaration of function [srand48, drand48, bzero, or close]
.
Compiling instead as:
gcc -g -o progfoo progfoo.c progbar.c
doesn't give me the warnings, but it does yell about my use of for
loops (which was the rationale for adding -std=c99
in the first place).
Given that man srand48
mentions including <stdlib.h>
, which I have, I'm unsure what else the problem could be. The for
loops aren't essential to anything (they were just to save time in initializing an array) so I have no problem removing them, but before I do I'd like to confirm whether the c99
standard is superseding some aspect of my #include
statements.
I'm using gcc 4.1.2-50 (Red Hat)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,但它们可能会显示出您对它们工作方式的知识的限制:-)
虽然函数
[sd]rand48
在stdlib.h< 中有一个原型/code>,它们位于
#ifdef
内,至少在我的系统上:所以您可能必须显式设置这些宏之一。
但是,在尝试之前,请注意它不起作用。这是因为所有这些东西都是由
gcc
的 功能测试宏。在
features.h
中有一组非常复杂的规则用于打开或关闭特定功能,并且其中创建的宏控制头文件包含和排除的内容。__USE_*
变体将根据您提供的其他宏在该头文件中清除和设置。例如,要设置
__USE_SVID
以便可以使用srand48
,您需要向编译器提供-D_SVID_SOURCE
参数。但也许更简单的方法是仅使用带有 GNU 扩展的 C99。为此,请将
-std=c99
替换为-std=gnu99
。并且,对于
bzero
和close
,可以分别从strings.h
和unistd.h
获取。一开始我有点困惑,为什么这些与 C99 完全没有关系,但用
-std=c99
编译,但后来我意识到标志只控制标准 C 标头为您提供。strings.h
(注意复数名称,这不是string.h
)和unistd.h
都不是ISO C 的一部分。No, but they may show up limitations in your knowledge of how they work :-)
While the functions
[sd]rand48
have a prototype instdlib.h
, they're inside an#ifdef
, at least on my system:So you will probably have to explicitly set one of those macros.
However, before you try it, be aware that it doesn't work. That's because all this stuff is controlled with
gcc
's feature test macros.There's a very complicated set of rules used to set specific features on or off in
features.h
and the macros created there control what the header files include and exclude. The__USE_*
variants are cleared and set in that header file based on other macros provided by yourself.For example, to get
__USE_SVID
set so you can usesrand48
, you need to provide the compiler with a-D_SVID_SOURCE
parameter.But perhaps an easier way is to just use C99 with the GNU extensions. To do that, replace
-std=c99
with-std=gnu99
.And, for
bzero
andclose
, these can be obtained fromstrings.h
andunistd.h
respectively.I was a little confused at first as to why these compiled with
-std=c99
when they have absolutely nothing to do with C99 but then I realised that flag only controls what the standard C headers give you.Neither
strings.h
(note the plural name, this is notstring.h
) norunistd.h
are part of ISO C.看起来您使用的函数不是 ISO C99,所以当您请求时
严格遵守 C99,它们将不可见。
此处的信息:https://bugzilla.redhat.com/show_bug.cgi?id=130815
标志
-D_POSIX_C_SOURCE=200809L
应该可以工作。另请参阅这个问题:为什么可以'当设置 -std=c99 时,gcc 是否找到 random() 接口?
Looks like the functions that you are using are not ISO C99, so when you request
strict C99 compliance they will not be visible.
Information here: https://bugzilla.redhat.com/show_bug.cgi?id=130815
The flag
-D_POSIX_C_SOURCE=200809L
should work.See also this question: Why can't gcc find the random() interface when -std=c99 is set?
您收到的错误听起来像是您正在使用的函数没有被声明。您确定为它们添加了正确的标头吗?
此外,使用 -std=c99 可能会禁用一些不属于标准的扩展。您提到的函数都不属于 C 标准的一部分。如果您找不到它们的单独标头,您可以尝试
-std=gnu99
。The error you're getting makes it sound like the functions you're using aren't being declared. Are you sure you're including the correct headers for them?
Also, using
-std=c99
may disable some extensions which are not part of the standard. None of the functions you mentioned are part of the C standard. If you can't find separate headers for them, you might try-std=gnu99
.-std=c99
导致标头忽略任何可能与 C99 保留命名空间之外的名称冲突的内容,包括所有标准 POSIX 函数。请求标头提供 POSIX 接口的可移植方法是将 _POSIX_C_SOURCE 定义为与所需 POSIX 版本相对应的值。对于最新的 POSIX (2008),这意味着:或在命令行上:
编辑: 看来您想要的功能不在 POSIX 基础中,而是在 XSI 选项中,因此您应该定义
_XOPEN_SOURCE
到适当的值(700
是最新的)来获取它们。这也可以从命令行或源文件完成(但如果从源文件完成,则必须在包含任何系统标头之前完成。-std=c99
causes the headers to omit anything that could clash with uses of names outside the C99 reserved namespace, including all standard POSIX functions. The portable way to request the headers give you the POSIX interfaces is to define_POSIX_C_SOURCE
to a value corresponding to the desired POSIX version. For the latest POSIX (2008), this means:or on the command line:
Edit: It seems the functions you want are not in the POSIX base but in the XSI option, so you should define
_XOPEN_SOURCE
to an appropriate value (700
is the latest) to get them. This too can be done from the command line or your source files (but if from the source files, it must be done before including any system headers.C99 中不再允许隐式声明(假定未声明的函数返回 int)。
也就是说,gcc 不会中止编译。
尝试包含
bzero
的strings.h
,另请参阅 paxdiablo 的答案。Implicit declarations (where an undeclared function is assumed to return int) are no longer allowed in C99.
That said, gcc will not abort the compilation.
Try to include
strings.h
forbzero
, see also paxdiablo's answer.您要求符合标准,以及 C99 没有将
srand48()
定义为
提供的函数。对于 GNU C 库,您可以通过定义
/usr/include/features.h
顶部注释中列出的一个或多个选项来请求其他功能,方法是使用#define
在#include
之前,或者使用-D
标志到gcc
。对于
srand48()
(和drand48()
),您可能需要-D_XOPEN_SOURCE=500
或-D_SVID_SOURCE
(或源文件中的#define _XOPEN_SOURCE 500
等)。如果您
#include
记录的标头,bzero()
和close()
甚至可以与-std=c99
一起使用它们的文件,分别是
和
。You're asking for standards compliance, and C99 doesn't define
srand48()
as a function provided by<stdlib.h>
.For the GNU C library, you can request additional features by defining one or more of the options listed in the comment at the top of
/usr/include/features.h
, either by#define
before you#include
, or with-D
flag togcc
.For
srand48()
(anddrand48()
) you probably want either-D_XOPEN_SOURCE=500
or-D_SVID_SOURCE
(or#define _XOPEN_SOURCE 500
etc. in the source files).bzero()
andclose()
should work even with-std=c99
if you#include
the documented header files for them, which are<strings.h>
and<unistd.h>
respectively.