【问题标题】:Why do static functions eliminate undefined symbols in Xcode?为什么静态函数会消除 Xcode 中的未定义符号?
【发布时间】:2011-12-02 18:31:01
【问题描述】:

我正在尝试使用 I/O 套件并已正确链接到 I/O 套件。

当我在 I/O 套件中使用函数并且不在静态函数中调用它时,我收到以下错误 Undefined symbols for architecture x86_64

这是一个抑制错误的例子

static void test(void)
{
    if (IORegisterForSystemPower(...)) 
    {

    }
}

这是一个会导致错误的示例。

void test(void)
{
    if (IORegisterForSystemPower(...)) 
    {

    }
}

关于为什么会发生这种情况的任何建议?

编辑:

以下是确切的错误消息:

Undefined symbols for architecture x86_64:
  "_IORegisterForSystemPower", referenced from:
      _registerNotificaitonUsingIOKit in AppDelegate.o
  "_IONotificationPortGetRunLoopSource", referenced from:
      _registerNotificaitonUsingIOKit in AppDelegate.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

【问题讨论】:

  • 你能粘贴完整的错误信息列表吗?例如,它是否抱怨符号 IORegisterForSystemPower?
  • 我添加了错误消息。请参见上文。

标签: objective-c xcode macos linker iokit


【解决方案1】:

好的,我有一个可能发生这种情况的情况。如果从不调用静态函数,则不会出现链接时间错误。

比如我用这个函数写了一个简单的c文件,没有定义undef_foobar

static int foobar (void) 
{
    undef_foobar ();
}

现在,如果从我的 main() 调用 foobar(),我会收到错误消息:

Undefined symbols for architecture x86_64:
  "_undef_foobar", referenced from:

如果在这个 c 文件中根本没有调用该函数,则没有链接器错误。

【讨论】:

  • 我尝试了您的示例,Xcode 警告我未定义函数 Implicit declaration of function "undef_foobar" is invalid in C99。您介意尝试链接,然后从 I/O 工具包中调用一个函数来查看是否遇到相同的链接器问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 2017-06-21
相关资源
最近更新 更多