如何在Windows上安装ncurses

问题描述 投票:0回答:2

如何让 ncurses 在我的 Windows 上运行?我已尝试各种方法使其工作,但我的 gcc 编译器无法识别它,它已正确安装在目录中 C:\msys64\mingw64\include 咒骂 我是通过MSYS2安装的。有人可以帮助我吗?

我尝试按如下方式编译我的代码: gcc test.c -o test -IC:\msys64\mingw64\include 它在编译器中向我返回了错误,我尝试使用另一个命令运行它: gcc test.c -o test -I C:\msys64\mingw64\include -L C:\msys64\mingw64\lib -lncurses 也没有用,我不知道该怎么办。

具体错误: test.c:1:10: 致命错误: ncurses.h: 没有这样的文件或目录 1 | #包括 | ^~~~~~~~~~~ 编译终止。 我正在使用 vscode,但即使在 Windows 终端中也会出现同样的问题。

gcc ncurses
2个回答
3
投票

VS Code 使用的 msys2 MingW64 默认安装,ncurses 基本存在,但正确的包含和链接并不明显。

这是一个非常简单的ncurses-helloworld.cpp:

#include <ncurses/ncurses.h>

int main(int argc, char ** argv)
{
    // init screen and sets up screen
    initscr();
    // print to screen
    printw("Hello World");
    // refreshes the screen
    refresh();
    // pause the screen output
    getch();
    // deallocates memory and ends ncurses
    // endwin();
    return 0;
}

当然,您可以在配置中设置包含路径,例如

vscode
或编译器的命令行或 makefile 或任何地方,以便 a

#include <ncurses.h>

有效,但与此同时,我习惯于只包含子目录中的所有内容

C:\msys64\mingw64\include
形式
#include <directory name/filename.h>
- 对于可以通过
pacman
使用 msys 软件包直接安装的所有内容,这非常容易,无需任何进一步的配置设置。

要使用它正确链接 ncurses 库,请使用以下命令:

g++ test.cpp -o test.exe -lncurses -DNCURSES_STATIC 

gcc test.c -o test.exe -lncurses -DNCURSES_STATIC 

对于纯 C 程序。

Brecht Sanders 在这个问题的评论中给出了解释,这导致当时的OP找到了适当的解决方案:

“除非定义了

__declspec(dllexport)
,否则标头定义
NCURSES_STATIC
,因此添加
-DNCURSES_STATIC
作为编译选项可以解决所有问题。”


0
投票

g++ test.c -o test.exe -lncursesw

这对我有用

我在 Windows 10 上使用 MSYS 安装了 ncurses

mingw-w64-x86_64-ncurses
命令

© www.soinside.com 2019 - 2024. All rights reserved.