我的第一个Clang插件
编写插件
文件夹结构标记:
插件加载逻辑定义
//在CMakeLists.txt文件最后 add_clang_subdirectory(AnnotationPlugin)
插件目录包含源码和配置文件
插件编译配置文件
插件源码
重新编译
cmake -G Xcode ../llvm && open LLVM.xcodeproj
选择target 插件名称 编译,完成后会生成动态库文件
使用插件
在命令行中使用
新建测试文件test.m
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
@implementation ViewController
- (instancetype)init
{
if(self = [super init]){
}
return self;
}
@end
将 test.m 和 AnnotationPlugin.dylib 放在llvm_xcode同级目录目录然后执行
-isysroot ${SDKROOT} -Xclang -load -Xclang ${CC}/PeregrinePlugin.dylib -Xclang -add-plugin -Xclang PeregrinePlugin
输出文件 test.o 表示编译成功
在 Xcode 10 中使用插件
关闭Bitcode
设置编译器
在Build Settings栏目中新增两项用户定义的设置
CC
、CXX
如果👆的步骤都确认无误之后,在编译的时候如果遇到了下图这种错误
则可以在Build Settings栏目中搜索index,将Enable Index-Wihle-Building Functionality的Default改为NO。
加载插件
在Xcode项目中指定加载插件动态库:Build Settings > Other C Flags
-Xclang -load -Xclang /Users/bonana/Github/ClangPlugin/llvm_xcode/Debug/lib/AnnotationPlugin.dylib -Xclang -add-plugin -Xclang AnnotationPlugin
编译项目后,即可看到插件输入的错误提示如下所示:
最终效果
Last updated
Was this helpful?