具有-std = c99的GCC抱怨不知道struct timespec [英] GCC with -std=c99 complains about not knowing struct timespec

查看:201
本文介绍了具有-std = c99的GCC抱怨不知道struct timespec的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在Linux上使用gcc -std=c99进行编译时,编译器会抱怨不知道struct timespec.但是,如果我不使用-std=c99进行编译,则一切正常.

When I try to compile this on Linux with gcc -std=c99, the compiler complains about not knowing struct timespec. However if I compile this without -std=c99 everything works fine.

#include <time.h>

int main(void)
{
  struct timespec asdf;
  return 0;
}

这是为什么,并且还有办法让它与-std=c99一起使用?

Why is this and is there a way to still get it to work with -std=c99?

推荐答案

明确启用POSIX功能

时间规格来自POSIX,因此您必须启用" POSIX定义:

Explicitly enabling POSIX features

The timespec comes from POSIX, so you have to 'enable' POSIX definitions:

#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */

#include <time.h>

void blah(struct timespec asdf)
{
}

int main()
{
    struct timespec asdf;
    return 0;
}

顶部的节是我当前使用的节-它会根据您使用的是C99还是C89编译器触发Single UNIX Specification(SUS)中的定义.

The stanza at the top is what I currently use - it triggers the definitions from Single UNIX Specification (SUS) based on whether you're using a C99 or C89 compiler.

  • 如果您要使用 POSIX 2008 (SUS v4)材料,请使用_XOPEN_SOURCE 700
  • 如果您要使用 POSIX 2004 (SUS v3)材料,请使用_XOPEN_SOURCE 600
  • 如果您要使用 POSIX 1995 (SUS v2,1997)材料,请使用_XOPEN_SOURCE 500
  • If you want the POSIX 2008 (SUS v4) material, use _XOPEN_SOURCE 700
  • If you want the POSIX 2004 (SUS v3) material, use _XOPEN_SOURCE 600
  • If you want the POSIX 1995 (SUS v2, 1997) material, use _XOPEN_SOURCE 500

如注释中所述,使用_XOPEN_SOURCE严格启用XSI(X/开放系统接口)扩展,而不是严格的POSIX,但是很少需要POSIX而不是XSI.通常,您应该指定_XOPEN_SOURCE而不是_POSIX_C_SOURCE.有关更多信息,请参见编译环境上的(POSIX 2018).有关功能宏.

As noted in the comments, using _XOPEN_SOURCE strictly enables the XSI (X/Open System Interface) extensions over strict POSIX, but it is very rare for you to want POSIX and not XSI. You should normally specify _XOPEN_SOURCE and not futz with _POSIX_C_SOURCE. See (POSIX 2018) on The Compilation Environment for more information about feature macros.

对于我在2010年使用的系统,POSIX 2008的普及程度不如POSIX 2004,因此,我使用的是YMMV.请注意,SUS v3和v4都需要C99编译.至少在Solaris上,使用C89失败.

For my systems in 2010, POSIX 2008 was not as widely available as POSIX 2004, so that's what I used - but YMMV. Note that SUS v3 and v4 both require C99 compilation. On Solaris, at least, using C89 failed.

如果为GCC(或Clang模拟GCC)指定-std=c11,则仅启用标准C定义.如果使用-std=gnu11,则默认情况下会显示POSIX和标准C的其他扩展.

If you specify -std=c11 to GCC (or Clang emulating GCC), then only the standard C definitions are enabled. If you use -std=gnu11, then POSIX and other extensions to standard C are visible by default.

请注意,默认情况下,GCC 4.x和更早版本使用了-std=gnu90(对应于C90及其扩展名). GCC 5.x和更高版本默认使用-std=gnu11.默认情况下,从来没有一个版本的GCC启用-std=gnu99.

Note that GCC 4.x and earlier used -std=gnu90 (corresponding to C90 plus extensions) by default. GCC 5.x and later use -std=gnu11 by default. There was never a version of GCC that enabled -std=gnu99 by default.

我现在(2019年)使用标头来封装此信息,以便将来的更改仅需要更改为一个标头,而不需要更改为使用POSIX功能的每个源文件.随着时间的流逝以及POSIX 2008的盛行,从多个源文件中编辑旧节非常痛苦.

I now (2019) use a header to encapsulate this information so that future changes only require the change to a single header, not to every source file that uses POSIX features. It was painful editing the old stanza out of multiple source files as time passed and POSIX 2008 became prevalent.

/*
@(#)File:           $RCSfile: posixver.h,v $
@(#)Version:        $Revision: 1.4 $
@(#)Last changed:   $Date: 2017/06/18 00:15:42 $
@(#)Purpose:        Request appropriate POSIX and X/Open Support
@(#)Author:         J Leffler
@(#)Copyright:      (C) JLSS 2010-2017
*/

/*TABSTOP=4*/

#ifndef JLSS_ID_POSIXVER_H
#define JLSS_ID_POSIXVER_H

/*
** Include this file before including system headers.  By default, with
** C99 support from the compiler, it requests POSIX 2008 support.  With
** C89 support only, it requests POSIX 1997 support.  Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if defined(__cplusplus)
#define _XOPEN_SOURCE 700   /* SUS v4, POSIX 1003.1 2008/13 (POSIX 2008/13) */
#elif __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 700   /* SUS v4, POSIX 1003.1 2008/13 (POSIX 2008/13) */
#else
#define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

#endif /* JLSS_ID_POSIXVER_H */

您可以使用此标头中的信息,而无需Stack Overflow使用的"CC by-sa 3.0"许可证通常要求的归属和版权声明.此代码可在我的GitHub上的 SOQ (堆栈溢出问题)存储库中找到,文件格式为posixver.h src/libsoq 子目录.

You may use the information from this header without the attribution and copyright notice normally required by the "CC by-sa 3.0" licence used by Stack Overflow. This code is available in my SOQ (Stack Overflow Questions) repository on GitHub as file posixver.h in the src/libsoq sub-directory.

请注意, C11 定义了struct timespec,并且在与POSIX(首先定义它)兼容的方式.

Note that C11 defines struct timespec, and does so in a way that is compatible with POSIX (which defined it first).

标头 <time.h> 定义了类型.在 <threads.h> 中声明了使用它的三个函数.另一个在<time.h>中:

这些当然也是C17(C18)的一部分.您必须使用-std=c11或类似的东西进行编译(GCC 9.2.0似乎可以识别-std=c17-std=c18,并且对于下一版本的标准来说,是-std=c2x)才能定义类型struct timespec自动.

These are also part of C17 (C18) of course. You would have to be compiling with -std=c11 or similar (GCC 9.2.0 seems to recognize both -std=c17 and -std=c18, and -std=c2x for the next version of the standard) for the type struct timespec to be defined automatically.

这篇关于具有-std = c99的GCC抱怨不知道struct timespec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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