ios|ios 编译调试技巧

一、clang编译oc文件
一般没有引用等系统动态库时:

clang -rewrite-objc main.m -o main.cpp

【ios|ios 编译调试技巧】引用oc库时,会报错
$ clang -rewrite-objc main.m -o main.cpp main.m:9:9: fatal error: 'UIKit/UIKit.h' file not found #import ^~~~~~~~~~~~~~~ 1 error generated.

解决方案:
clang -x objective-c -rewrite-objc -isysroot/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdkmain.m

其他解决方案,xcode命令行:
xcrun -sdk iphonesimulator clang -rewrite-objc main.m xcrun -sdk iphoneos clang -rewrite-objc main.m

二、开启调试模式、打印出所有运行时发送的消息
#include extern void instrumentObjcMessageSends(BOOL); int main(int argc, const char * argv[]) { @autoreleasepool { Student *student = [[Student alloc] init]; instrumentObjcMessageSends(YES); [student saySomething]; instrumentObjcMessageSends(NO); } return 0; }

执行后,本地会多出一个文件,在/private/tmp/msgSend-xxxx,可以看到当前调用的所有方法。
这个需要在objc源码环境
三、环境变量
添加方法: Project -> Scheme -> Edit Scheme ,在 Arguments 下可以添加运行时的环境变量( Environment Variables )
变量名 介绍 备注
OBJC_PRINT_OPTIONS list which options are set 输出OBJC已设置的选项
OBJC_PRINT_IMAGES log image and library names as they are loaded 输出已load的image信息
OBJC_PRINT_LOAD_METHODS log calls to class and category +load methods 打印 Class 及 Category 的 + (void)load 方法的调用信息
OBJC_PRINT_INITIALIZE_METHODS log calls to class +initialize methods 打印 Class 的 + (void)initialize 的调用信息
OBJC_PRINT_RESOLVED_METHODS log methods created by +resolveClassMethod and +resolveInstanceMethod: 打印通过 +resolveClassMethod: 或 +resolveInstanceMethod: 生成的类方法
OBJC_PRINT_CLASS_SETUP log progress of class and category setup 打印 Class 及 Category 的设置过程
OBJC_PRINT_PROTOCOL_SETUP log progress of protocol setup 打印 Protocol 的设置过程
OBJC_PRINT_IVAR_SETUP log processing of non-fragile ivars 打印 Ivar 的设置过程
OBJC_PRINT_VTABLE_SETUP log processing of class vtables 打印 vtable 的设置过程
OBJC_PRINT_VTABLE_IMAGES print vtable images showing overridden methods 打印 vtable 被覆盖的方法
OBJC_PRINT_CACHE_SETUP log processing of method caches 打印方法缓存的设置过程
OBJC_PRINT_FUTURE_CLASSES log use of future classes for toll-free bridging 打印从 CFType 无缝转换到 NSObject 将要使用的类(如 CFArrayRef 到 NSArray * )
OBJC_PRINT_GC log some GC operations 打印一些垃圾回收操作
OBJC_PRINT_PREOPTIMIZATION log preoptimization courtesy of dyld shared cache 打印 dyld 共享缓存优化前的问候语
OBJC_PRINT_CXX_CTORS log calls to C++ ctors and dtors for instance variables 打印类实例中的 C++ 对象的构造与析构调用
OBJC_PRINT_EXCEPTIONS log exception handling 打印异常处理
OBJC_PRINT_EXCEPTION_THROW log backtrace of every objc_exception_throw() 打印所有异常抛出时的 Backtrace
OBJC_PRINT_ALT_HANDLERS log processing of exception alt handlers 打印 alt 操作异常处理
OBJC_PRINT_REPLACED_METHODS log methods replaced by category implementations 打印被 Category 替换的方法
OBJC_PRINT_DEPRECATION_WARNINGS warn about calls to deprecated runtime functions 打印所有过时的方法调用
OBJC_PRINT_POOL_HIGHWATER log high-water marks for autorelease pools 打印 autoreleasepool 高水位警告
OBJC_PRINT_CUSTOM_RR log classes with un-optimized custom retain/release methods 打印含有未优化的自定义 retain/release 方法的类
OBJC_PRINT_CUSTOM_AWZ log classes with un-optimized custom allocWithZone methods 打印含有未优化的自定义 allocWithZone 方法的类
OBJC_PRINT_RAW_ISA log classes that require raw pointer isa fields 打印需要访问原始 isa 指针的类
OBJC_DEBUG_UNLOAD warn about poorly-behaving bundles when unloaded 卸载有不良行为的 Bundle 时打印警告
OBJC_DEBUG_FRAGILE_SUPERCLASSES warn about subclasses that may have been broken by subsequent changes to superclasses 当子类可能被对父类的修改破坏时打印警告
OBJC_DEBUG_FINALIZERS warn about classes that implement -dealloc but not -finalize 警告实现了 -dealloc 却没有实现 -finalize 的类
OBJC_DEBUG_NIL_SYNC warn about @synchronized(nil), which does no synchronization 警告 @synchronized(nil) 调用,这种情况不会加锁
OBJC_DEBUG_NONFRAGILE_IVARS capriciously rearrange non-fragile ivars 打印突发地重新布置 non-fragile ivars 的行为
OBJC_DEBUG_ALT_HANDLERS record more info about bad alt handler use 记录更多的 alt 操作错误信息
OBJC_DEBUG_MISSING_POOLS warn about autorelease with no pool in place, which may be a leak 警告没有 pool 的情况下使用 autorelease,可能内存泄漏
OBJC_DEBUG_DUPLICATE_CLASSES halt when multiple classes with the same name are present 当出现类重名时停机
OBJC_USE_INTERNAL_ZONE allocate runtime data in a dedicated malloc zone 在一个专用的 malloc 区分配运行时数据
OBJC_DISABLE_GC force GC OFF, even if the executable wants it on 强行关闭自动垃圾回收,即使可执行文件需要垃圾回收
OBJC_DISABLE_VTABLES disable vtable dispatch 关闭 vtable 分发
OBJC_DISABLE_PREOPTIMIZATION disable preoptimization courtesy of dyld shared cache 关闭 dyld 共享缓存优化前的问候语
OBJC_DISABLE_TAGGED_POINTERS disable tagged pointer optimization of NSNumber et al. 关闭 NSNumber 等的 tagged pointer 优化
OBJC_DISABLE_NONPOINTER_ISA disable non-pointer isa fields 关闭 non-pointer isa 字段的访问

    推荐阅读