无法编译使用 ncurses 的 C/C++ 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6538059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:20:08  来源:igfitidea点击:

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

c++ccompiler-errorsncurses

提问by Touzen

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?

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

回答by Karoly Horvath

you have to link the ncurses library

你必须链接 ncurses 库

g++ -o hello_world hello_world.cpp -lncurses