简体   繁体   English

如何链接Xcode 4中的库?

[英]How do I link libraries in Xcode 4?

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 GMP to /usr/local/bin, wrote a short program using the library, and compiled with gcc main.c -lgmp . 我在/ usr / local / bin中安装了GMP ,使用该库编写了一个简短的程序,并使用gcc main.c -lgmp编译。 It compiled with no warnings or errors, and the executable worked flawlessly. 它编译时没有任何警告或错误,并且可执行文件完美无缺。

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. 我启动了一个新的Xcode项目(命令行工具;类型:C),将代码复制到新创建的main.c,并打开项目构建设置。 From there I set Linking > Other Linker Flags to -lgmp and Search Paths > Library Search Paths to /usr/local/bin . 从那里我将链接>其他链接器标志设置-lgmp并将搜索路径>库搜索路径设置/usr/local/bin However, the build fails with the preprocessor error "Gmp.h: No such file or directory". 但是,构建失败,预处理程序错误“Gmp.h:没有这样的文件或目录”。

I have tried almost every header imaginable: 我已经尝试了几乎所有可以想象的标题:

#include "gmp.h"

#include <gmp.h>

#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.任何帮助我获得最终解决方案的帮助将不胜感激。

There's a few things you have to set up in your Xcode project. 您必须在Xcode项目中设置一些内容。 For example, I have gmp installed in /opt/gmp/5.0.2 and I will use that as an example. 例如,我在/opt/gmp/5.0.2安装了gmp,我将以此为例。 The actual library is installed into /opt/gmp/5.0.2/lib and the header files into /opt/gmp/5.0.2/include . 实际的库安装在/opt/gmp/5.0.2/lib ,头文件安装到/opt/gmp/5.0.2/include When installing the library setting the --PREFIX flag to /opt/gmp/5.0.2 would handle this automatically. 安装库设置时,将--PREFIX标志设置为/opt/gmp/5.0.2将自动处理。 If you don't set this flag the prefix is usually set to /usr/local by default. 如果未设置此标志,则前缀通常默认设置为/usr/local

  1. The Other Linker Flags looks right, it should be the name of the library. 其他链接器标志看起来正确,它应该是库的名称。
  2. Set the Header Search Path to the include directory, in my case /opt/gmp/5.0.2/include . 标题搜索路径设置为包含目录,在我的案例中为/opt/gmp/5.0.2/include
  3. Set the Library Search Path to the lib directory, in my case /opt/gmp/5.0.2/lib . 库搜索路径设置为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. 最后,您通常不会将库安装到/usr/local/bin ,您可以安装到/usr/local并将任何二进制文件安装到bin中,而像这样的库将安装到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. 当然任何路径方案都可以使用,我通常建议使用/opt/<project-name>/<version-number>因为它可以让我更好地跟踪我已经安装的内容并拥有相同库和工具的多个版本而无需处理碰撞。

I have updated my system from snow leopard to mountain lion and had to install gmp . 我已将我的系统从雪豹更新到山狮,并且必须安装gmp

First of all I have installed Xcode CommandLineTools set. 首先我安装了Xcode CommandLineTools。

Secondly, installed Homebrew . 其次,安装了Homebrew Then with it I have done steps in this topic: https://apple.stackexchange.com/questions/38222/how-do-i-install-gcc-via-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. 在我的最后一步,正如同事Marcus Karlsson所说,对xcode项目进行了更改。 It's finally working! 它终于奏效了! Very big Thank You :) 非常感谢你:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM