- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在阅读 the Standard : N1570并遇到了一些误解。我写了下面这个简单的例子:
test.h
:
#ifndef TEST_H
#define TEST_H
extern int second;
#endif //TEST_H
test.c
:
#include "test.h"
enum test_enum{
first,
second
};
但是编译失败,报错:
error: ‘second’ redeclared as different kind of symbol
second
^~~~~~
这很奇怪,因为部分 6.4.4.3#2
指定:
2 An identifier declared as an enumeration constant has type int.
在我们的案例中,枚举常量具有文件范围,因此我希望它能够正常编译。
我把上面的例子重写如下:main.c
:
extern int second;
int main(int argc, char const *argv[])
{
printf("Second: %d\n", second);
}
现在链接器提示:
undefined reference to `second'
为什么?它应该在 test.c
中找到定义,因为正如 Section 6.2.2#5
指定的那样:
If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.
最佳答案
你对 6.4.4.3 2 的引用,枚举常量的类型为 int
, 建议你认为因为 extern int second
和 enum { second }
申报second
成为int
, 这两个声明 second
可能指的是同一件事。这是不正确的。
extern int second
声明 second
成为一个对象(内存区域)的名称,该对象将保存 int
. enum { second }
声明 second
成为具有特定值的枚举常量。枚举常量没有与之关联的对象(没有内存)。一个int
对象和 int
常量是不同的东西,您不能在同一范围内对它们使用相同的标识符。
关于您关于链接错误的问题,“未定义对‘second’的引用”,虽然test.c
可能包含 external int second
(因为它由包含的 test.h
提供,这不是 second
的定义。它只是一个声明,它告诉编译器该名称引用一个对象。它没有定义对象。或者,如果test.c
包含 enum { second }
,这只是将 second
声明为常量。它没有定义对象。
由于编程语言发展的历史,关于什么是定义的规则有点复杂。对于在文件范围内声明的对象的标识符,基本上有四种情况:
extern
的声明只是声明,不是定义。示例:extern int second;
.int second = 2;
.extern
的声明没有初始值设定项是一个暂定定义。如果翻译单元(正在编译的源文件,包含所有包含的文件)中没有出现定义,则暂定定义成为定义。示例:int second;
.链接在这里没有帮助。 extern int second
在 test.c
和 extern int second
在 main.c
由于链接,可能引用同一个对象,但没有定义它们引用的对象。或者,换句话说,如果 test.c
包含 enum { second }
, 那么它不会定义名为 second
的对象, 所以没有 extern int second
的对象在 main.c
可以引用。
关于c - 了解标识符的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53568087/
我开始在 Ethereum blockchain 上了解如何开发智能合约以及如何写 web-script用于与智能合约交互(购买、销售、统计......)我得出了该怎么做的结论。我想知道我是否正确理解
我正在 UIView 中使用 CATransform3DMakeRotation,并且我正在尝试进行 45º,变换就像向后放置一样: 这是我拥有的“代码”,但显然没有这样做。 CATransform3
我目前正在测试 WebRTC 的功能,但我有一些脑逻辑问题。 WebRTC 究竟是什么? 我只读了“STUN”、“P2P”和其他...但是在技术方面什么是正确的 WebRTC(见下一个) 我需要什么
我在看 DelayedInit在 Scala in Depth ... 注释是我对代码的理解。 下面的 trait 接受一个非严格计算的参数(由于 => ),并返回 Unit .它的行为类似于构造函数
谁能给我指出一个用图片和简单的代码片段解释 WCF 的资源。我厌倦了谷歌搜索并在所有搜索结果中找到相同的“ABC”文章。 最佳答案 WCF 是一项非常复杂的技术,在我看来,它的文档记录非常少。启动和运
我期待以下 GetArgs.hs打印出传递给它的参数。 import System.Environment main = do args main 3 4 3 :39:1: Coul
private int vbo; private int ibo; vbo = glGenBuffers(); ibo = glGenBuffers(); glBindBuffer(GL_ARRAY_
我正在尝试一个 for 循环。我添加了一个 if 语句以在循环达到 30 时停止循环。 我见过i <= 10将运行 11 次,因为循环在达到 10 次时仍会运行。 如果有设置 i 的 if 语句,为什
我正在尝试了解 WSGI 的功能并需要一些帮助。 到目前为止,我知道它是一种服务器和应用程序之间的中间件,用于将不同的应用程序框架(位于服务器端)与应用程序连接,前提是相关框架具有 WSGI 适配器。
我是 Javascript 的新手,我正在尝试绕过 while 循环。我了解它们的目的,我想我了解它们的工作原理,但我在使用它们时遇到了麻烦。 我希望 while 值自身重复,直到两个随机数相互匹配。
我刚刚偶然发现Fabric并且文档并没有真正说明它是如何工作的。 我有根据的猜测是您需要在客户端和服务器端都安装它。 Python 代码存储在客户端,并在命令运行时通过 Fabric 的有线协议(pr
我想了解 ConditionalWeakTable .和有什么区别 class ClassA { static readonly ConditionalWeakTable OtherClass
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 5年前关闭。 Improve this questi
我还没有成功找到任何可以引导我理解 UIPickerView 和 UIPickerView 模型的好例子。有什么建议吗? 最佳答案 为什么不使用默认的 Apple 文档示例?这是来自苹果文档的名为 U
我在看foldM为了获得关于如何使用它的直觉。 foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a 在这个简单的例子中,我只返回 [Just
答案What are _mm_prefetch() locality hints?详细说明提示的含义。 我的问题是:我想要哪一个? 我正在处理一个被重复调用数十亿次的函数,其中包含一些 int 参数。
我一直在读这个article了解 gcroot 模板。我明白 gcroot provides handles into the garbage collected heap 然后 the handle
提供了一个用例: 流处理架构;事件进入 Kafka,然后由带有 MongoDB 接收器的作业进行处理。 数据库名称:myWebsite集合:用户 并且作业接收 users 集合中的 user 记录。
你好 我想更详细地了解 NFS 文件系统。我偶然发现了《NFS 图解》这本书,不幸的是它只能作为谷歌图书提供,所以有些页面丢失了。有人可能有另一个很好的资源,这将是在较低级别上了解 NFS 的良好开始
我无法理解这个问题,哪个更随机? rand() 或: rand() * rand() 我发现这是一个真正的脑筋急转弯,你能帮我吗? 编辑: 凭直觉,我知道数学答案是它们同样随机,但我忍不住认为,如果您
我是一名优秀的程序员,十分优秀!