【发布时间】: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
提前感谢您的帮助!
【问题讨论】:
-
显示的编译器输出未提及任何“未声明的标识符”。 “undefined reference”是一个 linker 错误,与包含等无关。您是否在链接器调用中添加了
-lncurses链接器标志? -
我已将 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lncurses") 添加到我的 cmake 文件中,但问题仍然存在。当我将鼠标悬停在 getch() 上时,未定义的引用错误显示为工具提示。
-
Welp,我想我需要了解更多关于 CMAKE 等的信息。通过使用正确的解决方案来解决,该解决方案使用 target_link_libraries 而不是链接器标志。