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

Deadly 提交于 2021-02-11 12:50:08

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/3299328/problem-to-compile-a-c-program-by-gcc-with-gd2-library-installed-via-macports

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!