如何为 Xcode 项目指定额外的 clang 选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20116475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:19:48  来源:igfitidea点击:

How can I specify additional clang options for Xcode project?

iosxcodepluginscompiler-constructionclang

提问by BergP

I have created my custom clangplugin with help of this tutorialand I want to run it automatically on my Xcode iOS project.

我在本教程的clang帮助下创建了我的自定义插件, 我想在我的 Xcode iOS 项目上自动运行它。

I need to run following additional commands on clang,

我需要运行以下附加命令clang

-Xclang -load \
-Xclang ~/static_analysis/llvm/Debug+Asserts/lib/libPluginExample.so \
-Xclang -plugin -Xclang -example-plugin \

I would like to save all other commands generated by Xcode, because it is difficult to create and pass those commands for every Xcode project. That is the reason why I choose to use clangplugin but not clangtool.

我想保存 Xcode 生成的所有其他命令,因为很难为每个 Xcode 项目创建和传递这些命令。这就是我选择使用clang插件而不是clang工具的原因。

How can I do achieve this?

我怎样才能做到这一点?

Or how can I extract compiler flags generated by xcode aoutomtically, to use them in clang tool? (becouse, for correct using tool I need to specify all include directories, and all sources, and all frameworks)

或者如何自动提取由 xcode 生成的编译器标志,以在 clang 工具中使用它们?(因为,为了正确使用工具,我需要指定所有包含目录、所有源和所有框架)

Update:

更新:

I have added thous commands in Project

我在 Project 中添加了 thous 命令

Settings -> Build Phases -> Compile Sources (double click on source)

设置 -> 构建阶段 -> 编译源(双击源)

, but in compile time there is error (plugin is standard example libPrintFunctionNames.dylib from clang sources):

,但在编译时出现错误(插件是来自 clang 源的标准示例 libPrintFunctionNames.dylib):

error: unable to load plugin '/Users/...llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib': 'dlopen(/Users/.../llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib, 9): Symbol not found: __ZN5clang11ASTConsumer21HandleInterestingDeclENS_12DeclGroupRefE Referenced from: /Users/.../llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib Expected in: flat namespace in /Users/.../llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib' Command /Applications/Xcode 2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1

错误:无法加载插件 '/Users/...llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib': 'dlopen(/Users/.../llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib, 9) :找不到符号:__ZN5clang11ASTConsumer21HandleInterestingDeclENS_12DeclGroupRefE 引用自:/Users/.../llvm/Debug+Asserts/lib/libPrintFunctionNames.dylib 预期在:/Users/.../llvm/Debug+Asserts/libNames.libPrintFunction 中的平面命名空间dylib' 命令 /Applications/Xcode 2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang 失败,退出代码为 1

I have tried to use libPrintFunctionNames.ainstead of libPrintFunctionNames.dylib, but it doe not help.

我曾尝试使用libPrintFunctionNames.a而不是libPrintFunctionNames.dylib,但它没有帮助。

Maybe the cause is that I built my plugin on separated source files of llvm and clang, and in xcode I use other version of clang. I will check that.

也许原因是我在 llvm 和 clang 的单独源文件上构建了我的插件,而在 xcode 中我使用了其他版本的 clang。我会检查的。

回答by Volodymyr Sapsai

I would specify additional Clang options in build option OTHER_CFLAGS. You can do so in

我会在构建选项 OTHER_CFLAGS 中指定额外的 Clang 选项。你可以这样做

target/project Build Settings -> Apple LLVM 5.0 - Custom Compiler Flags -> Other C Flags

Or you can specify OTHER_CFLAGS for xcodebuild, for example,

或者,您可以为 指定 OTHER_CFLAGS xcodebuild,例如,

xcodebuild -scheme SampleProject build OTHER_CFLAGS="-Xclang -load -Xclang /path/to/libPrintFunctionNames.dylib -Xclang -plugin -Xclang print-fns"

xcodebuildis convenient when you don't want to maintain 2 targets which differ only in OTHER_CFLAGS.

xcodebuild当您不想维护仅在 OTHER_CFLAGS 中不同的 2 个目标时,这很方便。

But you're right, it looks like you really need to link against the same libraries as clang itself is linked. At least I've downloaded Clang+LLVM 3.3 binaries from http://llvm.org/releases/download.html, built the plugin with downloaded libraries and it works with clang from http://llvm.org, but doesn't work with clang from Xcode - I encounter the following error:

但是你是对的,看起来你真的需要链接到与 clang 本身链接的相同的库。至少我已经从http://llvm.org/releases/download.html下载了 Clang+LLVM 3.3 二进制文件,使用下载的库构建了插件,并且它与来自http://llvm.org 的clang 一起工作,但没有从 Xcode 使用 clang - 我遇到以下错误:

error: unable to find plugin 'print-fns'

I've created Xcode workspace which builds Clang plugin and shows how you can try to use it with default iOS application. You can find it at https://github.com/vsapsai/ClangPluginExample

我已经创建了 Xcode 工作区,它构建了 Clang 插件并展示了如何尝试将它与默认的 iOS 应用程序一起使用。你可以在https://github.com/vsapsai/ClangPluginExample找到它