在 Linux 和 Windows 中启用 curses [英] Enabling curses in both Linux and Windows

查看:43
本文介绍了在 Linux 和 Windows 中启用 curses的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on small project in C++ and I am using curses for user interface. I am pretty nicely able to make it work in my arch-linux installation, because it is pretty simple to set up ncurses to work there. But with my cmake setting which is working nicely at Linux I am not able to properly make it work at Windows.

这是我的 CMakeList.txt

Here is my 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 上运行.

I tried several approaches to make it work on Windows too.

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

I tried to build pdcurses with mingw32-make. It created pdcurses.a I added it to same location as project, but it still shows it cannot find curses library.

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

I used installation manager from mingw and let it download both .dll and dev package of libpdcurses. Just trying to run cmake through clion showed it is still not found. So I copied it both into windows32 and project folder but it still didn't help.

我不知道我应该做什么.不幸的是,我既不是 C++ 用户,也不是 Windows 用户.

I don't know what I should do. Unfortunately I am neither C++ user neither Windows user.

推荐答案

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

I needed to build a cross-platform project that uses ncurses on Linux and MacOS but uses pdcurses on Windows. Some variant of curses is usually installed on popular distributions of Linux. ncurses is also available on MacOS as well. The same isn't quite true for Windows. My solution was to download the pdcurses sources and write a cmake script for building it on Windows. if (WIN32 or MSVC) build pdcurses else() find ncurses. You might also want to create a proxy header that #includes pdcurses or ncurses depending on the platform.

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

After cloning the GitHub repo, I copied the headers in ., the C files in ./pdcurses, the sources in ./wincon into a new directory in my project. Then I wrote a CMakeLists.txt file to compile all of these files into a library. It looked something like this:

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}
)

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

My main CMakeLists.txt file compiled the pdcurses sources if the target is windows.

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

在大多数情况下,PDCurses 似乎是(或多或少)ncurses 的替代品.我能够使用curses在我的Mac上编译PDcurses附带的示例程序,没有任何问题.

PDCurses seems to be a (more or less) drop-in replacement for ncurses in most situations. I was able to compile the example programs that came with PDcurses on my Mac using curses without any troubles.

这篇关于在 Linux 和 Windows 中启用 curses的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