views:

23

answers:

2

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?

+3  A: 

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.

Marcelo Cantos
Thanks, Marcelo! If I add `CFLAGS += '-I/opt/local/include'` to `Makefile`, then I compiles `plotgeometry.c`. However there is a problem during collecting: `ld: library not found for -lgd`
Andrey
Is there a series of `libgd*` files in `/opt/local/lib`? Did you also add the `LDFLAGS` prefix? (Based on the error message, you probably don't need `LDLIBS`.)
Marcelo Cantos
Ok, sorry, it works after I read all your answer!
Andrey
+1  A: 

You need to add -I/opt/local/include to compiler arguments (and not $PATH which is only used by shell to find executables).

Piotr Kalinowski