我有一个 Xcode 静态库项目,如何向其中添加测试目标以便可以在那里运行它? (而不是在链接到它的项目中。)

发布于 2024-09-05 08:17:58 字数 565 浏览 12 评论 0原文

我希望能够在库目标中测试库代码,这样我就不必切换到单独的项目来运行它。

我了解如何添加目标,但不确定如何将其设置为像“命令行工具”项目模板一样运行。我尝试添加一个新的“Shell Tool”目标,但我不知道如何使其像目标一样运行。我必须向该目标添加哪些构建设置?我需要什么文件(main.m?)来启动它?

编辑(添加了我在下面的评论,希望使问题更容易回答。)

我的库有两个目标,一个用于 iOS,一个用于 MacOS。到目前为止我只将它包含在一个 iOS 项目中,所以这就是我目前想要使用的项目。我尝试添加 Cocoa Touch 单元测试捆绑包目标,但我的构建失败,并且命令 /bin/sh/ 失败,退出代码为 1,并且架构“i386”测试失败(GC 关闭)。我不认为我想在 i386 上运行(至少不想测试我的 iOS 目标)并且我在目标设置中找不到任何显示 i386 的内容。

我看到该项目的活动架构是 i386(在“项目”>“设置活动架构”菜单中),但它不允许我在那里更改它。我的 TestApp 目标将标准(armv6)作为体系结构集,我尝试搜索设置,但没有任何内容包含 i386。我该如何改变这一点?

I want to be able to test library code in the library target so I don't have to switch over to a separate project to run it.

I see how to add a target, but I'm not sure how to set it up to run like the "Command Line Tool" project template does. I tried adding a new "Shell Tool" target, but I don't know how to make it run like one. What build settings do I have to add to that target? What files (main.m?) do I need to start it up?

EDIT (Added my comments from below to hopefully make the question more answerable.)

My library has two targets, one for iOS and one for MacOS. I'm only including it in an iOS project so far, so that's the one I want work with presently. I tried adding a Cocoa Touch Unit Test Bundle target, but my build fails and I get command /bin/sh/ failed with exit code 1 and Failed tests for architecture 'i386' (GC OFF). I don't think I want to run on i386 (at least not for a test of my iOS target) and I can't find anything that says i386 in the target settings.

I see that my active architecture for the project is i386 (in the Project > Set Active Architecture menu), but it doesn't let me change it there. My TestApp target has Standard (armv6) as the architecture set and I tried searching the settings and nothing has i386 in it. How do I change that?

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

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

发布评论

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

评论(4

流绪微梦 2024-09-12 08:17:59

Mac OS X SDK 中有一个单元测试捆绑目标的模板。

请注意,“捆绑包”是测试目标的构建产品:目标生成一个单元测试捆绑包。它不要求您的实际产品(库、应用程序等)是捆绑包;事实上,您的实际产品是什么并不重要。

也就是说,当您有静态库时,您可以采取一些不同的做法:

  1. 右键单击组树中的任何内容。从“添加”子菜单中,选择“新目标”。
  2. 在 Mac OS X 标题下,选择 Cocoa。在模板列表中向下滚动并选择单元测试包。单击“确定”。
  3. 命名您的单元测试包目标。
  4. 将您的库构建产品(来自“产品”组)添加到单元测试捆绑目标的“链接二进制文件与库”阶段。
  5. 在任何构建阶段之外,将您的库目标(从“目标”组)添加到您的单元测试捆绑包目标。这使其成为依赖项,以便测试捆绑包目标将在必要时重建库。
  6. 将您的活动目标设置为测试包目标。

通常,您只需将被测代码的源代码文件添加到库目标和测试包目标中,但这样做也会测试您将测试包链接到库的能力。如果库存在任何阻止链接的问题,这可能会检测到它们。

There's a template for a unit-test bundle target in the Mac OS X SDK.

Note that the “bundle” is the build product of the test target: The target produces a unit-test bundle. It does not require that your actual product (library, app, etc.) be a bundle; in fact, it doesn't matter what your actual product is.

That said, you can do it a little differently when you have a static library:

  1. Right-click on anything in your group tree. From the “Add” submenu, choose “New Target”.
  2. Under the Mac OS X header, choose Cocoa. Scroll down in the list of templates and choose Unit Test Bundle. Click OK.
  3. Name your unit-test bundle target.
  4. Add your library build product (from the Products group) to your unit-test bundle target's Link Binary with Libraries phase.
  5. Add your library target (from the Targets group) to your unit-test bundle target, outside of any build phase. This makes it a dependency, so that the test bundle target will rebuild the library when necessary.
  6. Set your active target to be the test bundle target.

Normally, you would just add the source code files for your code under test to both the library target and the test bundle target, but doing things this way also tests your ability to link the test bundle against the library. If the library has any problems that would prevent linking against it, this may detect them.

水中月 2024-09-12 08:17:59

您可以更改活动的可执行文件吗? (它位于 Xcode 中的“项目”菜单下:设置活动可执行文件)

您的 Shell 工具目标在其构建文件之一中是否包含 int main(int argc, char* argv[]) 函数?

Can you change the Active Executable? (it is under the Project menu in Xcode: Set Active Executable)

Does your Shell Tool target contain the int main(int argc, char* argv[]) function in one of its built files?

妞丶爷亲个 2024-09-12 08:17:59

对于 Mac,您可以添加新目标。选择命令行工具模板。

For Mac, you can add a new target. Choose the command-line tool template.

坚持沉默 2024-09-12 08:17:59

在 XCode 9.1 中:

  • 单击 File ->新->目标...
  • 选择MacOS 单元测试包

In XCode 9.1:

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