gpt4 book ai didi

c++ - 使用特定命名空间名称时 Arduino 链接器错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:17 25 4
gpt4 key购买 nike

我正在尝试编写一个 Arduino 库。

我已将所有代码放入命名空间中,但出现了一些链接器错误。

奇怪的是,当我更改命名空间的名称 时,这些错误都消失了。

我的问题是这可能是什么原因造成的?会不会是这个命名空间已经被其他库使用了?

最小示例:

at24c256.h

#ifndef AT24C256
#define AT24C256

namespace AT24C256 {
int f();
}

#endif

at24c256.cpp

#include "at24c256.h"

namespace AT24C256 {
int f() {return 42;}
}

然后在草图中,我只调用了 AT24C256::f()

#include "at24c256.h"

void setup() {
}

void loop() {
AT24C256::f();
}

所有三个文件都在同一目录中。我在这种情况下得到的错误是:

sketch_dec13b.cpp: In function ‘void loop()’:
sketch_dec13b.cpp:155:13: error: ‘::f’ has not been declared

如果我将 AT24C256 更改为其他内容,构建将顺利完成。

更新:

我不小心发布了错误的错误消息。我实际得到的是:

sketch_jan04a.cpp.o: In function `loop':
sketch_jan04a.cpp:10: undefined reference to `(anonymous namespace)::f()'
collect2: ld returned 1 exit status

最佳答案

哦,我终于找到问题所在了。 include guards 定义了 AT24C256,它正是我的命名空间的名称,因此它被预处理为空。

关于c++ - 使用特定命名空间名称时 Arduino 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920575/

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