macOS上的Clang无法从ncurses链接lmenu [英] Clang on macOS fails linking lmenu from ncurses

查看:268
本文介绍了macOS上的Clang无法从ncurses链接lmenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用ncurses库,因此我一直在尝试重新创建此页面上的一些示例,

I'm new to using the ncurses library, so I've been trying to recreate some of the examples on this page, http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html.

我已经获得了有关创建菜单的部分,特别是示例21.我编写的程序可以在Linux(特别是Ubuntu 18.04)上运行,但是当我使用Menu库时无法编译.我使用JUST ncurses编写的所有其他示例程序都可以正常编译而没有问题,只是当我尝试使用Menu库时.

I've gotten the section about creating menus, specifically example 21. The program I've written works on Linux, specifically Ubuntu 18.04, but I'm not able to compile when I'm using the Menu library. All the other example programs I've written using JUST ncurses compiles fine without issue, it's just when I try to use the Menu library.

我用于在Linux和macOS上构建的命令是

The command I'm using to build on Linux and macOS is,

gcc libmenutest.c -o test -lmenu -lncurses

我尝试移动-lmenu -lncurses并将macOS上的顺序更改为未成功.我已经通过brew安装了ncurses,并尝试使用brew中的gcc-8,但那里也没有成功.

I've tried moving -lmenu -lncurses about and changing the order on macOS to no success. I've installed ncurses via brew and tried using gcc-8 from brew, but no success there either.

我正在运行全新安装的macOS和最新的命令行工具.我可以在/usr/lib中看到libmenu,与libncurses相同.所以我很困惑为什么编译器找不到它.

I'm running a practically fresh install of macOS and the latest command line tools. I can see libmenu in /usr/lib, same as libncurses. So I'm really confused why the compiler isn't find it.

这是我一直在尝试诊断问题的一些测试代码.

Here's some test code I've been trying to diagnose the problem with.

#include <curses.h>
#include <menu.h>
#include <stdlib.h>

#define ARRAY_SIZE(a) (sizeof a / sizeof a[0])

int main (void)
{
  int i;
  int nchoices;

  char *choices[] = {
    "Choice 1", "Choice 2", "Choice 3", "Exit", (char *) NULL,
  };

  // Test that the types are present, this should test for the include headers
  ITEM **items;
  MENU *menu;
  WINDOW *win;

  // This will test for includes and to see if libncurses can be linked
  initscr ();
  noecho ();
  cbreak ();
  keypad (stdscr, TRUE);

  // this bit will test for libmenu include and if it can be linked
  nchoices = ARRAY_SIZE (choices);
  items = calloc (nchoices, sizeof (ITEM *));
  if (items == NULL) exit (1);
  for (i = 0; i < nchoices; i++)
    items[i] = new_item (choices[i], choices[i]);

  // write smarmy message to screen :^^^^^^)
  printw ("This worked :^)");
  refresh ();
  getch ();

  // clean up
  for (i = 0; i < nchoices; i++)
    free_item (items[i]);
  endwin ();

  return 0;
}

这是我现在得到的输出...

Here's the output which I'm getting right now...

Undefined symbols for architecture x86_64:
  "_free_item", referenced from:
      _main in libmenutest-0f0c39.o
  "_new_item", referenced from:
      _main in libmenutest-0f0c39.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

推荐答案

您可以执行以下操作:

brew install ncurses

由于macOS已经包含ncurses版本,brew将其替代版本安装在/usr/local/opt/ncurses中.

Since macOS already contains a ncurses version brew installs its alternative version in /usr/local/opt/ncurses.

为了使编译器和链接器可以访问它,构建命令现在应如下所示:

So that the compiler and linker can access it, your build command should now look like this:

gcc -I/usr/local/opt/ncurses/include -L/usr/local/opt/ncurses/lib libmenutest.c -o test -lmenu -lncurses

当您最终调用程序时,将输出以下内容:

When you finally call your program, the following is output:

This worked :^) 

CMake

对于使用CMake的人,您的CMakeLists.txt可能看起来像这样:

For the folks using CMake your CMakeLists.txt could look like this:

cmake_minimum_required(VERSION 3.14)
project(libmenutest C)

set(CMAKE_C_STANDARD 99)

include_directories(/usr/local/opt/ncurses/include)

link_directories(/usr/local/opt/ncurses/lib)

add_executable(libmenutest libmenutest.c)

target_link_libraries(libmenutest menu ncurses)

这篇关于macOS上的Clang无法从ncurses链接lmenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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