向 xcode 4 项目添加一个简单的库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6975796/
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
adding a simple library to an xcode 4 project
提问by user813611
I know this is a very simple question but I have been struggling with it for a while. I have read a few threads but still can seem to find the answer.
我知道这是一个非常简单的问题,但我已经为此苦苦挣扎了一段时间。我已经阅读了一些主题,但似乎仍然可以找到答案。
I am trying to add this DDMathParserlibrary to my existing project. The math parser documentation states
我正在尝试将此DDMathParser库添加到我现有的项目中。数学解析器文档说明
"Simply copy the "DDMathParser" subfolder into your project, #import "DDMathParser.h", and you're good to go."
“只需将“DDMathParser”子文件夹复制到您的项目中,#import“DDMathParser.h”,您就可以开始了。”
I added the subfolder to my project and added the header `#import "DDMathParser.h' to my first view controller. At first Xcode was stating that it can't find the file. I took the advice from another thread and changed my header to include the folder
我将子文件夹添加到我的项目中,并将标题 `#import "DDMathParser.h' 添加到我的第一个视图控制器。起初 Xcode 说它找不到文件。我从另一个线程中获取了建议并更改了我的标题包含文件夹
#import "DDMathParser/DDMathParser.h"
Xcode seems to find the folder and header file but when I run a test from the documentation such as
Xcode 似乎找到了文件夹和头文件,但是当我从文档中运行测试时,例如
NSLog(@"%@", [@"1 + 2" numberByEvaluatingString]);
I am getting the error 'Thread 1: Program received sginal: "SIGABRT"' I can see in the DDMathParser sample file that there is a target, I am wondering if this is my problem.
我收到错误 'Thread 1: Program received sginal: "SIGABRT"' 我可以在 DDMathParser 示例文件中看到有一个目标,我想知道这是否是我的问题。
To clarify the question I am trying to add the DDMathParserlibrary to a simple view controller and run the staement NSLog(@"%@", [@"1 + 2" numberByEvaluatingString]);
Any help/clarification in doing so would be greatly appreciated.
为了澄清这个问题,我试图将DDMathParser库添加到一个简单的视图控制器并运行 staement NSLog(@"%@", [@"1 + 2" numberByEvaluatingString]);
任何帮助/澄清这样做将不胜感激。
回答by memmons
First, please do not add the DDMathParser code to your project via a subfolder copy. Doing so will make it a pain to easily grab any future updates to the code and in general is not a very good approach to including external code in your projects.
首先,请不要通过子文件夹副本将 DDMathParser 代码添加到您的项目中。这样做会使轻松获取代码的任何未来更新变得痛苦,并且通常不是在项目中包含外部代码的好方法。
Instead, you should add the git repo as a submodule of your project (you are using git, right?) and import the relevant files to your project. Here's a step-by-step solution:
相反,您应该将 git repo 添加为项目的子模块(您正在使用 git,对吗?)并将相关文件导入到您的项目中。这是一个分步解决方案:
- Add the DDMathParser repo as a submodule to your project. Here are instructionson how to add a submodule in your project directory. Take a look at that answer, but for brevity's sake, you'll issue this command from terminal in your project's root directory:
git submodule add https://github.com/davedelong/DDMathParser.git External/DDMathParser
. This will create a new subdirectory named External to your project root directory and inside External the DDMathParser project will be copied. - Add the DDMathParser directory to your project. It will be found in
<Your Root Project Dir>/External/DDMathParser/DDMathParser
directory. Be sure to check the "add to targets" checkbox and notto check the "copy items" checkbox. - Add
#import "DDMathParser.h"
to your viewcontroller.
- 将 DDMathParser 存储库作为子模块添加到您的项目中。以下是有关如何在项目目录中添加子模块的说明。看看这个问题的答案,但为了简洁起见,你会发出从你的项目的根目录终端命令:
git submodule add https://github.com/davedelong/DDMathParser.git External/DDMathParser
。这将在您的项目根目录中创建一个名为 External 的新子目录,并在 External 内复制 DDMathParser 项目。 - 将 DDMathParser 目录添加到您的项目中。它将在
<Your Root Project Dir>/External/DDMathParser/DDMathParser
目录中找到。请务必选中“添加到目标”复选框,而不是选中“复制项目”复选框。 - 添加
#import "DDMathParser.h"
到您的视图控制器。
DDMathParser should now work as you expect. If the author comes out with an update to the code you can just issue the following command from terminal to pull the latest updates from github: git submodule update
.
DDMathParser 现在应该可以正常工作了。如果作者隆重推出的更新,你可以从终端发出以下命令来拉从GitHub最新的更新的代码:git submodule update
。
Note: I created a new project from scratch, followed these steps and included your NSLog()
example to ensure that there aren't any issues.
注意:我从头开始创建了一个新项目,遵循这些步骤并包含您的NSLog()
示例以确保没有任何问题。
回答by Motti Shneor
I followed these steps too, until I could make it work. The suggested method has some flaws.
我也遵循了这些步骤,直到我可以使它工作。建议的方法有一些缺陷。
As the guy before me had it --- You only bring in the SOURCES of DDMathParser, and compile them as part of your target. This binds you to use the same build-settings, SDK version and deployment system-version as the author of DDMathParser. Examples:
- didn't use ARC in my target --- now I must.
- I intended to use MacOS 10.8 SDK in my project (for many good reasons) but I got strange ARC compilation error in one DDMathParser source, that only disappeared as I switched to SDK 10.9 (version used by author in his samples and docs).
- I MUST provide my application for MacOS 10.8 and above, now I'm unsure it will work
I don't know what happens if I push my parent repo to the server, and another friend pulls it. Will he receive the DDMathParser repo automatically? How? He must have access to it! The scheme does not seem complete to me.
正如我之前的那个人所拥有的 --- 您只需引入 DDMathParser 的 SOURCES,并将它们编译为目标的一部分。这将绑定您使用与 DDMathParser 的作者相同的构建设置、SDK 版本和部署系统版本。例子:
- 没有在我的目标中使用 ARC --- 现在我必须。
- 我打算在我的项目中使用 MacOS 10.8 SDK(出于很多很好的理由),但我在一个 DDMathParser 源中遇到了奇怪的 ARC 编译错误,只有在我切换到 SDK 10.9(作者在他的示例和文档中使用的版本)时才消失。
- 我必须为 MacOS 10.8 及更高版本提供我的应用程序,现在我不确定它是否有效
我不知道如果我将我的父仓库推送到服务器,然后另一个朋友拉取它会发生什么。他会自动收到 DDMathParser 存储库吗?如何?他一定可以访问它!该计划对我来说似乎并不完整。
I will suggest to aid the author in packaging his DDMathParser as a framework, and then you will be able to import the PROJECT FILE of DDMAthParser into your project --- not the sources, and simply link against the product of DDMathParser project (a private framework). Seems much nicer. the framework can be compiled using the author's settings, and we just receive the nicely-packaged binary.
我会建议帮助作者将他的 DDMathParser 打包成一个框架,然后你就可以将 DDMAthParser 的 PROJECT FILE 导入你的项目——而不是源代码,只需链接到 DDMathParser 项目的产品(私人框架)。看起来好多了。该框架可以使用作者的设置进行编译,我们只收到打包好的二进制文件。