2012-01-24 87 views
-2

我試圖使用"pngwriter"來製作png文件。我使用Ubuntu作爲我的操作系統。我把這些文件放在/ home/ediamin/png /文件夾中 1.png.h,2.pngconf.h,3.pngwriter.h,4.test.cpp,5.zconf.h,6.zlib .H如何使用pngwriter編譯C++程序

我TEST.CPP包含此代碼

#include<iostream> 
#include "pngwriter.h" 
using namespace std; 

int main() { 
int i; 
int y; 

pngwriter png(300,300,0,"test.png"); 

for(i = 1; i < 300;i++) { 
    y = 150+100*sin((double)i*9/300.0); 
    png.plot(i,y, 0.0, 0.0, 1.0); 
} 

png.close(); 

return 0; 
} 

現在我想用這個命令來編譯,

g++ -o test test.cpp -DNO_FREETYPE 

,它給了我下面的錯誤,

/tmp/ccLr9Szo.o: In function `main': 
test.cpp:(.text+0x30): undefined reference to `pngwriter::pngwriter(int, int, double, char const*)' 
test.cpp:(.text+0x72): undefined reference to `pngwriter::filledcircle(int, int, int, double, double, double)' 
test.cpp:(.text+0x7e): undefined reference to `pngwriter::close()' 
test.cpp:(.text+0x8f): undefined reference to `pngwriter::~pngwriter()' 
test.cpp:(.text+0xa4): undefined reference to `pngwriter::~pngwriter()' 
collect2: ld returned 1 exit status 

我該怎麼辦?請注意,我不想安裝任何庫文件,這就是爲什麼我將頭文件放在同一個文件夾中的原因。

+1

標題只包含這些函數的聲明。您必須鏈接到包含這些函數定義的庫('.a'或'.so'文件)。 – hmjd

+1

你的代碼似乎已經在編譯。鏈接失敗,這意味着你沒有提供pngwriter到鏈接器的實現。如果你堅持不使用庫,你可能(至少)需要至少編譯pngwriter源代碼,並鏈接到生成的目標文件。 –

+0

除了您正在編譯的問題之外,您可能要查找的內容被稱爲[靜態鏈接](http://en.wikipedia.org/wiki/Static_library),它將所有已編譯的代碼包括到單個可執行文件中。 –

回答

0

如果您不想將庫包含在編譯中,則必須包含所有進入庫的源文件。