Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active February 5, 2025 14:51
Show Gist options
  • Save scivision/017664f4b0b5a1a3f5314349c9cacef3 to your computer and use it in GitHub Desktop.
Save scivision/017664f4b0b5a1a3f5314349c9cacef3 to your computer and use it in GitHub Desktop.
Fix find_package(Curses) in CMake to work with a minimal example in C
cmake_minimum_required(VERSION 3.18)
project(CursesDemo LANGUAGES C)
include(curses.cmake)
add_executable(test_curses test_curses.c)
target_link_libraries(test_curses PRIVATE Curses::Curses)
# CMake FindCurses may not find all needed include directories and may need definitions
find_package(Curses REQUIRED)
if(NOT CURSES_HAVE_CURSES_H)
foreach(d IN LISTS CURSES_INCLUDE_DIRS)
find_path(curses_inc
NAMES curses.h
HINTS ${d}/ncurses ${d}/pdcurses
NO_DEFAULT_PATH
)
if(curses_inc)
list(APPEND CURSES_INCLUDE_DIRS ${curses_inc})
break()
endif()
endforeach()
endif()
if(NOT TARGET Curses::Curses)
add_library(Curses::Curses INTERFACE IMPORTED)
target_link_libraries(Curses::Curses INTERFACE ${CURSES_LIBRARIES})
target_include_directories(Curses::Curses INTERFACE ${CURSES_INCLUDE_DIRS})
if(CURSES_HAVE_NCURSES_H OR CURSES_HAVE_NCURSES_NCURSES_H)
# https://stackoverflow.com/a/67964252/4236751
# avoids MinGW link failure
target_compile_definitions(Curses::Curses INTERFACE $<$<BOOL:${MINGW}>:NCURSES_STATIC>)
endif()
endif()
#include <curses.h>
int main(void)
{
initscr();
printw("Hello from Curses. Press any key to continue...");
refresh();
// pause the screen output
getch();
endwin();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment