将版本号添加到 XCode 命令行实用程序 C++项目

发布于 2024-12-12 16:34:17 字数 272 浏览 0 评论 0原文

我使用 Objective-C 在 XCode 中创建了一个命令行实用程序 C++ 工具。我希望版本号显示在创建的可执行文件的信息中。因此,我在<的当前项目版本字段中添加了版本号1.0.0.0 em>构建设置。但是,当我构建它时,版本号不会添加到创建的“Unix可执行文件”中。

我错过了什么吗? 感谢您的帮助。

I have created a Command Line Utility C++ tool in XCode using Objective-C. I want the version number to be displayed in the Info of the created executable. So I have added the version number 1.0.0.0 in the Current Project Version field in the Build settings. However, when I build it, the version number does not get added to the created 'Unix Executable File'.

Am I missing something?
Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫雨熙 2024-12-19 16:34:17

不幸的是,从版本 3 开始,Xcode 不会将当前项目版本本身嵌入到可执行文件中。我不确定这在 Xcode 4 中是否有所改变。

几年前我遇到了这个问题。事实证明:

  1. Finder 中显示的版本号来自应用程序的 Info.plists。对于应用程序包来说,Info.plist 是一个文件。对于可执行文件,它必须嵌入到 __TEXT 部分中。无论哪种方式,Info.plist 的格式都是相同的。

  2. Xcode 不支持将 Info.plists 嵌入可执行文件的一键式支持,但这是可能的。

  3. Xcode 不会预处理 Info.plist 文件,除非它们要进入应用程序包(例如插入 CURRENT_PROJECT_VERSION 的值)。

我编写了一个解决方案,但它尚未完善,可能并不代表最佳实践。

首先,我创建了一个名为 Info_template.plist 的新库存 Info.plist 文件。我将 CFBundleVersion 设置为 ${CURRENT_PROJECT_VERSION},将 CFBundleShortVersionString 设置为 ${CURRENT_MARKETING_VERSION}

然后,我在构建开始时添加了一个名为“Preprocess Info.plist”的运行脚本阶段,使用 /bin/sh 作为 shell,使用以下脚本:

set -u

if ! [[ ${CURRENT_PROJECT_VERSION:-""} && ${CURRENT_MARKETING_VERSION:-""} ]]; then
    echo "Version numbers are not set" >&2
    exit 1
fi
# Ghetto environment variable expansion, since Xcode does not appear to have built-in expansion for arbitrary files
{ echo "cat <<EOF"
  cat ${SRCROOT}/Info_template.plist
  echo "EOF"
} | sh > ${DERIVED_FILES_DIR}/Info.plist

我添加了 $(DERIVED_FILE_DIR )/Info.plist 到其输出文件。

然后,我打开目标的构建设置,并将其添加到其他链接器标志:

-sectcreate __TEXT __info_plist ${DERIVED_FILE_DIR}/Info.plist

这要求链接器将生成的 Info.plist 文件滚动到可执行文件中。

让我知道这对你有什么用,或者我是否遗漏了任何内容!

Unfortunately, Xcode will not embed the current project version into the executable by itself, as of version 3. I'm not sure if this has changed in Xcode 4.

I ran into this problem a couple of years ago. It turns out that:

  1. Version numbers displayed in the Finder come from applications’ Info.plists. In the case of an application bundle, Info.plist is a file. In the case of an executable, it must be embedded in a __TEXT section. The format of the Info.plist is the same either way.

  2. Xcode does not have one-click support for embedding Info.plists into executables, but it is possible.

  3. Xcode doesn’t preprocess Info.plist files unless they’re going into an application bundle (e.g. to insert the value of CURRENT_PROJECT_VERSION).

I hacked up a solution, but it’s unpolished and probably doesn’t represent best practices.

First, I created a new, stock Info.plist file called Info_template.plist. I set CFBundleVersion to ${CURRENT_PROJECT_VERSION}, and CFBundleShortVersionString to ${CURRENT_MARKETING_VERSION}.

Then, I added a Run Script phase called “Preprocess Info.plist” at the beginning of the build, using /bin/sh as the shell, with this script:

set -u

if ! [[ ${CURRENT_PROJECT_VERSION:-""} && ${CURRENT_MARKETING_VERSION:-""} ]]; then
    echo "Version numbers are not set" >&2
    exit 1
fi
# Ghetto environment variable expansion, since Xcode does not appear to have built-in expansion for arbitrary files
{ echo "cat <<EOF"
  cat ${SRCROOT}/Info_template.plist
  echo "EOF"
} | sh > ${DERIVED_FILES_DIR}/Info.plist

I added $(DERIVED_FILE_DIR)/Info.plist to its output files.

Then, I opened the target’s build settings, and added this to Other Linker Flags:

-sectcreate __TEXT __info_plist ${DERIVED_FILE_DIR}/Info.plist

This asks the linker to roll the generated Info.plist file into the executable.

Let me know how this works for you, or if I left anything out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文