cgccgdmacports

Problem to compile a C-program by GCC with GD2 library installed via MacPorts


I have successfully compiled a C-program by GCC on Mac with GD2 library installed directly from sources. Now I am trying to do that with GD2 library installed via MacPorts and have the following error message:

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'

I guess, I need to provide the path to GD2 library for GCC. The gd.h is found in the following dirs

$ 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

I have added /opt/local/include to my $PATH variable, but it did't help. Do I need to path an additional parameter with that path to GCC? Could you help me with this?


Solution

  • You don't use $PATH. Add it via the -I command-line option to gcc. For direct builds:

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

    For make:

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

    You'll also need to reference the library while linking. Use -L/opt/local/lib -lgd or, via make:

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

    You can, of course, set these variables in your Makefile.