Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling curses in both Linux and 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.

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

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

1. Downloading sources

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.

2. Downloading via mingw32-get

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.

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

like image 568
Jakub Peschel Avatar asked May 03 '18 11:05

Jakub Peschel


1 Answers

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.

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

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 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.

like image 181
Indiana Kernick Avatar answered Oct 06 '22 08:10

Indiana Kernick