2010-07-21 97 views
0

我已經成功編譯了一個由GCC在Mac上編譯的C程序,其中GD2庫直接從源代碼安裝。現在我要做的是通過使用MacPorts安裝GD2庫,並有以下錯誤信息: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' 

我想,我需要提供路徑GD2庫GCC。該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? 你能幫我解決嗎?

回答

4

您不使用$ PATH。通過-I命令行選項將其添加到gcc。對於直接構建:

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

對於化妝:

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

您還需要引用庫的同時連接。使用-L/opt/local/lib -lgd或通過化妝:

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

你可以,當然,在你的Makefile中設置這些變量。

+0

謝謝,馬塞洛!如果我將'CFLAGS + ='-I/opt/local/include''添加到'Makefile',然後編譯'plotgeometry.c'。但是在收集過程中有一個問題:'ld:library找不到-lgd' – Andrei 2010-07-21 13:13:48

+1

'/ opt/local/lib'中是否有一系列'libgd *'文件?你還添加了'LDFLAGS'前綴嗎? (根據錯誤信息,你可能不需要'LDLIBS'。) – 2010-07-21 13:16:29

+0

好吧,對不起,它在我閱讀所有答案後有效! – Andrei 2010-07-21 13:17:11

1

您需要將-I/opt/local/include添加到編譯器參數中(而不是$ PATH,它僅被shell用於查找可執行文件)。