2011-06-30 34 views
13

我剛剛發現ncurses並剛剛開始學習它,但是我的教程中的示例不能在我的計算機上編譯。無法編譯使用ncurses的C/C++代碼

我必須手動安裝ncurses,並輸入「apt-get install libncurses5-dev libncursesw5-dev」。我必須這樣做,因爲在這樣做之前,我得到一個錯誤,說我不能「#include」。

安裝它的工作,但現在我得到這個錯誤,而不是:

[email protected]:~/learning_ncurses$ g++ -o hello_world hello_world.cpp 
/tmp/ccubZbvK.o: In function `main': 
hello_world.cpp:(.text+0xa): undefined reference to `initscr' 
hello_world.cpp:(.text+0x16): undefined reference to `printw' 
hello_world.cpp:(.text+0x1b): undefined reference to `refresh' 
hello_world.cpp:(.text+0x20): undefined reference to `stdscr' 
hello_world.cpp:(.text+0x28): undefined reference to `wgetch' 
hello_world.cpp:(.text+0x2d): undefined reference to `endwin' 
collect2: ld returned 1 exit status 

的代碼我編譯如下所示:

#include <ncurses.h> 
int main(){ 
    initscr(); 
    printw("Hai thar world..."); 
    refresh(); 
    getch(); 
    endwin(); 

    return 0; 
} 

爲什麼會出現這種錯誤。更重要的是,我該如何解決這個問題?

回答

30

你有ncurses庫

g++ -o hello_world hello_world.cpp -lncurses 
+0

我很新的C++鏈接所以這個概念是新的我。當我使用未包含在編譯器中的庫時,我是否總是必須這樣做? – Touzen

+3

是的。頭文件允許您的代碼通過將函數聲明與代碼中的調用進行匹配來進行編譯。鏈接器的工作是爲程序中使用的每個函數查找*實際可運行代碼*,這是通過在鏈接時指定封裝庫名稱(以及用於自己的代碼的目標文件)。 –

+0

準確地說 - 您需要鏈接到所有圖書館,甚至是標準圖書館。 –