将 ncurses 静态链接到程序

发布于 2024-09-14 14:00:24 字数 899 浏览 2 评论 0原文

我在将 ncurses 静态链接到我的程序之一时遇到一些问题

这是一个非常简单的示例程序:

#include<ncurses.h>


int main(){

    initscr();
    printw("Hello world\n");
    refresh();
    getch();
    endwin();
    return 0;
}

当我使用它进行编译时,

gcc -static -lncurses hello_curses.c -o curses

我收到这些错误:

/tmp/ccwHJ6o1.o: In function `main':
curses_hello.c:(.text+0x5): undefined reference to `initscr'
curses_hello.c:(.text+0x14): undefined reference to `printw'
curses_hello.c:(.text+0x1b): undefined reference to `stdscr'
curses_hello.c:(.text+0x20): undefined reference to `wrefresh'
curses_hello.c:(.text+0x27): undefined reference to `stdscr'
curses_hello.c:(.text+0x2c): undefined reference to `wgetch'
curses_hello.c:(.text+0x31): undefined reference to `endwin'
collect2: ld returned 1 exit status

我有点困惑为什么这不起作用。我在这里缺少什么?

I'm having some problems statically linking ncurses to one of my programs

Here's a really simple sample program:

#include<ncurses.h>


int main(){

    initscr();
    printw("Hello world\n");
    refresh();
    getch();
    endwin();
    return 0;
}

When I compile it with

gcc -static -lncurses hello_curses.c -o curses

I get these errors:

/tmp/ccwHJ6o1.o: In function `main':
curses_hello.c:(.text+0x5): undefined reference to `initscr'
curses_hello.c:(.text+0x14): undefined reference to `printw'
curses_hello.c:(.text+0x1b): undefined reference to `stdscr'
curses_hello.c:(.text+0x20): undefined reference to `wrefresh'
curses_hello.c:(.text+0x27): undefined reference to `stdscr'
curses_hello.c:(.text+0x2c): undefined reference to `wgetch'
curses_hello.c:(.text+0x31): undefined reference to `endwin'
collect2: ld returned 1 exit status

I'm a little confused why this isn't working. What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

╰つ倒转 2024-09-21 14:00:24

您需要在命令行末尾传递 -l 选项:

gcc -static hello_curses.c -o curses -lncurses

当编译器遇到 -lfoo 时,它会链接来自 foo 的所有符号> 之前的文件已请求。如果将 -lfoo 放在开头,则尚未请求任何符号,因此不会链接任何符号。

You need to pass -l options at the end of the command line:

gcc -static hello_curses.c -o curses -lncurses

When the compiler encounters -lfoo, it links in all the symbols from foo that have been requested by a previous file. If you put -lfoo at the beginning, no symbol has been requested yet, so no symbol gets linked.

无妨# 2024-09-21 14:00:24

编辑:

我认为真正的问题是您需要在命令末尾指定 -l 选项。我只是按照你的方式尝试了一下并重现了你的错误。如果我将 -l:libncurses.a 放在该行的末尾,那么它就可以工作。顺便说一句,所有这些都没有 -static 选项。


我认为发生的情况是您有一个 ncurses 动态库,但您使用了 -static 选项,这意味着不使用任何动态库。我怀疑您实际上没有 ncurses 库的静态版本,即以 .a 后缀结尾的库。

如果您想链接 ncurses 的静态版本 (.a) 而不是动态版本 (.so),请暂时删除 libncurses.so 的符号链接,以便链接器改为选择 .a 文件。或者将 .a 文件复制到其他位置并将其添加到较早的搜索路径中。

或者,如果您的链接器支持它(例如ld),那么您可以指定-l:libncurses.a而不是-lncurses

Edit:

I think the real problem is that you need to specify your -l option at the end of the command. I just tried it the way you had it and reproduced your error. If I put -l:libncurses.a at the end of the line then it works. All without the -static option BTW.


I think what is happening is that you have a dynamic library for ncurses but you have used the -static option which means to not use any dynamic libraries. I suspect you do not actually have a static version of the ncurses library i.e. one ending with a .a suffix.

If you want to link with the static version (.a) of ncurses rather than the dynamic version (.so) then temporarily remove the symlink for libncurses.so so that the linker picks up the .a file instead. Alternatively copy the .a file somewhere else and add that to an earlier search path.

Alternatively if your linker supports it (eg. ld) then you could specify -l:libncurses.a instead of -lncurses.

梦魇绽荼蘼 2024-09-21 14:00:24

我只是在 ARM 处理器上花了几个小时,试图让它工作,因为接受的答案对我不起作用。

以下是我的发现:

显然

gcc -static hello_curses.c -o curses -lncurses

适用于 x64 处理器,但不适用于 ARM 处理器。

当我尝试使用上面的行时,我仍然收到OP的所有“未定义的引用错误”(以及更多)。

您还需要链接 libtinfo.a,并注意顺序很重要。
这是有效的正确命令行:

gcc -static hello_curses.c -o curses -lncurses -ltinfo

如果你混淆了顺序,那么它将无法工作......

gcc -static hello_curses.c -o curses -ltinfo -lncurses 

对“unctrl”的未定义引用

当然,如果您使用 :lib 语法,这也有效

这可以编译

