如何在 Xcode 4 中链接库?

发布于 2024-11-27 04:56:24 字数 669 浏览 5 评论 0原文

我是 Apple Xcode 的完全初学者,但我遵循了 Xcode 文档和一些相关问题的建议,但没有成功。

我将 GMP 安装到 /usr/local/bin,使用该库编写了一个简短的程序,并使用 gcc 进行编译main.c -lgmp。它编译时没有警告或错误,并且可执行文件完美运行。

我启动了一个新的 Xcode 项目(命令行工具;类型:C),将代码复制到新创建的 main.c,然后打开项目构建设置。从那里我设置了链接>;其他链接器标志-lgmp搜索路径>库搜索路径/usr/local/bin。但是,构建失败并出现预处理器错误“Gmp.h:没有这样的文件或目录”。

我尝试了几乎所有可以想象到的标头:

#include "gmp.h"

#include

#include "gmp"

<代码>#include“libgmp.a”。 。 。

这是我过去三个月学习 C 语言的主要障碍。任何帮助我找到最终解决方案的帮助都将不胜感激。

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.

I installed GMP to /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.

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 Flags to -lgmp and Search Paths > Library Search Paths to /usr/local/bin. However, the build fails with the preprocessor error "Gmp.h: No such file or directory".

I have tried almost every header imaginable:

#include "gmp.h"

#include <gmp.h>

#include "gmp"

#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.

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

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

发布评论

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

评论(2

你是年少的欢喜 2024-12-04 04:56:24

您必须在 Xcode 项目中设置一些内容。例如,我在 /opt/gmp/5.0.2 中安装了 gmp,我将使用它作为示例。实际的库安装到 /opt/gmp/5.0.2/lib 中,头文件安装到 /opt/gmp/5.0.2/include 中。安装库时,将 --PREFIX 标志设置为 /opt/gmp/5.0.2 将自动处理此问题。如果您不设置此标志,则前缀通常默认设置为 /usr/local

  1. 其他链接器标志看起来是正确的,它应该是库的名称。
  2. 标题搜索路径设置为包含目录,在我的例子中为/opt/gmp/5.0.2/include
  3. 库搜索路径设置为lib目录,在我的例子中为/opt/gmp/5.0.2/lib

由于标头搜索路径已经设置,您现在应该能够像这样包含头文件:

#include <gmp.h>

当然,将 /opt/gmp/5.0.2 替换为安装时使用的 PREFIX 路径GMP。

最后,您通常不会将库安装到 /usr/local/bin,而是安装到 /usr/local 并让任何二进制文件安装到 bin 中,而像这样的库这些将被安装到 lib 中。当然,任何路径方案都可以,我通常推荐 /opt// 因为它允许我更好地跟踪我已安装的内容并拥有多个相同库和工具的版本,而不必处理冲突。

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 --PREFIX flag 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.

  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.
  3. Set the Library Search Path to the lib directory, in my case /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.

Lastly, you typically don't install libraries to /usr/local/bin, you would install to /usr/localand 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.

坠似风落 2024-12-04 04:56:24

我已将系统从雪豹更新为山狮,并且必须安装gmp

首先,我已安装 Xcode CommandLineTools 设置。

其次,安装Homebrew。然后我就完成了本主题中的步骤: https://apple .stackexchange.com/questions/38222/how-do-i-install-gcc-via-homebrew

在我的最后一步中,按照同事 Marcus Karlsson 的说法对 xcode 项目进行了更改。
终于可以工作了!非常大谢谢:)

I have updated my system from snow leopard to mountain lion and had to install gmp.

First of all I have installed Xcode CommandLineTools set.

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

In my last step, made changes to an xcode project as colleague Marcus Karlsson told.
It's finally working! Very big Thank You :)

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