静态库中的外部变量,使用 Objective-C

发布于 2024-09-07 00:02:46 字数 577 浏览 1 评论 0原文

我已经构建了一个静态库,可以链接到我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀念你的温柔 2024-09-14 00:02:46

这些是链接器错误,告诉您找不到引用的实体。这可能意味着您尚未将库添加到项目中。

顺便说一句,您可能应该区分声明这些东西的位置(它们确实应该声明为extern)和定义的位置< /em> 他们,他们不应该在的地方。也就是说,您可能有一个头文件,其中包括:

extern void do_stuff (const int a);
extern const int a_variable;
extern const int an_array[];

然后是一个实现文件,其中包含以下内容:

void do_stuff (const int a)
{
    return a*a;
}

const int a_variable = 42;
const int an_array[DEFINED_VALUE] = { 1, 2, 3, 4 };

另外,当它实际上是一个 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:

extern void do_stuff (const int a);
extern const int a_variable;
extern const int an_array[];

And then an implementation file that has something like:

void do_stuff (const int a)
{
    return a*a;
}

const int a_variable = 42;
const int an_array[DEFINED_VALUE] = { 1, 2, 3, 4 };

As another aside, calling something a_variable when it's actually a const is a bit misleading!

趁微风不噪 2024-09-14 00:02:46

@walkytalky好吧,我在用 grep 过滤的 .a 上运行 nm 来查看这些符号是否已导出。

host-006:Release-iphonesimulator <username>$ nm -g libCardLib.a | grep CP_
nm: no name list
     U _CP_BACK
     U _CP_FILE_EXTENSION_SUFFIX
     U _CP_FILE_PATH
     U _CP_SUIT_PREFIX
     U _CP_VALUE_PREFIX
00002020 D _CP_BACK
00002018 D _CP_FILE_EXTENSION_SUFFIX
0000201c D _CP_FILE_PATH
00002024 D _CP_FRONT
00002108 D _CP_SUIT_PREFIX
0000210c D _CP_VALUE_PREFIX
nm: no name list
nm: no name list
nm: no name list

那么似乎每个符号都有一个未定义的副本?

@walkytalky Well I ran nm on the .a filtered with grep to see if those symbols were exported.

host-006:Release-iphonesimulator <username>$ nm -g libCardLib.a | grep CP_
nm: no name list
     U _CP_BACK
     U _CP_FILE_EXTENSION_SUFFIX
     U _CP_FILE_PATH
     U _CP_SUIT_PREFIX
     U _CP_VALUE_PREFIX
00002020 D _CP_BACK
00002018 D _CP_FILE_EXTENSION_SUFFIX
0000201c D _CP_FILE_PATH
00002024 D _CP_FRONT
00002108 D _CP_SUIT_PREFIX
0000210c D _CP_VALUE_PREFIX
nm: no name list
nm: no name list
nm: no name list

So it seems that for each symbol there's an undefined copy?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文