macOS 上的 Clang 无法从 ncurses 链接 lmenu

Sau*_*vil 3 c macos menu ncurses clang

我是使用 ncurses 库的新手,所以我一直在尝试重新创建此页面上的一些示例,http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/index.html

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

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

gcc libmenutest.c -o test -lmenu -lncurses

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

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

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

#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;
}

Run Code Online (Sandbox Code Playgroud)

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

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)
Run Code Online (Sandbox Code Playgroud)

Ste*_*cht 6

您可以执行以下操作:

brew install ncurses
Run Code Online (Sandbox Code Playgroud)

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

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

gcc -I/usr/local/opt/ncurses/include -L/usr/local/opt/ncurses/lib libmenutest.c -o test -lmenu -lncurses
Run Code Online (Sandbox Code Playgroud)

当你最终调用你的程序时,输出如下:

This worked :^) 
Run Code Online (Sandbox Code Playgroud)

制作

对于使用 CMake 的人,您的 CMakeLists.txt 可能如下所示:

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)
Run Code Online (Sandbox Code Playgroud)