Not able to compile C/C++ code that's using ncurses [duplicate]

时间:2022-07-16 09:44:28

This question already has an answer here:

这个问题在这里已有答案:

I've just discovered ncurses and have just started learning it, however the examples in my tutorial don't compile on my computer.

我刚刚发现了ncurses并且刚开始学习它,但是我的教程中的示例不能在我的计算机上编译。

I had to install ncurses manually and did so by entering "apt-get install libncurses5-dev libncursesw5-dev". I had to do this because before having done so I got an error saying that I could not "#include ".

我必须手动安装ncurses,并输入“apt-get install libncurses5-dev libncursesw5-dev”。我不得不这样做,因为在这样做之前我得到一个错误,说我不能“#include”。

Installing it worked, but now I get this error instead:

安装它工作,但现在我得到此错误:

touzen@comp:~/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

The code I compiled looks like this:

我编译的代码如下所示:

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

    return 0;
}

Why do I get this error. And even more importantly, how do I fix this?

为什么我会收到此错误。更重要的是,我该如何解决这个问题呢?

1 个解决方案

#1


30  

you have to link the ncurses library

你必须链接ncurses库

g++ -o hello_world hello_world.cpp -lncurses

#1


30  

you have to link the ncurses library

你必须链接ncurses库

g++ -o hello_world hello_world.cpp -lncurses