【问题标题】:Ncurses is included on Cygwin, but getch() doesn't work. Says it's an undeclared identifierNcurses 包含在 Cygwin 中,但 getch() 不起作用。说这是一个未声明的标识符
【发布时间】:2020-01-01 03:45:59
【问题描述】:

我正在使用 Cygwin,并且正在尝试编写游戏培训师。我希望能够让用户只需按一个键即可启用/禁用功能,而不必在之后按 Enter。似乎 ncurses 库中的 getch() 应该能够做到这一点。但是,当我尝试使用 getch() 时,我收到错误“使用未定义的标识符 'getch'”。然后,构建输出还会显示对 ncwrap_stdscr 和 wgetch 的未定义引用。

我尝试包含 curses.h 而不是 ncurses,但这会产生相同的错误(这是有道理的,因为 ncurses 头文件似乎只是一个指向 curses.h 的符号链接)。

理论上,我认为这段代码应该构建得很好:

#include <iostream>
#include <ncurses.h>

int main() {
    int in = getch();
    std::cout << in << std::endl;
}

这是构建的输出:

CMakeFiles/getchTest.dir/main.cpp.o: In function `main':
/cygdrive/c/Users/Sasschary/CLionProjects/getchTest/main.cpp:5: undefined reference to `ncwrap_stdscr'
/cygdrive/c/Users/Sasschary/CLionProjects/getchTest/main.cpp:5: undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/getchTest.dir/build.make:84: getchTest.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/getchTest.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/getchTest.dir/rule] Error 2
make: *** [Makefile:118: getchTest] Error 2

提前感谢您的帮助!

【问题讨论】:

标签: c++ ncurses


【解决方案1】:

我需要在 ncurses 库中有 cmake 链接。解决方案是添加

target_link_libraries(BasicTrainer ncurses)

到我的 cmake 文件。谢谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-06
    相关资源
    最近更新 更多