如何在 Xcode 4 中链接库?
我是 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在 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
。/opt/gmp/5.0.2/include
。/opt/gmp/5.0.2/lib
。由于标头搜索路径已经设置,您现在应该能够像这样包含头文件:
当然,将
/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./opt/gmp/5.0.2/include
./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:
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/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.我已将系统从雪豹更新为山狮,并且必须安装
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 :)