如何在Cmake中链接curses.h? [英] How to link curses.h in Cmake?

查看:237
本文介绍了如何在Cmake中链接curses.h?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可能是一个愚蠢的问题,但是我看不到它,我在这里搜索了其他答案,这些答案与我的非常接近,但是,我仍然不知道该怎么做。 / p>

问题是,当我尝试执行Windows时,我无法编译使用curses.h的'C'程序(我在MinGW中使用Clion)它为curses.h中的函数提供了未定义的引用(例如'initscr','clear'等)。



通过MinGW Installation Manager I安装了 mingw-32-libpdcurses(有两个可用的类,有两个不同的类:dev和dll;我安装了一个dll)。



我正在使用的CMAKEfile是这样的:

  cmake_minimum_required(版本3.3)
项目(Project1)

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

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $ {CMAKE_CURRENT_SOURCE_DIR})

file(GLOB Project1_SRC $ b .h
* .c


add_ex ecutable(Project1 $ {Project1_SRC})

我应该对其进行哪些更改才能使其与curses.h?

解决方案

基本上与查找和集成 any 第三方库的方式相同CMake:使用打包的 Find ___。cmake 模块之一。



这些位于 share / cmake-XY / Modules 到您的CMake安装目录。检查文件本身以获取单独的文档,并查看 cmake --help-command find_package 了解如何调用它们的详细信息。



我还没有在MinGW上尝试使用PDCurses进行以下操作,但如果它不起作用,则向Kitware(CMake的制造商)报告了明确的错误:

  find_package(需要诅咒)
include_directories($ {CURSES_INCLUDE_DIRS})
target_link_libraries(Project1 $ {CURSES_LIBRARIES})

根据需要设置以下变量以告诉您哪个报头可用:




  • CURSES_HAVE_CURSES_H for curses.h

  • CURSES_HAVE_NCURSES_H ncurses.h

  • CURSES_HAVE_NCURSES_NCURSES_H for ncurses / ncurses.h

  • CURSES_HAVE_NCURSES_CURSES_H ncurses / curses.h






广告常规建议:

  file(GLOB Project1_SRC 
* .h
* .c

请勿这样做。



要引用该功能文档:


我们不建议使用GLOB从您的源树中收集源文件列表。如果在添加或删除源时没有CMakeLists.txt文件更改,则生成的构建系统将不知道何时要求CMake重新生成。







  set(CMAKE_RUNTIME_OUTPUT_DIRECTORY $ {CMAKE_CURRENT_SOURCE_DIR})

也不要这样做。



您不想要任何东西生成的文件将最终保存在您的 source 目录中(这些文件会妨碍您的版本控制系统,或更糟糕的是,实际上会将其检入到存储库中)。您想完全在 binary 目录中生成所有内容。


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?

解决方案

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.

这篇关于如何在Cmake中链接curses.h?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