Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring an undefined symbol in a dynamic library from Xcode

Tags:

xcode

clang

ld

I have a symbol that is being referenced in an Xcode dynamic library target, but it is not defined there. I NEED this symbol to be undefined. This is because it will be compiled differently in each process that includes it (based upon some compile time defines).

The dynamic library target in Xcode that fails to link because it contains a reference to this symbol (which is not unexpected), but I know that the symbol will be available at run time. I will be compiling this function into each target that the common library is linked to.

I am trying to get the linker to mark this particular symbol for dynamic lookup at run time.

I have been able to get it to link if I specify "-undefined dynamic_lookup" as one of the "Other Linker Flags" in my Xcode project. The problem is that I don't want to go that far. I know that only 1 symbol is supposed to be undefined. I want all the rest of the symbols to generate errors if they are left as undefined (I want to avoid a run time missing symbol error basically).

I found a ld linker option that seems like it should do what I need (from ld man page):

-U symbol_name
             Specified that it is ok for symbol_name to have no definition.  With -two_levelnamespace, the resulting symbol will be marked dynamic_lookup which means dyld will search all loaded images.

However, I cannot seem to get it to work. Whenever I specify "-U symbolName" or "-UsymbolName" in the "Other Linker Flags" I am still greeted with this linker error:

Undefined symbols for architecture x86_64:
     "_symbolName", referenced from: <various object files>

Am I using -U incorrectly perhaps? Is it not really the option I need, or is it just not working like it is supposed too?

like image 554
John Bowers Avatar asked Jun 24 '13 17:06

John Bowers


2 Answers

Setting -Wl,-undefined,dynamic_lookup is dangerous, since it disables all undefined warning.

Use -Wl,-U,symbol_namein OTHER_LDFLAGS to disable warnings for a single symbol.

In Xcode:

  • Go to Project/Select Target
  • Click Build Settings
  • Search Other Linker Flags
  • Enter options
like image 164
John Smith Avatar answered Jan 03 '23 11:01

John Smith


Set -Wl,-undefined,dynamic_lookup to OTHER_LDFLAGS.

Link: Xcode clang link: Build Dynamic Framework (or dylib) not embed dependencies

like image 28
BB9z Avatar answered Jan 03 '23 13:01

BB9z