辛亚平的博客专栏

在Windows平台用Visual C++ 2022 (v143)编译PDCurses

综述

PDCurses是一个开源的跨平台curses库,它提供了一组函数,开发者可以用这组函数在终端(Console、Terminal)上设置光标的位置和字符的显示样式。本文介绍在Windows平台(Windows 10)上用Visual Studio 2022版本的Visual C++编译PDCurses的过程。

获取PDCurses源代码

PDCurses的相关资源如下:

  • 官方网站:https://pdcurses.org/
  • Github代码仓库页面:https://github.com/wmcbrine/PDCurses
  • Git代码仓库地址:https://github.com/wmcbrine/PDCurses.git
  • SourceForge页面:https://sourceforge.net/projects/pdcurses
  • 文档:https://pdcurses.org/docs/

当前最新稳定版为3.9版(2019年9月5日发布)。下载名称为 3.9 的Tag压缩文件并解压,即可得到3.9版的PDCurses源代码。进入wincon目录,Makefile.vc 即用于Visual C++编译的脚本。

编译过程

X86平台编译

看了 sunny_老王 的 文章,在 Makefile.vc 编译脚本的开头部分插入“PLATFORM = X86”,如下图所示。

  打开Visual Studio 2022的“x86 Native Tools Command Prompt for VS2022”窗口:

  进入进入wincon目录,输入以下编译命令,编译 Debug 版本:

nmake -f Makefile.vc WIDE=Y UTF8=Y DLL=Y DEBUG=Y

 不到一分钟编译完成:

这就是我们编译的成果:

先将这个PDCurses-3.9文件夹重命名备份,再解压出一个新的PDCurses-3.9文件夹来。同理,用以下命令编译 Release 版本:

nmake -f Makefile.vc WIDE=Y UTF8=Y DLL=Y

 编译成功:

 编译成果如下:

 

X64平台编译

在 Makefile.vc 编译脚本的开头部分插入“PLATFORM = X64”,如下图所示。

 打开Visual Studio 2022的“x64 Native Tools Command Prompt for VS2022”窗口,用以下命令编译 Debug 版本:

nmake -f Makefile.vc WIDE=Y UTF8=Y DLL=Y DEBUG=Y

  

  

 同理,用以下命令编译 Release 版本:

nmake -f Makefile.vc WIDE=Y UTF8=Y DLL=Y

 执行结果:

 

应用示例

设置环境变量

为了便于在程序中调用PDCurses,首先,将刚刚编译好的PDCurses库文件与头文件归置到适当的目录中:

  至于里面的文件怎么放置,当然是看 Visual C++ 的项目设置怎么方便怎么来。然后,设置系统环境变量PDCurses如下所示:

 

 检查一下系统是否已经能够正确识别该环境变量:

 

 编程实例

 在 Visual Studio 2022 中建立C/C++控制台应用程序项目,项目属性中:

 (1) C/C++ | General | Additional Include Directories 新增条目:$(PDCurses)\include

 (2) Linker | General | Additional Library Directories 新增条目:$(PDCurses)\LIB\$(Platform)\$(Configuration)

   (3) Linker | Input | Additional Dependencies 新增:pdcurses.lib

  (4) Build Events | Post-Build Event | Command Line 新增:

copy "$(PDCurses)\DLL\$(Platform)\$(Configuration)\pdcurses.dll" "$(TargetDir)"

   (5) C/C++ | Preprocessor | Preprocessor Definitions 新增:PDC_DLL_BUILD

 (6) 编写代码:

#include <string.h>
#include <time.h>
#include "Curses/curses.h"

void initialize();
void shutdown();

int main()
{
    char ch = '\n';
    char* message = "Hello, World!";

    initialize();
    
    do
    {
        mvprintw(LINES / 2, (COLS - (int)strlen(message)) / 2, message);
        refresh();

        ch = getch(stdscr);
    }
    while (ch != 'Q' && ch != 'q');

    shutdown();
    return 0;
}

void initialize()
{
    initscr();
    start_color();
    raw();
    cbreak();
    noecho();
    curs_set(0);
    srand((unsigned int)time(NULL));
}

void shutdown()
{
    endwin();
}

  执行结果:

参考文献

 (完)

posted on 2022-02-26 12:52  辛亚平  阅读(2361)  评论(3编辑  收藏  举报

导航