c - 使用通过 MacPorts 安装的 GD2 库通过 GCC 编译 C 程序的问题

标签 c gcc compiler-construction gd macports

我已经通过直接从源代码安装的 GD2 库在 Mac 上通过 GCC 成功编译了一个 C 程序。现在,我正在尝试使用通过 MacPorts 安装的 GD2 库来执行此操作,并出现以下错误消息:

plotgeometry.c:5:16: error: gd.h: No such file or directory
plotgeometry.c: In function 'PlotGeometry':
plotgeometry.c:28: error: 'gdImagePtr' undeclared (first use in this function)
plotgeometry.c:28: error: (Each undeclared identifier is reported only once
plotgeometry.c:28: error: for each function it appears in.)
plotgeometry.c:28: error: expected ';' before 'im'
plotgeometry.c:29: warning: ISO C90 forbids mixed declarations and code
plotgeometry.c:748: error: 'im' undeclared (first use in this function)
plotgeometry.c:748: warning: implicit declaration of function 'gdImageCreate'
plotgeometry.c:752: warning: implicit declaration of function 'gdImageColorAllocate'
plotgeometry.c:780: warning: implicit declaration of function 'gdImageSetPixel'
plotgeometry.c:801: warning: implicit declaration of function 'gdImagePng'
plotgeometry.c:809: warning: implicit declaration of function 'gdImageDestroy'

我想,我需要为 GCC 提供 GD2 库的路径。 gd.h 在以下目录中找到

$ find /opt/local/ -name 'gd.h'
/opt/local//include/gd.h
/opt/local//var/macports/software/gd2/2.0.35_7/opt/local/include/gd.h

我已将 /opt/local/include 添加到我的 $PATH 变量中,但它没有帮助。我是否需要将附加参数与该路径一起传递到 GCC? 你能帮我解决这个问题吗?

最佳答案

您不使用 $PATH。通过 -I 命令行选项将其添加到 gcc。对于直接构建:

gcc -I/opt/local/include ...

对于制作:

CPPFLAGS='-I/opt/local/include' make

您还需要在链接时引用库。使用 -L/opt/local/lib -lgd 或通过 make:

CPPFLAGS='-I/opt/local/include' LDFLAGS='-L/opt/local/lib' LDLIBS='-lgd' make

当然,您可以在 Makefile 中设置这些变量。

关于c - 使用通过 MacPorts 安装的 GD2 库通过 GCC 编译 C 程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3299328/

相关文章:

c - C中字符串压缩的ascii输出

c++ - 可以为多个构造函数消除 0 (NULL) 的歧义吗?和赋值运算符?

gcc - 使用 gcc 链接到随 Homebrew 安装的动态库?

delphi - Delphi命令行编译器

compiler-construction - 自学编译器类(class)/好的入门编译器书籍?

c++ - .net 应用程序的 Bootstrap (预安装程序)

c - 如何组合 fputs 而不引发错误

c - 使用指针在函数外部操作数组?

编译因#include "..."而失败,但因#include <...> 而失败

gcc - 在 gcc/g++ 中禁用预测