修改Xcode配置并支持iPhone上dylib工程 实例

移动开发 iOS
本文介绍的是修改Xcode配置并支持iPhone上dylib工程,一篇不错的文章,与大家分享一下。先来看内容。

修改Xcode配置并支持iPhonedylib工程是本文要介绍的内容,逛坛子发现一篇好文章,与大家分享一下,先来看内容。此帖纯粹讨论怎样更改xcode配置来使其支持创建iphone上的dylib工程文件,不涉及任何其他破解话题!

测试机器:10.6 with Xcode3.2

最近在研究如何创建iphone上的dylib文件,google出来的都是toolchain的东西,使用makefile来编译的,像我这种习惯IDE的人来说非常之不爽,所以花了一天时间来破解了下Xcode来让它可以创建编译iphone上的dylib文件的工程。

简单说下分析思路吧

首先xcode支持创建iphone static libtary和Cocoa的dynamic linrary的工程,通过不同编译对比可以发现Xcode通过productType = "com.apple.product-type.library.dynamic";配置来确定工程类型的。

打开**.xcodeproj/project.pbxproj文件,搜索productType=定位,可以发现如下图所示的内容

[attachment=10545]

可以创建一个iphone的static工程把static改成dynamic编译看看,可以发现xcode提示不支持dynamic类型的工程,无法编译,那我们让他支持就好了。

我们需要修改以下文件

  1. /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Specifications/iPhoneOSProductTypes.xcspec  
  2. /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Specifications/iPhoneOSPackageTypes.xcspec  
  3. 在iPhoneOSProductTypes.xcspec中添加下列片段  
  4.  
  5. // Dynamic library  
  6.     {   Type = ProductType;  
  7.         Identifier = com.apple.product-type.library.dynamic;  
  8.         Class = PBXStaticLibraryProductType;  
  9.         Name = "Dynamic Library";  
  10.         Description = "Dynamic library";  
  11.         IconNamePrefix = "TargetLibrary";  
  12.         DefaultTargetName = "Dynamic Library";  
  13.         DefaultBuildProperties = {  
  14.             FULL_PRODUCT_NAME = "$(EXECUTABLE_NAME)";  
  15.             MACH_O_TYPE = "mh_dylib";  
  16.             REZ_EXECUTABLE = YES;  
  17.             EXECUTABLE_PREFIX = "";  
  18.             EXECUTABLE_SUFFIX = ".$(EXECUTABLE_EXTENSION)";  
  19.             EXECUTABLE_EXTENSION = "dylib";  
  20.             PUBLIC_HEADERS_FOLDER_PATH = "/usr/local/include";  
  21.             PRIVATE_HEADERS_FOLDER_PATH = "/usr/local/include";  
  22.             INSTALL_PATH = "/usr/local/lib";  
  23.             DYLIB_INSTALL_NAME_BASE = "$(INSTALL_PATH)";  
  24.             LD_DYLIB_INSTALL_NAME = "$(DYLIB_INSTALL_NAME_BASE:standardizepath)/$(EXECUTABLE_PATH)";  
  25.             DYLIB_COMPATIBILITY_VERSION = "1";  
  26.             DYLIB_CURRENT_VERSION = "1";  
  27.             FRAMEWORK_FLAG_PREFIX = "-framework";  
  28.             LIBRARY_FLAG_PREFIX = "-l";  
  29.             LIBRARY_FLAG_NOSPACE = YES;  
  30.             STRIP_STYLE = "debugging";  
  31.             GCC_INLINES_ARE_PRIVATE_EXTERN = YES;  
  32.             CODE_SIGNING_ALLOWED = NO;  
  33.         };  
  34.         PackageTypes = (  
  35.             com.apple.package-type.mach-o-library   // default  
  36.         );  
  37.     },  
  38.  
  39. 在iPhoneOSPackageTypes.xcspec中添加下列片段  
  40.  
  41. // Mach-O dynamic library  
  42.     {   Type = PackageType;  
  43.         Identifier = com.apple.package-type.mach-o-library;  
  44.         Name = "Mach-O Dynamic Library";  
  45.         Description = "Mach-O dynamic library";  
  46.         DefaultBuildSettings = {  
  47.             EXECUTABLE_PREFIX = "";  
  48.             EXECUTABLE_SUFFIX = ".dylib";  
  49.             EXECUTABLE_NAME = "$(EXECUTABLE_PREFIX)$(PRODUCT_NAME)$(EXECUTABLE_VARIANT_SUFFIX)$(EXECUTABLE_SUFFIX)";  
  50.             EXECUTABLE_PATH = "$(EXECUTABLE_NAME)";  
  51.         };  
  52.         ProductReference = {  
  53.             FileType = compiled.mach-o.dylib;  
  54.             Name = "$(EXECUTABLE_NAME)";  
  55.             IsLaunchable = NO;  
  56.         };  
  57.     }, 

