objective-c - 为什么静态函数会消除 Xcode 中的 undefined symbol ?

标签 objective-c xcode macos linker iokit

我正在尝试使用 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)

最佳答案

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

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

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

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

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

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

关于objective-c - 为什么静态函数会消除 Xcode 中的 undefined symbol ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7683579/

相关文章:

iphone - 数据保护开启 : standardUserdefaults corrupted after iOS restart

ios - 如何在 XCode 中使用 NSConnection 进行 JSON 延迟加载?

iphone - UIWebView 中的谷歌地图从核心传递数据

ruby-on-rails - 使用 wicked_pdf gem 用 wkhtmltopdf 生成 PDF,卡在 Mac 上

iphone - 应用内购买确认对话框的本地化

ios - 如何区分在 iOS 上单击的音量按钮与软件设置的音量按钮

iphone - 接收者类型 *** 例如消息是前向声明

ios - 为什么 Collection View 使用此代码加载中心轮播?

cocoa - 是否有等效的 -applicationDidReceiveMemoryWarning : on the Mac

尽管 Android 文件传输检测到设备,但 Android ADB 未在 Mac OS X 中检测到设备