c++ - 在 Linux 和 Windows 中启用 curses

标签 c++ windows cmake mingw32 pdcurses

我正在用 C++ 开发一个小项目,我正在使用 curses 作为用户界面。我能够很好地让它在我的 arch-linux 安装中工作,因为设置 ncurses 在那里工作非常简单。但是我的 cmake 设置在 Linux 上运行良好,我无法在 Windows 上正常运行。

这是我的 CMakeList.txt

cmake_minimum_required(VERSION 3.9)
project(fighting_pit)

find_package(Curses REQUIRED)
include_directories(${CURSES_INCLUDE_DIR})

set(CMAKE_CXX_STANDARD 11)

include_directories( ./include)
include_directories( ./src)
add_executable(fighting_pit
    include/Arena.h
    include/cursor.h
    include/Player.h
    include/spell.h
    include/Turns.h
    include/weapon.h
    include/Draw.h
    src/Arena.cpp
    src/cursor.cpp
    src/Player.cpp
    src/spell.cpp
    src/Turns.cpp
    src/weapon.cpp
    src/Draw.cpp
    main.cpp )

target_link_libraries(fighting_pit ${CURSES_LIBRARIES})

我尝试了几种方法使其也适用于 Windows。

1。下载资源

我尝试使用 mingw32-make 构建 pdcurses。它创建了 pdcurses.a 我将它添加到与项目相同的位置,但它仍然显示它找不到 curses 库。

2。通过mingw32-get下载

我使用了 mingw 的安装管理器,让它下载 libpdcurses 的 .dll 和开发包。只是试图通过 clion 运行 cmake 显示它仍然没有找到。所以我将它复制到 windows32 和项目文件夹中,但仍然没有帮助。

我不知道该怎么办。不幸的是,我既不是 C++ 用户也不是 Windows 用户。

最佳答案

我需要构建一个跨平台项目,该项目在 Linux 和 MacOS 上使用 ncurses,但在 Windows 上使用 pdcurses。 curses 的某些变体通常安装在流行的 Linux 发行版上。 ncurses 也可以在 MacOS 上使用。对于 Windows,情况并非如此。我的解决方案是下载 pdcurses sources并编写用于在 Windows 上构建它的 cmake 脚本。 if (WIN32 or MSVC)构建 pdcurses else()找到 ncurses。您可能还想创建一个 #include 的代理 header s pdcurses 或 ncurses 取决于平台。

克隆 GitHub 存储库后,我复制了 . 中的标题, ./pdcurses 中的 C 文件,来源在./wincon进入我项目中的一个新目录。然后我写了一个CMakeLists.txt文件将所有这些文件编译成一个库。它看起来像这样:

cmake_minimum_required(VERSION 3.2)

add_library(PDcurses
         # all of the sources
         addch.c
         addchstr.c
         addstr.c
         attr.c
         beep.c
         # ...
)
target_include_directories(PDcurses
        PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}
)

我的主CMakeLists.txt如果目标是 windows,文件会编译 pdcurses 源。

if(WIN32 OR MSVC)
        add_subdirectory(pdcurses)
        target_link_libraries(MyTarget
                PRIVATE
                PDcurses
        )
else()
        # find ncurses and use that
endif()

在大多数情况下,PDCurses 似乎(或多或少)替代了 ncurses。我能够在我的 Mac 上使用 curses 毫无问题地编译 PDcurses 附带的示例程序。

关于c++ - 在 Linux 和 Windows 中启用 curses,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50153653/

相关文章:

c - 使用 cmake find_package 在包中查找

c++ - vector 问题 (std::out_of_range)

c++ - %ud, %vud​​ 那些是什么

windows - FFMPEG 帧到 DirectX 表面硬件加速

php - 为什么 Windows 服务器上的查询不起作用

c++ - 如何将 emscripten 与 cmake 一起用于项目依赖项?

c++ - 为什么在定义变量并链接库时为 "undefined reference"?

c++ - 在 libcurl 中写入正文失败

c++ - 使用 openMP 进行并行化 - 堆栈或堆变量

windows - 更改作为镜像安装的 Windows 系统的主机名/IP 地址