从 Xcode 忽略动态库中未定义的符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17281901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ignoring an undefined symbol in a dynamic library from Xcode
提问by John Bowers
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).
我有一个在 Xcode 动态库目标中被引用的符号,但它没有在那里定义。我需要这个符号是未定义的。这是因为它将在包含它的每个进程中以不同的方式编译(基于某些编译时间定义)。
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.
Xcode 中的动态库目标无法链接,因为它包含对此符号的引用(这并不意外),但我知道该符号将在运行时可用。我将把这个函数编译到公共库链接到的每个目标中。
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).
如果我在我的 Xcode 项目中将“-undefined dynamic_lookup”指定为“其他链接器标志”之一,我就能够将它链接起来。问题是我不想走那么远。我知道只有 1 个符号应该是未定义的。我希望所有其余的符号在未定义时生成错误(我想基本上避免运行时丢失符号错误)。
I found a ld linker option that seems like it should do what I need (from ld man page):
我发现了一个 ld 链接器选项,它似乎应该满足我的需要(来自 ld 手册页):
-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:
但是,我似乎无法让它发挥作用。每当我在“其他链接器标志”中指定“-U symbolName”或“-UsymbolName”时,我仍然会遇到此链接器错误:
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?
我是否错误地使用了 -U?它真的不是我需要的选项,还是它也没有像预期的那样工作?
回答by BB9z
Set -Wl,-undefined,dynamic_lookup
to OTHER_LDFLAGS
.
设置-Wl,-undefined,dynamic_lookup
为OTHER_LDFLAGS
。
Link: Xcode clang link: Build Dynamic Framework (or dylib) not embed dependencies
回答by John Smith
Setting -Wl,-undefined,dynamic_lookup
is dangerous, since it disables all undefined warning.
设置-Wl,-undefined,dynamic_lookup
很危险,因为它禁用了所有未定义的警告。
Use -Wl,-U,symbol_name
in OTHER_LDFLAGS
to disable warnings for a single symbol.
使用-Wl,-U,symbol_name
中OTHER_LDFLAGS
要禁用警告,一个符号。
In Xcode:
在Xcode中:
- Go to Project/Select Target
- Click Build Settings
- Search
Other Linker Flags
- Enter options
- 转到项目/选择目标
- 单击构建设置
- 搜索
Other Linker Flags
- 输入选项