在 Linux 和 Windows 中启用诅咒

Enabling curses in both Linux and Windows

本文关键字:启用 诅咒 Windows Linux      更新时间:2023-10-16

我正在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 我将其添加到与项目相同的位置,但它仍然显示它找不到诅咒库。

2. 通过 mingw32-get 下载

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

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

我需要构建一个跨平台的项目,在Linux和MacOS上使用ncurses,但在Windows上使用pdcurses。curses 的某些变体通常安装在流行的 Linux 发行版上。ncurses 在 MacOS 上也可用。对于Windows来说,情况并非如此。我的解决方案是下载 pdcurses 源代码并编写一个 cmake 脚本以在 Windows 上构建它。if (WIN32 or MSVC)构建 pdcurseselse()找到 ncurses。您可能还希望创建一个代理标头,该标头根据平台#include的 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}
)

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

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附带的示例程序,没有任何麻烦。