【问题标题】:Problem to compile a C-program by GCC with GD2 library installed via MacPorts使用通过 MacPorts 安装的 GD2 库的 GCC 编译 C 程序的问题
【发布时间】:2010-07-21 12:45:36
【问题描述】:

我已经通过 GCC 在 Mac 上成功编译了一个 C 程序,并直接从源代码安装了 GD2 库。现在我正在尝试使用通过 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 的附加参数? 你能帮我解决这个问题吗?

【问题讨论】:

    标签: c gcc compiler-construction gd macports


    【解决方案1】:

    您不使用 $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 中设置这些变量。

    【讨论】:

    • 谢谢,马塞洛!如果我将CFLAGS += '-I/opt/local/include' 添加到Makefile,那么我将编译plotgeometry.c。但是在采集过程中出现问题:ld: library not found for -lgd
    • /opt/local/lib中是否有一系列libgd*文件?您是否还添加了LDFLAGS 前缀? (根据错误信息,您可能不需要LDLIBS。)
    【解决方案2】:

    您需要将-I/opt/local/include 添加到编译器参数(而不是$PATH,它仅用于shell 查找可执行文件)。

    【讨论】:

      猜你喜欢
      • 2013-01-18
      • 2014-08-15
      • 1970-01-01
      • 2021-03-01
      • 2023-03-31
      • 2015-12-18
      • 2012-08-06
      • 1970-01-01
      • 2012-01-11
      相关资源
      最近更新 更多