gcc -static hello_curses.c -o curses -l:libncursesw.a -l:libtinfo.a

这不能编译

gcc -static hello_curses.c -o curses -l:libtinfo.a -l:libncursesw.a 

哦,我多么喜欢 gcc...
这个项目根本不应该被允许从幼儿园毕业

(.text+0x2a8):对 cur_term' 的未定义引用
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数
init_pair': (.text+0x2ac): 未定义的引用
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数
init_pair'中:(.text+0x50a):对tparm'的未定义引用
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数
init_pair': (.text+0x518): 未定义的引用
<代码>_nc_putp'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数 init_color': (.text+0x552): 未定义引用
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数
init_color': (.text+0x556): 未定义引用
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数
init_color': (.text+0x5e4): 未定义引用
tparm'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数
init_color': (.text+0x5f2): 未定义的引用
<代码>_nc_putp'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数中can_change_color': (.text+0x740): 未定义的引用
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数中
can_change_color': (.text+0x744): 未定义的引用
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数
has_colors': (.text+0x768): 未定义的引用
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数中
has_colors': (.text+0x76c): 未定义的引用
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数中
color_content': (.text+0x7c2): 未定义引用
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):(.text+0x7c6):
更多对
cur_term' 的未定义引用如下
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数 _nc_do_color': (.text+0x8de) 中:未定义引用
tparm'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数 _nc_do_color': (.text+0x8e6) 中:未定义引用
tputs'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数 _nc_do_color': (.text+0x958) 中:未定义引用
tputs'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数 set_foreground_color': (.text+0x62) 中:未定义的引用
tputs'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
在函数 set_background_color': (.text+0xa2) 中:未定义的引用
tputs'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_hline.o):
在函数 whline': (.text+0xec): 未定义引用acs_map'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_hline.o):
在函数 whline': (.text+0xf0): 未定义引用acs_map'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):
在函数 wadd_wch': (.text+0x4fe) 中:未定义引用
TABSIZE'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):
在函数 wadd_wch': (.text+0x502) 中:未定义引用
TABSIZE'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):
在函数 wecho_wchar': (.text+0x6d8) 中:未定义引用
TABSIZE'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):
在函数 wecho_wchar': (.text+0x6dc) 中:未定义引用
TABSIZE'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_wunctrl.o):
在函数 wunctrl': (.text+0x30): 未定义引用unctrl'
collect2:错误:ld 返回 1 退出状态


I just spent a few hours on an ARM processor, trying to get it to work, as the accepted answer didn't work for me.

Here are my findings:

Apparently

gcc -static hello_curses.c -o curses -lncurses

works on an x64 processor, but not on an ARM processor.

When I tried with the above line, I still got all the "undefined reference errors" (and a lot more) of the OP.

You need to also link against libtinfo.a, and note that sequence matters.
This is the correct command line that works:

gcc -static hello_curses.c -o curses -lncurses -ltinfo

If you mix up the sequence, then it won't work...

gcc -static hello_curses.c -o curses -ltinfo -lncurses 

undefined reference to `unctrl'

Of course this also works if you use the :lib syntax

This compiles

gcc -static hello_curses.c -o curses -l:libncursesw.a -l:libtinfo.a

This does not compile

gcc -static hello_curses.c -o curses -l:libtinfo.a -l:libncursesw.a 

Oh how I like gcc...
This program should never have been allowed to graduate from kindergarden

(.text+0x2a8): undefined reference to cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
init_pair': (.text+0x2ac): undefined reference to
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
init_pair': (.text+0x50a): undefined reference to tparm'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
init_pair': (.text+0x518): undefined reference to
_nc_putp'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
init_color': (.text+0x552): undefined reference to
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
init_color': (.text+0x556): undefined reference to
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
init_color': (.text+0x5e4): undefined reference to
tparm'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
init_color': (.text+0x5f2): undefined reference to
_nc_putp'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
can_change_color': (.text+0x740): undefined reference to
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
can_change_color': (.text+0x744): undefined reference to
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
has_colors': (.text+0x768): undefined reference to
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
has_colors': (.text+0x76c): undefined reference to
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function
color_content': (.text+0x7c2): undefined reference to
cur_term'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):(.text+0x7c6):
more undefined references to
cur_term' follow
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function _nc_do_color': (.text+0x8de): undefined reference to
tparm'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function _nc_do_color': (.text+0x8e6): undefined reference to
tputs'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function _nc_do_color': (.text+0x958): undefined reference to
tputs'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function set_foreground_color': (.text+0x62): undefined reference
to
tputs'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):
In function set_background_color': (.text+0xa2): undefined reference
to
tputs'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_hline.o):
In function whline': (.text+0xec): undefined reference toacs_map'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_hline.o):
In function whline': (.text+0xf0): undefined reference toacs_map'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):
In function wadd_wch': (.text+0x4fe): undefined reference to
TABSIZE'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):
In function wadd_wch': (.text+0x502): undefined reference to
TABSIZE'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):
In function wecho_wchar': (.text+0x6d8): undefined reference to
TABSIZE'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):
In function wecho_wchar': (.text+0x6dc): undefined reference to
TABSIZE'
/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_wunctrl.o):
In function wunctrl': (.text+0x30): undefined reference tounctrl'
collect2: error: ld returned 1 exit status

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文