如何在 Xcode 4 中链接库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6870589/
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
How do I link libraries in Xcode 4?
提问by Vortico
I'm a complete beginner to Apple's Xcode, but I have followed the Xcode documentation and the advice of a few related questions without success.
我是 Apple Xcode 的完全初学者,但我遵循了 Xcode 文档和一些相关问题的建议,但没有成功。
I installed GMPto /usr/local/bin, wrote a short program using the library, and compiled with gcc main.c -lgmp
. It compiled with no warnings or errors, and the executable worked flawlessly.
我将GMP安装到 /usr/local/bin,使用该库编写了一个短程序,并使用gcc main.c -lgmp
. 它编译时没有警告或错误,并且可执行文件完美无缺。
I started a new Xcode project (Command Line Tool; Type: C), copied the code to the newly created main.c, and opened the project build settings. From there I set Linking > Other Linker Flagsto -lgmp
and Search Paths > Library Search Pathsto /usr/local/bin
. However, the build fails with the preprocessor error "Gmp.h: No such file or directory".
我开始了一个新的Xcode项目(命令行工具;类型:C),将代码复制到新创建的main.c中,并打开项目构建设置。从那里我设置Linking > Other Linker Flagsto-lgmp
和Search Paths > Library Search Pathsto /usr/local/bin
。但是,构建失败并显示预处理器错误“Gmp.h: No such file or directory”。
I have tried almost every header imaginable:
我尝试了几乎所有可以想象的标题:
#include "gmp.h"
#include "gmp.h"
#include <gmp.h>
#include <gmp.h>
#include "gmp"
#include "gmp"
#include "libgmp.a"
. . .
#include "libgmp.a"
. . .
This has been my main obstacle over the last three months which has prevented me from learning C. Any help leading me to an eventual solution would be greatly appreciated.
这一直是我在过去三个月中的主要障碍,它阻止了我学习 C。任何帮助我找到最终解决方案的帮助将不胜感激。
回答by
There's a few things you have to set up in your Xcode project. For example, I have gmp installed in /opt/gmp/5.0.2
and I will use that as an example. The actual library is installed into /opt/gmp/5.0.2/lib
and the header files into /opt/gmp/5.0.2/include
. When installing the library setting the --PREFIXflag to /opt/gmp/5.0.2
would handle this automatically. If you don't set this flag the prefix is usually set to /usr/local
by default.
您必须在 Xcode 项目中设置一些内容。例如,我安装了 gmp /opt/gmp/5.0.2
,我将以此为例。实际的库安装/opt/gmp/5.0.2/lib
到/opt/gmp/5.0.2/include
. 安装库时,将--PREFIX标志设置为/opt/gmp/5.0.2
会自动处理。如果您不设置此标志,则前缀通常/usr/local
默认设置为。
- The Other Linker Flagslooks right, it should be the name of the library.
- Set the Header Search Pathto the include directory, in my case
/opt/gmp/5.0.2/include
. - Set the Library Search Pathto the lib directory, in my case
/opt/gmp/5.0.2/lib
.
- 在其它链接器标记看起来正确的,它应该是库的名称。
- 将标题搜索路径设置为包含目录,在我的情况下为
/opt/gmp/5.0.2/include
. - 将库搜索路径设置为 lib 目录,在我的情况下为
/opt/gmp/5.0.2/lib
.
Since the header search path has been set, you should now be able to include the header file like this:
由于已经设置了头文件搜索路径,您现在应该能够像这样包含头文件:
#include <gmp.h>
Of course, replace /opt/gmp/5.0.2
with the PREFIX path you used when you installed gmp.
当然,替换/opt/gmp/5.0.2
成你安装gmp时使用的PREFIX路径。
Lastly, you typically don't install libraries to /usr/local/bin
, you would install to /usr/local
and let any binaries be installed into bin while libraries like these would be installed into lib. Of course any path scheme would work, I usually recommend /opt/<project-name>/<version-number>
since it allows me to keep better track of what I have installed and have multiple versions of the same libraries and tools without having to deal with collisions.
最后,你通常不会安装库/usr/local/bin
,你会安装到/usr/local
让任何二进制文件安装到仓,而像这些库将被安装到库。当然,任何路径方案都可以使用,我通常会推荐/opt/<project-name>/<version-number>
它,因为它可以让我更好地跟踪我已安装的内容并拥有相同库和工具的多个版本,而无需处理冲突。
回答by piotr_ch
I have updated my system from snow leopardto mountain lionand had to install gmp
.
我已经将我的系统从雪豹更新为山狮,并且必须安装gmp
。
First of all I have installed XcodeCommandLineTools set.
首先我已经安装了XcodeCommandLineTools 集。
Secondly, installed Homebrew. Then with it I have done steps in this topic: https://apple.stackexchange.com/questions/38222/how-do-i-install-gcc-via-homebrew
其次,安装Homebrew。然后我已经完成了本主题中的步骤:https: //apple.stackexchange.com/questions/38222/how-do-i-install-gcc-via-homebrew
In my last step, made changes to an xcode project as colleague Marcus Karlsson told. It's finally working! Very big Thank You :)
在我的最后一步中,正如同事 Marcus Karlsson 所说,对 xcode 项目进行了更改。它终于工作了!非常感谢您:)