在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();
}

  執行結果:

參考文獻

 (完)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM