Xcode clang link: Build Dynamic Framework (or dyli

2019-06-05 01:00发布

问题:

I’m building a dynamic framework for iOS. It needs reference some symbols from code or other libraries, but I not want link them into the framework.

This can be achieved when build an static library, just setup search path and make sure them not included in target’s build phases.

But when build a dynamic framework or dylib, this result undefined symbol error. I tried all kinds of link options, eg -l -weak_library -weak_framework -I -rpath -rpath-link. But none works.

The link command looks like this:

clang -arch x86_64 -dynamiclib
    -isysroot *iPhone_SDK_PATH* 
    *OPTIONS_NOT_IMPORTANT* 
    -install_name @rpath/Foo.framework/Foo
    -Xlinker -rpath -Xlinker @executable_path/Frameworks
    -Xlinker -rpath -Xlinker @loader_path/Frameworks
    -Xlinker -rpath -Xlinker *BUILD_PATH*
    -mios-simulator-version-min=7.0
    -Xlinker -no_deduplicate
    -Xlinker -objc_abi_version
    -Xlinker 2 
    -fobjc-arc -fobjc-link-runtime
    -framework Foundation
    -single_module
    -compatibility_version 1
    -current_version 1
    -Xlinker -dependency_info
    -Xlinker *BUILD_PATH*/Foo.build/Objects-normal/x86_64/Foo_dependency_info.dat 
    -o *BUILD_PATH*/Foo.framework/Foo

回答1:

clang use ld command to make the final link, I checked the manual and found -U and -undefined can ignore undefined symbols.

-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.

-undefined treatment

Specifies how undefined symbols are to be treated. Options are: error, warning, suppress, or dynamic_lookup. The default is error.

So the final solution is set -Wl,-undefined,dynamic_lookup to OTHER_LDFLAGS, also make sure search path set correctly. It works.