文件附上,可以直接下载替换,替换/修改前做好备份

[attachment=10546]

现在再编译应该不会提示工程不支持了,那么剩下的就是改变其编译选项了,就是libtool的参数问题。

我们需要修改下列文件

  1. /Developer/Library/Xcode/Plug-ins/CoreBuildTasks.xcplugin/Contents/Resources/Libtool.xcspec  
  2. /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Plug-ins/iPhoneOS Build System Support.
  3. xcplugin/Contents/Resources/iPhoneLinkerOptions.xcspec  
  4. 在Libtool.xcspec中添加下列片段  
  5.  
  6. {   Identifier = com.apple.pbx.linkers.libtooldynamic;  
  7.     Type = Linker;  
  8.     Class = PBXLinkerSpecificationLibtool;  
  9.     Name = "Libtool";  
  10.     Description = "Create a dynamic library using Apple Mach-O Librarian (libtool)";  
  11.     IsAbstract = Yes;       // This is an internal tool, so we keep it out of the user interface  
  12.     BinaryFormats = (mach-o);  
  13.     Architectures = (ppc, ppc7400, ppc7450, ppc970, ppc64, i386, x86_64);  
  14.     CommandLine = "$(LIBTOOL) -dynamic -arch_only $(arch) -compatibility_version $(DYLIB_COMPATIBILITY_VERSION) 
  15. -current_version $(DYLIB_CURRENT_VERSION) [options] [special-args] -o $(OutputPath)";     
  16.  // 'special-args' includes the input files  
  17.     RuleName = "Libtool $(OutputPath) $(variant) $(arch)";  
  18.     ExecDescription = "Create Dynamic Library $(OutputPath:file)";  
  19.     InputFileTypes = (  
  20.         compiled.mach-o.objfile  
  21.     );  
  22.     Outputs = (  
  23.         // We're a linker-like task, so we expect to be given an output path in 'OutputPath'.  
  24.         "$(OutputPath)"  
  25.     );  
  26.     CommandOutputParser = XCGccCommandOutputParser;  
  27.     Options = (  
  28.         // LIBTOOL  
  29.         {   Name = LIBTOOL;  
  30.             Type = Path;  
  31.             DefaultValue = "$(PLATFORM_DEVELOPER_BIN_DIR)/libtool";  
  32.         },  
  33.  
  34.         // SDK  
  35.         {   Name = SDKROOT;  
  36.             Type = Path;  
  37.             CommandLineFlag = "-syslibroot";  
  38.             IsInputDependency = Yes;  
  39.         },  
  40.  
  41.         // Search paths  
  42.         {   Name = LIBRARY_SEARCH_PATHS;  
  43.             Type = PathList;  
  44.             FlattenRecursiveSearchPathsInValue = Yes;   // causes any paths that have a '/**' suffix to be replaced with matching paths  
  45.             CommandLinePrefixFlag = "-L";  
  46.         },  
  47.  
  48.         // Input file lists  
  49.         {   Name = __INPUT_FILE_LIST_PATH__;  
  50.             Type = Path;  
  51.             DefaultValue = "$(LINK_FILE_LIST_$(variant)_$(arch))";      // this is set up for us as a read-only property  
  52.             CommandLineFlag = "-filelist";  
  53.             IsInputDependency = Yes;  
  54.         },  
  55.         {   Name = auxiliary_file_lists;  
  56.             Type = PathList;  
  57.             CommandLineFlag = "-filelist";  
  58.             IsInputDependency = Yes;  
  59.         },  
  60.  
  61.         // Various flags  
  62.         {   Name = ALL_OTHER_LIBTOOLFLAGS;  
  63.             Type = StringList;  
  64.             DefaultValue = "$(LD_FLAGS) $(SECTORDER_FLAGS) $(OTHER_LDFLAGS) $(OTHER_LDFLAGS_$(variant))
  65.   $(OTHER_LDFLAGS_$(arch)) $(OTHER_LDFLAGS_$(variant)_$(arch)) $(OTHER_LIBTOOLFLAGS) $(OTHER_LIBTOOLFLAGS_$(variant)) 
  66. $(OTHER_LIBTOOLFLAGS_$(arch)) $(OTHER_LIBTOOLFLAGS_$(variant)_$(arch)) $(PRODUCT_SPECIFIC_LIBTOOLFLAGS)";  
  67.             CommandLinePrefixFlag = "";  
  68.         },  
  69.         {   Name = EXPORTED_SYMBOLS_FILE;  
  70.             Type = Path;  
  71.             Condition = "$(SEPARATE_SYMBOL_EDIT) == NO";  
  72.             CommandLineFlag = "-exported_symbols_list";  
  73.             IsInputDependency = Yes;  
  74.         },  
  75.         {   Name = UNEXPORTED_SYMBOLS_FILE;  
  76.             Type = Path;  
  77.             Condition = "$(SEPARATE_SYMBOL_EDIT) == NO";  
  78.             CommandLineFlag = "-unexported_symbols_list";  
  79.             IsInputDependency = Yes;  
  80.         },  
  81.         {   Name = AdditionalCommandLineArguments;  
  82.             Type = StringList;  
  83.             CommandLinePrefixFlag = "";  
  84.         },  
  85.  
  86.         {   Name = MACOSX_DEPLOYMENT_TARGET;  
  87.             Type = String;  
  88.             SetValueInEnvironmentVariable = "MACOSX_DEPLOYMENT_TARGET";  
  89.         },  
  90.     );  
  91. }, 

