[转]Mac OS X framework 解析

转载地址:http://hi.baidu.com/yonderbyron/item/9838b73472152e009cc65ec8

Mac OS X framework 解析

1、framework是什么?

framework是一个层级的目录结构,将一系列可共享的资源,比如动态共享库、nib文件、图形文件、本地化相关文件、头文件、以及相关引用文档,包装成一个包(package)。可供许多应用程序同时使用。系统在需要的时候载入他们,且只在内存中存在一个副本。

framework的使用方式和一个单独文件一样,通过Core Foundation Bundle Service 或者 Cocoa NSBundle类访问。framework设计目的和动态共享库、静态共享库类似,但有更多的优点:

>framework将相关但是分散的资源整合起来,易于装卸和定位。

>framework可包含比库更丰富多样的资源类型

>不同版本的framework可被包含在同一个framework里,有更好的后向兼容性

>有且仅有一份framework的只读资源存在于memory中。

注:framework可以只包含resources,也programmatic interface不是必须的。当然这中用法很少使用。

Mac OS X 关键的几个framework:包含Carbon、Cocoa、Application Services、Core Services,组合成umbrella framework,将底层的framework和用户程序隔离开来。

 

2、framework的文件结构

 

 

A simple framework bundle

MyFramework.framework/

 

    MyFramework  -> Versions/Current/MyFramework

 

    Resources    -> Versions/Current/Resources

 

    Versions/

 

        A/

 

            MyFramework

 

            Resources/

 

                English.lproj/

 

                    InfoPlist.strings

 

                Info.plist

 

        Current  -> A

 

 

A framework with multiple versions

MyFramework.framework/

 

    MyFramework  -> Versions/Current/MyFramework

 

    Resources    -> Versions/Current/Resources

 

    Versions/

 

        A/

 

            MyFramework

 

            Resources/

 

                English.lproj/

 

                    InfoPlist.strings

 

                Info.plist

 

        B/

 

            MyFramework

 

            Resources/

 

                English.lproj/

 

                    InfoPlist.strings

 

                Info.plist

 

        Current  -> B

 

 

 

A framework with additional resource types

MyFramework.framework/

 

    Headers      -> Versions/Current/Headers

 

    MyFramework  -> Versions/Current/MyFramework

 

    Resources    -> Versions/Current/Resources

 

    Versions/

 

        A/

 

            Headers/

 

                MyHeader.h

 

            MyFramework

 

            Resources/

 

                English.lproj/

 

                    Documentation

 

                    InfoPlist.strings

 

                Info.plist

 

        B/

 

            Headers/

 

                MyHeader.h

 

            MyFramework

 

            Resources/

 

                English.lproj/

 

                    Documentation

 

                    InfoPlist.strings

 

                Info.plist

 

        Current  -> B

 

 

 

Structure of the Core Services umbrella framework

CoreServices.framework/

 

    CoreServices           -> Versions/Current/CoreServices

 

    CoreServices_debug     -> Versions/Current/CoreServices_debug

 

    CoreServices_profile   -> Versions/Current/CoreServices_profile

 

    Frameworks             -> Versions/Current/Frameworks

 

    Headers                -> Versions/Current/Headers

 

    Resources              -> Versions/Current/Resources

 

    Versions/

 

        A/

 

            CoreServices

 

            CoreServices_debug

 

            CoreServices_profile

 

            Frameworks/

 

                CarbonCore.framework

 

                CFNetwork.framework

 

                OSServices.framework

 

                SearchKit.framework

 

                WebServicesCore.framework

 

            Headers/

 

                Components.k.h

 

                CoreServices-gcc3.p

 

                CoreServices-gcc3.pp

 

                CoreServices.h

 

                CoreServices.p

 

                CoreServices.pp

 

                CoreServices.r

 

            Resources/

 

                Info-macos.plist

 

                version.plist

 

        Current             -> A

 

 

 

3、framework and binding

 

binding意指动态绑定Mach-O库(动态库)文件,动态共享库有几个优点:一、memory使用更有效率,二、更容易让开发者修正错误。

 

关于动态共享库的symbol binding,不同于static linked shared library在编译链接期需要确定symbol是否存在,他是在运行期检查symbol的有效性。这个特点使得动态库存在“lazy linking”的行为:动态链接器自动加载和链接那些需要的模块,也即,一个模块是否链接取决于程序是否有引用这个模块的symbol。

 

上述特点同样也影响framework code的组织:互相依赖和对应的code应该放在同一个module里,并且必须保证每一个symbol只能在一个library里出现一次。

 

framework的安装位置也是一个需要考量的因素,注意以下几点:1)第三方的framework安装在/library/Frameworks。 2)一定不要安装在/system/Library/Frameworks。3) 模块搜索顺序:一、链接期指定的显示路径,二、/library/frameworks,三、/system/library/frameworks

 

4、create a framework

几点注意:编码时使用统一的前缀,以避免和其他framework的symbol冲突。决定是使用private framework还是普通的framework。决定那些code是可以提取做成framework。不要创建umbrella framework

posted @ 2014-06-08 20:02  csshaw  阅读(3901)  评论(0编辑  收藏  举报