Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do static functions eliminate undefined symbols in Xcode?

I am attempting to use I/O kit and have linked to I/O kit properly.

When I use a function in I/O kit and don't call it within a static function, I get the following error Undefined symbols for architecture x86_64.

Here is an example to suppress the error

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

    }
}

Here is an example that will cause the error.

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

    }
}

Any suggestions as to why this is happening?

EDIT:

Here are the exact error messages:

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)
like image 531
David Avatar asked Oct 07 '11 06:10

David


People also ask

How do I fix undefined symbol in XCode?

The error Undefined symbols for architecture arm64: "_OBJC_CLASS_$_SKAdImpression" during the iOS build usually happens if XCode or CocoaPods version is lower than required. To fix it update XCode to 12.5 or higher and CocoaPods to 1.10. 0 or higher.

What is undefined symbols for architecture x86_64?

Why Is the Undefined Symbols for Architecture x86_64: Error Happening? This error is happening due to the lack of included values inside the declared statements in your code. The browser is going to render the information incorrectly and show this error, especially if you are working with Main and Similarity tools.


1 Answers

Okay I got one scenario when this can happen. If the static function is never called, you won't get that link time error.

For example, I wrote a simple c file with this function, and undef_foobar is not defined:

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

Now, if foobar() is called from my main(), I get the error:

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

If the function isn't called at all from within this c file, there are no linker errors.

like image 150
jman Avatar answered Oct 01 '22 23:10

jman