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

Ric*_*ira 5 c global-variables objective-c static-libraries extern

我已经构建了一个静态库,可以在我的iPhone应用程序中链接.这个库使用了一些全局变量和函数,就像在C中一样.我的问题是,当使用时,例如:

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

extern const int a_variable;
extern const int an_array[DEFINED_VALUE];
Run Code Online (Sandbox Code Playgroud)

当我在代码中的任何地方使用此函数或访问这些变量时,编译器会告诉我

"_do_stuff"引自: - test.o中的[Object testMethod]

"_a_variable"引用自: - test.o中的[Object testMethod]

"_an_array"引用自: - test.o中的[Object testMethod]

未找到符号Collect2:Id返回1退出状态

有没有人曾经遇到过这个问题?我知道我做的事情很愚蠢,我缺少一些关键的Objective-C或C概念,但我真的看不出来.所以我希望有人可以帮助我.提前致谢.

wal*_*lky 5

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

顺便说一句,你可能应该区分你声明这些东西的地方,它们确实应该被声明为extern的地方,以及你定义它们的地方,它们不应该在哪里.也就是说,您可能有一个包含以下内容的头文件:

extern void do_stuff (const int a);
extern const int a_variable;
extern const int an_array[];
Run Code Online (Sandbox Code Playgroud)

然后是一个具有以下内容的实现文件:

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

const int a_variable = 42;
const int an_array[DEFINED_VALUE] = { 1, 2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)

除此之外,a_variable当它实际上const是一个东西时调用它有点误导!