静态库中的外部变量,使用 Objective-C
我已经构建了一个静态库,可以链接到我的 iPhone 应用程序中。这个库使用一些全局变量和函数,就像在 C 中一样。我的问题是,例如使用时:
extern
void do_stuff (const int a)
{
return a*a;
}
extern const int a_variable;
extern const int an_array[DEFINED_VALUE];
当我在代码中的任何位置使用这个函数或访问这些变量时,编译器告诉我
“_do_stuff”引用自: -tests.o
“_a_variable”中的[Object testMethod]引用自: -tests.o
“_an_array”中的[Object testMethod]引用自: -tests.o 中的[对象 testMethod]
未找到符号 Collect2: Id returned 1 exit status
以前有人遇到过这个问题吗?我知道我在做一些愚蠢的事情,我错过了一些关键的 Objective-C 或 C 概念,但我真的看不出是什么。所以我希望有人能帮助我。提前致谢。
I've built a static library, to be linked in my iPhone apps. This library uses some global variables and functions, like in C. My problem is, when using for example:
extern
void do_stuff (const int a)
{
return a*a;
}
extern const int a_variable;
extern const int an_array[DEFINED_VALUE];
When I use this function, or access these variables, anywhere in my code, the compiler tells me
"_do_stuff" referenced from:
-[Object testMethod] in tests.o
"_a_variable" referenced from:
-[Object testMethod] in tests.o
"_an_array" referenced from:
-[Object testMethod] in tests.o
Symbol(s) not found
Collect2: Id returned 1 exit status
Has anyone ever faced this problem before? I know I'm doing something stupid, I'm missing some key Objective-C or C concept, but I can't really see what. So I was hoping someone could help me. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些是链接器错误,告诉您找不到引用的实体。这可能意味着您尚未将库添加到项目中。
顺便说一句,您可能应该区分声明这些东西的位置(它们确实应该声明为
extern
)和定义的位置< /em> 他们,他们不应该在的地方。也就是说,您可能有一个头文件,其中包括:然后是一个实现文件,其中包含以下内容:
另外,当它实际上是一个
const
时,调用某个a_variable
是一个有点误导!These are linker errors, telling you that the referenced entities can't be found. Probably this means that you haven't added your library to the project.
As an aside, you probably should distinguish between the place where you declare these things, where they should indeed be declared as
extern
, and the place where you define them, where they shouldn't be. That is, you might have a header file that includes:And then an implementation file that has something like:
As another aside, calling something
a_variable
when it's actually aconst
is a bit misleading!@walkytalky好吧,我在用 grep 过滤的 .a 上运行 nm 来查看这些符号是否已导出。
那么似乎每个符号都有一个未定义的副本?
@walkytalky Well I ran nm on the .a filtered with grep to see if those symbols were exported.
So it seems that for each symbol there's an undefined copy?