在 Xcode 中使用 bash 编译标志 [英] Compilation flags using bash in Xcode

查看:48
本文介绍了在 Xcode 中使用 bash 编译标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Xcode 中传递一个编译器宏定义,该定义依赖于一个简单的系统 shell 命令.

I am trying to pass in a compiler macro definition in Xcode that relies on a simple system shell command.

我尝试的第一件事是将其添加到 Build Phases->Compile Sources 中的特定文件中,如下所示:

The first thing I tried was to add it to the specific file in Build Phases->Compile Sources like this:

令我惊讶的是这不起作用,所以我尝试了这个:

to my surprise that didn't work, so I tried this:

这正确打印了值,但宏没有正确传递到源文件.我认为这个不起作用,因为它创建了一个单独的 shell 实例,因此环境变量不会保留到编译阶段.

This printed out the value correctly but the macro wasn't passed to the source file correctly. I figure this one didn't work because it creates a separate shell instance and thus the environment variable is not persisted to the compilation phase.

我尝试进行谷歌搜索并环顾 StackOverflow,我发现唯一有希望的事情是这个,它似乎没有一个可行的解决方案:XCode C/C++ 标志 Bash 脚本

I tried doing a Google search and looking around StackOverflow as well, and the only promising thing I found was this, which didn't seem to have a working solution: XCode C/C++ Flags Bash Script

我写的代码是这样的:

#define STRINGIFY(X) #X
#define TOSTRING(X) STRINGIFY(X)
#define AT __FILE__ ":" TOSTRING(__LINE__)

NSString* getBuildHash()
{
    return [NSString stringWithCString:TOSTRING(GIT_COMMIT_HASH) encoding:NSASCIIStringEncoding];
}

任何帮助将不胜感激:)谢谢!

Any help would be greatly appreciated :) Thanks!

推荐答案

所以,感谢 https://therealbnut.wordpress.com/2012/01/01/setting-xcode-4-0-environment-variables-from-a-script/ 我能够解决它!

So, thanks to https://therealbnut.wordpress.com/2012/01/01/setting-xcode-4-0-environment-variables-from-a-script/ I was able to solve it!

将 Xcode 6 的简单步骤放在这里,以防其他人遇到此问题...

Putting the simple steps for Xcode 6 here in case anyone else runs into this...

点击目标,然后在构建阶段下点击加号下的新运行脚本阶段.

Click on the target and under Build Phases click on the New Run Script Phase under the plus sign.

我使用了以下脚本:

LLVM_XCCONFIG="llvm.xcconfig"
BUILD_INFO="-DGIT_COMMIT_HASH="`git rev-parse --short HEAD`
echo "// Configuration file for LLVM settings, generated as a build phase." > $LLVM_XCCONFIG
echo "BUILD_INFO = $BUILD_INFO" >> $LLVM_XCCONFIG

# Force clang to rebuild the file
touch libUtils/BuildInfo.m

第 2 步:

建造它!产品->构建这会在项目目录中生成一个 llvm.xcconfig 文件:)

Step 2:

Build it! Product->Build This generates a llvm.xcconfig file in the project directory :)

通过右键单击目标并点击将文件添加到"

Add the .xcconfig to the project by right clicking on the target and hitting Add Files to ""

在目标设置中,在构建阶段窗格下,双击编译源下的相应源并添加$(BUILD_INFO) 用于文件的编译器标志.

In the target settings, under the Build Phases pane, double click on the appropriate source under Compile Sources and add in $(BUILD_INFO) for the file's compiler flags.

转到项目设置(不是目标设置!).它应该会将您带到信息 窗格.在Configurations 下设置DebugRelease 以使用llvm"配置.

Go to the project settings (not the target settings!). It should bring you to the Info pane. Under Configurations set Debug and Release to use the "llvm" configuration.

在此菜单下,您可能还需要移动 llvm.xcconfig 复制捆绑资源 目标.

Under this menu, you may also have to move the llvm.xcconfig Copy Bundle Resources target.

你完成了!!尝试构建并确保一切正常:)

You're done!! Try building and make sure it all works :)

这篇关于在 Xcode 中使用 bash 编译标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