[英]Using extern to refer to a function defined in a different compilation unit
Due to some static data, I have a function 由于一些静态数据,我有一个功能
void foo(MyNamespace::bar)
defined in a certain compilation unit. 在某个编译单元中定义。 But its point of use is in another compilation unit.
但它的使用点在于另一个编译单元。 So I use
所以我用
namespace MyNamespace
{
extern void foo(bar);
}
But the linker can't find the function definition. 但是链接器找不到函数定义。 Am I misusing
extern
? 我是否误用了
extern
?
extern
can be used for this kind of thing. extern
可以用于这种事情。
Your problem is that the linker is expecting a function MyNamespace::foo(bar);
你的问题是链接器需要一个函数
MyNamespace::foo(bar);
due to the fact that your extern
statement is within MyNamespace
. 由于您的
extern
语句在MyNamespace
。
You have two choices: 你有两个选择:
use extern void foo(MyNamespace::bar);
使用
extern void foo(MyNamespace::bar);
at your "point of use". 在你的“使用点”。 Don't enclose that line within
MyNamespace
. 不要将该行括在
MyNamespace
。
Alternatively, enclose the function definition within MyNamespace
. 或者,将函数定义包含在
MyNamespace
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.