gpt4 book ai didi

c - gcc 由于无法识别 __TIMESTAMP__ 和其他一些已定义的构建变量而生成编译时错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:34:23 31 4
gpt4 key购买 nike

我正在使用基于 eclipse 和 GCC 的 IDE 的现有示例来为嵌入式 EFTPOS 设备编译我的应用程序。但在下面几行:

fprintf(stdout, "** Compilation date: %s \n", __TIMESTAMP__);
fprintf(stdout, "**** SDK release: %s \n", __SDK_RELEASE__);
fprintf(stdout, "**** ToolChain release: %s \n", __TOOLCHAIN_RELEASE__);

我收到这些编译时错误,似乎编译器无法识别这些宏。

Symbol '__TIMESTAMP__' could not be resolved    main.c
Symbol '__SDK_RELEASE__' could not be resolved main.c
Symbol '__TOOLCHAIN_RELEASE__' could not be resolved main.c

如果我尝试 fprintf(stdout, "** Compilation date: %s\n", __TIME__); 它编译成功。为什么编译器识别出 __TIME__ 宏但返回 __TIMESTAMP__ 的错误?同样如您所见,__SDK_RELEASE____TOOLCHAIN_RELEASE__ 在构建变量中定义。

Image Link

编辑:感谢您的回复,请原谅我缺少所提供的信息。在 C/C++ build ->setting->symbols 中:

'__SDK_RELEASE__="${SDK_RELEASE}"'
'__TOOLCHAIN_RELEASE__="${TOOLCHAIN_RELEASE}"'

被定义,那么__SDK_RELEASE____TOOLCHAIN_RELEASE__必须是有效的。另外我的意思是如果__TIME__宏是有效的__TIMESTAMP__ 必须做同样的事情,但事实并非如此。

最佳答案

您在构建变量中定义了 SDK_RELEASETOOLCHAIN_RELEASE,那么为什么要将它们与双下划线一起使用??..

将您的代码更改为

fprintf(stdout, "**** SDK release: %s \n", SDK_RELEASE);
fprintf(stdout, "**** ToolChain release: %s \n", TOOLCHAIN_RELEASE);

关于__TIME__ ,如您所见,自 起定义了宏。带有“双下划线”,这就是它起作用的原因。

形成C11

6.4.2.2 Predefined identifiers

Semantics

1 The identifier __func__ shall be implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char func[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function.72)

note.72 Since the name __func__ is reserved for any use by the implementation (7.1.3), if any other identifier is explicitly declared using the name __func__, the behavior is undefined.

强调我的

6.10.8.1 Mandatory macros

1 The following macro names shall be defined by the implementation:

__DATE__ The date of translation of the preprocessing translation unit: a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10. If the date of translation is not available, an implementation-defined valid date shall be supplied.

__FILE__ The presumed name of the current source file (a character string literal).

__LINE__ The presumed line number (within the current source file) of the current source line (an integer constant).

__STDC__ The integer constant 1, intended to indicate a conforming implementation.

__STDC_HOSTED__ The integer constant 1 if the implementation is a hosted implementation or the integer constant 0 if it is not.

__STDC_VERSION__ The integer constant 201112L.

__TIME__ The time of translation of the preprocessing translation unit: a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function. If the time of translation is not available, an implementation-defined valid time shall be supplied.

强调我的

7.1.3 Reserved identifiers

1 Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.

All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces.

[...]

强调我的


如您所见,__TIMESTAMP__ 定义不是标准的,因此编译器可以自由定义或不定义它:您当前的编译器没有。

关于c - gcc 由于无法识别 __TIMESTAMP__ 和其他一些已定义的构建变量而生成编译时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44898557/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com