在iPhoneLinkerOptions.xcspec中搜索

  1. BasedOn = "default:com.apple.pbx.linkers.libtool"

替换成

  1. BasedOn = "default:com.apple.pbx.linkers.libtooldynamic"

这样修改后编译iphone的library工程师都会使用dynamic的参数来编译,

注意:如果要编译iphone static library这里需要将libtooldynamic修改回libtool并重启Xcode(***的小缺陷,应该可以通过hook xcode编译设置函数来动态修改,很麻烦,需要很多时间去找到相应的方法,找了一会儿就没高兴继续,先将就用吧)

做好以上工作后就可以创建并编译iphone上dynamic library的工程了。

这里附上工程和target模板文件

  1. [attachment=10547

解压此zip得到Cocoa Touch Dynamic Library文件夹,将其放到

  1. /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/Library/下面  
  2. [attachment=10548

解压此zip得到Dynamic Library.trgttmpl文件,将其放到

  1. /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Target Templates/Cocoa Touch/下面 

重启Xcode就可以看到了,如图

  1. [attachment=10549]  [attachment=10550]  

测试结果

  1. [attachment=10551]  

使用otool查看编译出来的dylib文件

  1. [attachment=10552]  

成功!

谨记!!!!!需要修改文件来切换回编译static library!!!

小结:关于修改Xcode配置并支持iPhonedylib工程的内容介绍完了,希望本文对你有所帮助,更多相关内容请参考便捷而推荐。

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-07-27 14:56:07

iPhone Makefile Dylib

2011-08-08 17:31:30

Xcode 工程

2011-08-04 17:24:12

iPhone开发 Xcode 窗口

2011-07-06 18:10:20

Xcode 4 iPhone

2011-07-06 15:44:26

iPhone XCode Leopard

2011-07-07 15:32:07

2011-07-06 17:53:40

iPhone SDK Xcode

2011-07-07 09:20:30

Xcode

2023-02-08 18:08:16

2011-07-06 18:31:21

Xcode 4 iPhone 模拟器

2011-07-07 17:36:13

iPhone Xcode 窗口

2011-07-20 10:12:33

XCode Cocoa dylib

2011-07-06 17:48:30

iPhone Xcode 模拟器

2011-07-22 18:25:20

XCode iPhone SDK

2011-08-04 17:19:49

iPhone开发 Xcode 文档

2019-04-22 11:50:38

LinuxFlatpak

2011-07-18 17:52:47

Linux iPhone

2011-07-25 18:02:51

iPhone LibFetion 移植

2011-07-20 10:59:46

2011-07-06 10:32:07

Xcode
点赞
收藏

51CTO技术栈公众号