环境
- macOS 10.14.1
- Xcode 10.3
- objc4-750.1
下载源码以及相关依赖
- 在MacOS 10.14.1 Source下载相关源码,
Command + F
快捷键查找objc4
,下载。
- 下载
objc4
相关依赖库,dyld、libauto、Libc、libclosure、libdispatch、libplatform、libpthread、xnu等,解压后的文件夹如下图:
针对编译错误一一修改
1、出错原因:
error:The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture. (in target 'objc-trampolines')
error: The i386 architecture is deprecated. You should update your ARCHS build setting to remove the i386 architecture. (in target 'objc')
解决方案: 找出target(objc和objc-trampolines都要改) -> Build Settings -> Architectures ->选择标准架构。
2、在objc-os.h
头文件里找不到sys/reason.h
文件
解决:工程目录下创建Common/sys
目录,在编译设置(Build
Settings里面搜索,Header Search Paths
,然后将Common
索
引添加进去),然后在之前下载的依赖包中搜索reason.h(路
径:xnu-4903.221.2/bsd/sys/reason.h
)头文件,复制到
Common/sys目录下,如图:
3、在objc-os.h
头文件报错mach-o/dyld_priv.h
file not found
解决:在Common文件夹下添加mach-o
文件夹,在依赖库文件中搜索dyld_priv.h
,路径为dyld-635.2/include/mach-o/dyld_priv.h
,搜索到的文件添加进mac-o文件夹内
4、'os/lock_private.h' file not found
依赖库文件路径:libplatform-177.200.16/private/os/lock_private.h
,在Common文件夹下创建os文件夹,将该头文件添加进去。
5、总结缺失的文件,按照上述方法添加
'os/base_private.h' file not found
,在libplatform-177.200.16/private/os/base_private.h
'pthread/tsd_private.h' file not found
,在libpthread-330.220.2/private/tsd_private.h
System/machine/cpu_capabilities.h' file not found
,在xnu-4903.221.2/osfmk/machine/cpu_capabilities.h
'os/tsd.h' file not found
,在xnu-4903.221.2/libsyscall/os/tsd.h
'pthread/spinlock_private.h' file not found
,在libpthread-330.220.2/private/spinlock_private
'System/pthread_machdep.h' file not found
,在Libc-825.26/pthreads/pthread_machdep.h
,(PS:这个头文件在Libc-1272.200.26
下找不到哦,请下载Libc-825.26
)'CrashReporterClient.h' file not found
,在Libc-825.26/include/CrashReporterClient.h
,并且在Build Settings->Preprocessor Macros中加入:LIBC_NO_LIBCRASHREPORTERCLIENT
'Block_private.h' file not found
,在libdispatch-1008.220.2/src/BlocksRuntime/Block_private.h
'objc-shared-cache.h' file not found
,在dyld-635.2/include/objc-shared-cache.h
Typedef redefinition with different types ('int' vs 'volatile OSSpinLock' (aka 'volatile int'))
,这种redefinition错误时,在工程目录下搜索pthread_lock_t
,发现有两个文件夹里都定义了pthread_lock_t
,注释掉pthread_machdep.h
文件中的定义提示
Static declaration of '_pthread_getspecific_direct' follows non-static declaration
,这里三个函数定义重复,这里把pthread_machdep.h
文件中的定义注释掉。'_simple.h' file not found
,在libplatform-177.200.16/private/_simple.h
'isa.h' file not found
,isa.h
文件在项目runtime
文件夹中,把它引入到common文件夹下can't open order file: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/AppleInternal/OrderFiles/libobjc.order
,修改工程配置,将Build Settings->Linking->Order File改为工程根目录下的libobjc.order,即:$(SRCROOT)/libobjc.order
。library not found for -lCrashReporterClient
,此时在 Build Settings -> Linking -> Other Linker Flags里删掉"-lCrashReporterClient"(Debug和Release都删了)SDK "macosx.internal" cannot be located. xcrun: error: unable to find utility "clang++", not a developer tool or in PATH
,把Target-objc
的Build Phases->Run Script(markgc)里的内容macosx.internal改为macosxno such public header file: '/tmp/objc.dst/usr/include/objc/ObjectiveC.apinotes'
,这里需要把Target-objc的Build Settings->Other Text-Based InstallAPI Flags里的内容设为空!并且一定记得要把Text-Based InstallAPI Verification Model里的值改为Errors Only.
完整的项目目录结构如下图:
最终编译成功~
测试代码
新建一个Target,在MyMac上我创建了一个命令行的工程。
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import <objc/message.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
Class newClass = objc_allocateClassPair(objc_getClass("NSObject"), "newClass",
0);
objc_registerClassPair(newClass);
id newObject = [[newClass alloc] init];
NSLog(@"%@",newObject);
}
return 0;
}