微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用 ncurses 运行 C 代码时出现编译和链接错误

如何解决尝试使用 ncurses 运行 C 代码时出现编译和链接错误

我想在 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 库包,但出现错误提示正在安装的包中的文件与已安装的包冲突。

解决方法

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

<div class='page_body'>
  <div class='col_1'>Post navigation</div>
  <div class='col_2'>
    <div class='rich_text'>
      Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
      <br><br>
      <div class='table_container'>Table</div>
      <br>
      Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 
      <br>
      <div class='table_container_abs'>Absolute table</div>
    </div>
  </div>
</div>

如果它没有从终端重新安装库,但另一方面尝试使用此命令运行您的代码 #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; }

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。