C++ 无法编译C/C++;代码为';s使用ncurses

C++ 无法编译C/C++;代码为';s使用ncurses,c++,c,compiler-errors,ncurses,C++,C,Compiler Errors,Ncurses,我刚刚发现了ncurses,并开始学习它,但是我的教程中的示例并没有在我的计算机上编译 我必须手动安装ncurses,并通过输入“apt get install libncurses5 dev libncursesw5 dev”来完成安装。我不得不这样做,因为在这样做之前,我犯了一个错误,说我不能“包括” 安装成功了,但现在我遇到了以下错误: touzen@comp:~/learning_ncurses$ g++ -o hello_world hello_world.cpp /tmp/ccubZ

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

我必须手动安装ncurses,并通过输入“apt get install libncurses5 dev libncursesw5 dev”来完成安装。我不得不这样做,因为在这样做之前,我犯了一个错误,说我不能“包括”

安装成功了,但现在我遇到了以下错误:

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
我编译的代码如下所示:

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

    return 0;
}
#包括
int main(){
initscr();
printw(“海塔尔世界…”);
刷新();
getch();
endwin();
返回0;
}

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

您必须链接ncurses库

g++ -o hello_world hello_world.cpp -lncurses

我对C++很陌生,所以这个概念对我来说是新的。每当我使用编译器中不包含的库时,我总是必须这样做吗?是的。头文件允许通过将函数声明与代码中的调用相匹配来编译代码。链接器的工作是为程序中使用的每个函数找到实际的可运行代码,这是通过在链接时指定封装库名称(以及您自己代码的目标文件)实现的。确切地说,您需要链接到所有库,甚至是标准的。到目前为止,你还没有意识到这一点的唯一原因是GCC自动链接
libc
(除非你告诉它不要使用-ffreestanding)与OP有完全相同的问题,但有点不同:我在学习非常基本的关于ncurses的hello world教程,教程解释了如何编译,像这样:
g++-lncurses helloworld.cpp
给出了与OP相同的错误。所以我的问题是我需要解释一下,
-lncurses
必须在编译命令的末尾。