【问题标题】:Getting complilation and linking errors while trying to run a C code with ncurses尝试使用 ncurses 运行 C 代码时出现编译和链接错误
【发布时间】:2021-07-05 06:27:14
【问题描述】:

我想在 C 中实现一个循环,该循环在按下特定键时停止。为此,我一直在尝试使用 ncurses 库。我有一个离线的 Redhat 系统,无法连接到互联网。运行命令locate libncurses 后,我得到许多行,例如/usr/lib64/libncurses++.so.5。我认为这意味着我的系统上存在 ncurses 库。接下来我尝试运行以下代码来测试 ncurses

#include <ncurses.h>

int
main()
{
    initscr();
    cbreak();
    noecho();
    scrollok(stdscr, TRUE);
    nodelay(stdscr, TRUE);
    while (true) {
        if (getch() == 'g') {
            printw("You pressed G\n");
        }
        napms(500);
        printw("Running\n");
    }
}

这取自问题Using kbhit() and getch() on Linux

当我尝试以以下方式运行时 gcc -o testncurses testncurse.c 我收到错误 undefined reference to 'initscr'。然后我继续以gcc -o testncurses testncurse.c -lncurses 运行它。但我得到了错误

/usr/bin/ld: cannot find -lncurses

collect2:error: ld returned 1 exit status

我应该怎么做才能运行上面的代码?

我也尝试安装一个较新的 ncurses 库包,但这给了我一个错误,说正在安装的包中的文件与已安装的包冲突。

【问题讨论】:

  • 你有安装ncurses的开发包吗?
  • 正如我所说,在尝试安装它们时,我收到包与已安装的包冲突的错误
  • 你需要ncurses的开发包。如果你不能安装它,你可能不走运。所以,你应该解决安装冲突而不是忽略它。

标签: compilation linker ncurses


【解决方案1】:

首先我建议运行一个 hello world 示例来查看您的库是否正常工作:

#include <ncurses.h>

int main()
{   
    initscr();          /* Start curses mode          */
    printw("Hello World !!!");  /* Print Hello World          */
    refresh();          /* Print it on to the real screen */
    getch();            /* Wait for user input */
    endwin();           /* End curses mode        */

    return 0;
}

如果它没有从终端重新安装库,但另一方面尝试使用此命令运行您的代码gcc &lt;program file&gt; -lncurses

不更改名称。 并在每次打印后尝试 refresh() 或 wrefresh()

【讨论】:

  • 我在运行你的 hello world 程序时遇到了同样的错误 /usr/bin/ld: cannot find -lncurses collect2:error: ld returned 1 exit status
猜你喜欢
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
相关资源
最近更新 更多