How to link curses.h in Cmake?

2020-04-12 09:08发布

I know that maybe this is a silly question but I can't see through it, I searched for other answers here, that are pretty close to mine, but, still, I didn't understand how to do it.

The problem is that I can't compile a 'C' program that uses curses.h in Windows (I'm using Clion with MinGW), when I try to do it, it gives "undefined reference" for functions in curses.h (Such as 'initscr', 'clear', ...).

Through MinGW Installation Manager I installed "mingw-32-libpdcurses" (There were two available with two different classes: dev and dll; I installed the dll one).

The CMAKEfile i'm using is this:

cmake_minimum_required(VERSION 3.3)
project(Project1)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -lpdcurses")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

file(GLOB Project1_SRC
        "*.h"
        "*.c"
        )

add_executable(Project1 ${Project1_SRC})

What should I change in it in order to make it compile with curses.h?

1条回答
Deceive 欺骗
2楼-- · 2020-04-12 10:02

Basically the same way you locate and integrate about any third-party library with CMake: Using one of the packaged Find___.cmake modules.

These are located in share/cmake-X.Y/Modules of your CMake installation directory. Check the files themselves for their individual documentation, and cmake --help-command find_package for details on how to call them.

I haven't tried the following with PDCurses on MinGW specifically, but if it doesn't work, that'd a clear bug report to Kitware (the makers of CMake):

find_package( Curses REQUIRED )
include_directories( ${CURSES_INCLUDE_DIRS} )
target_link_libraries( Project1 ${CURSES_LIBRARIES} )

The following variables are set as appropriate to tell you which header is available:

  • CURSES_HAVE_CURSES_H for curses.h
  • CURSES_HAVE_NCURSES_H for ncurses.h
  • CURSES_HAVE_NCURSES_NCURSES_H for ncurses/ncurses.h
  • CURSES_HAVE_NCURSES_CURSES_H for ncurses/curses.h

Additional advice:

file(GLOB Project1_SRC
        "*.h"
        "*.c"
        )

Don't do that.

To quote from the documentation of that very function:

We do not recommend using GLOB to collect a list of source files from your source tree. If no CMakeLists.txt file changes when a source is added or removed then the generated build system cannot know when to ask CMake to regenerate.


set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")

Don't do that either.

You do not want any generated files to end up in your source directory (where they get in the way of your versioning system, or worse, get actually checked in to the repository). You want to generate everything in the binary directory, cleanly out of the way.

查看更多
登录 后发表回答