-std=c99 可以阻止我的 #includes 正常工作吗?

发布于 2024-10-15 23:36:41 字数 689 浏览 2 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

倒数 2024-10-22 23:36:41

-std=c99 可以阻止我的#includes 正常工作吗?

不,但它们可能会显示出您对它们工作方式的知识的限制:-)


虽然函数 [sd]rand48stdlib.h< 中有一个原型/code>,它们位于 #ifdef 内,至少在我的系统上:

#if defined __USE_SVID || defined __USE_XOPEN

所以您可能必须显式设置这些宏之一。

但是,在尝试之前,请注意它不起作用。这是因为所有这些东西都是由 gcc功能测试宏

features.h 中有一组非常复杂的规则用于打开或关闭特定功能,并且其中创建的宏控制头文件包含和排除的内容。 __USE_* 变体将根据您提供的其他宏在该头文件中清除和设置。

例如,要设置 __USE_SVID 以便可以使用 srand48,您需要向编译器提供 -D_SVID_SOURCE 参数。

但也许更简单的方法是仅使用带有 GNU 扩展的 C99。为此,请将 -std=c99 替换为 -std=gnu99

并且,对于bzeroclose,可以分别从strings.hunistd.h获取。

一开始我有点困惑,为什么这些与 C99 完全没有关系,但用 -std=c99 编译,但后来我意识到标志只控制标准 C 标头为您提供。

strings.h(注意复数名称,这不是string.h)和unistd.h都不是ISO C 的一部分。

Can -std=c99 prevent my #includes from working properly?

No, but they may show up limitations in your knowledge of how they work :-)


While the functions [sd]rand48 have a prototype in stdlib.h, they're inside an #ifdef, at least on my system:

#if defined __USE_SVID || defined __USE_XOPEN

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 use srand48, 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 and close, these can be obtained from strings.h and unistd.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 not string.h) nor unistd.h are part of ISO C.

晚风撩人 2024-10-22 23:36:41

看起来您使用的函数不是 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?

素食主义者 2024-10-22 23:36:41

您收到的错误听起来像是您正在使用的函数没有被声明。您确定为它们添加了正确的标头吗?

此外,使用 -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.

旧城空念 2024-10-22 23:36:41

-std=c99 导致标头忽略任何可能与 C99 保留命名空间之外的名称冲突的内容,包括所有标准 POSIX 函数。请求标头提供 POSIX 接口的可移植方法是将 _POSIX_C_SOURCE 定义为与所需 POSIX 版本相对应的值。对于最新的 POSIX (2008),这意味着:

#define _POSIX_C_SOURCE 200809L

或在命令行上:

-D_POSIX_C_SOURCE=200809L

编辑: 看来您想要的功能不在 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:

#define _POSIX_C_SOURCE 200809L

or on the command line:

-D_POSIX_C_SOURCE=200809L

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.

失去的东西太少 2024-10-22 23:36:41

C99 中不再允许隐式声明(假定未声明的函数返回 int)。

也就是说,gcc 不会中止编译。

尝试包含 bzerostrings.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 for bzero, see also paxdiablo's answer.

猫性小仙女 2024-10-22 23:36:41

您要求符合标准,以及 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 to gcc.

For srand48() (and drand48()) you probably want either -D_XOPEN_SOURCE=500 or -D_SVID_SOURCE (or #define _XOPEN_SOURCE 500 etc. in the source files).

bzero() and close() should work even with -std=c99 if you #include the documented header files for them, which are <strings.h> and <unistd.h> respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文