4

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?

Aster
  • 143
  • 2
  • 11
  • I think you should add the library to `CMAKE_EXE_LINKER_FLAGS`. – kfx Feb 02 '16 at 17:12
  • Hm... you installed the dll, but not the dev package? I don't know much about MinGW, but usually you *need* the dev package to get the header files. It's curious you got a *linker* error, then, and not a compiler error about the missing header. Make sure you don't use a header from a different library version, or even a different curses distribution. That's *bound* to fail. – DevSolar Feb 02 '16 at 18:18

1 Answers1

5

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.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • I modified the CMake file as [this](http://pastebin.com/YLMh98LZ) (For now i'll leave the 'GLOB' and the generated file in the source till I have resolved things with PDcurses) now it compiles but when I try to execute the program it says that I don't have "libncursesw-6.dll" in my computer. If I try to execute one of the demos of the PDcurses it says the i lack of "libpdcurses.dll". I don't know what's wrong. I think that I installed the library properly as you can see [here](http://i.imgur.com/qJynBnq.png) – Aster Feb 03 '16 at 10:43
  • 1
    @Aster: This means that the library is found at *compile* time (because CMake always supplies the full file path to any library), but not at *run* time (because the path information is lost in the binary). You can either a) extend the library search path (that would mean extending %PATH% for Windows, or editing /etc/ld.so.conf resp. adding to /etc/ld.so.conf.d on Linux), or b) make CMake compile the full path to the library into the executable (`set( CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )`). But we are talking rather basic toolchain issues here; are you sure... (t.b.c.) – DevSolar Feb 03 '16 at 10:53
  • 1
    (ctd) ...you should be fiddling with CMake, Curses, *and* MinGW at this point, instead of familiarizing yourself a bit more with one at a time? – DevSolar Feb 03 '16 at 10:54
  • Thank you very much for you help and patience, I modified the %PATH% variable and now it works. Yes, I problably shouldn't mess with many things at once but I have to for University purposes. The problems is that I started using CLion (That uses CMake) for less than a month and before that I never used CMake or messed with minGW, I was using Code-Blocks (That doesn't use CMake) and with it was easier to build and compile but, once looked at CLion, I fell in love, it's an amazing IDE. Thanks again for all your help! – Aster Feb 03 '16 at 12:30