简体   繁体   English

静态链接ncurses到程序

[英]Statically link ncurses to program

I'm having some problems statically linking ncurses to one of my programs 我有一些问题静态地将ncurses链接到我的一个程序

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? 我在这里错过了什么?

You need to pass -l options at the end of the command line: 您需要在命令行的末尾传递-l选项:

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. 当编译器遇到-lfoo ,它会链接foo中前一个文件请求的所有符号。 If you put -lfoo at the beginning, no symbol has been requested yet, so no symbol gets linked. 如果您在开头放置-lfoo ,则尚未请求任何符号,因此没有符号链接。

Edit: 编辑:

I think the real problem is that you need to specify your -l option at the end of the command. 我认为真正的问题是你需要在命令的末尾指定你的-l选项。 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. 如果我在行尾添加-l:libncurses.a ,那么它可以正常工作。 All without the -static option BTW. 所有没有-static选项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. 我认为正在发生的事情是你有一个动态的ncurses库,但是你使用了-static选项,这意味着不使用任何动态库。 I suspect you do not actually have a static version of the ncurses library ie one ending with a .a suffix. 我怀疑你实际上没有静态版本的ncurses库,即一个以.a后缀结尾的版本。

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. 如果要链接ncurses的静态版本(.a)而不是动态版本(.so),则暂时删除libncurses.so的符号链接,以便链接器选择.a文件。 Alternatively copy the .a file somewhere else and add that to an earlier search path. 或者将.a文件复制到其他位置并将其添加到较早的搜索路径中。

Alternatively if your linker supports it (eg. ld ) then you could specify -l:libncurses.a instead of -lncurses . 或者,如果您的链接器支持它(例如ld ),那么您可以指定-l:libncurses.a而不是-lncurses

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. 我只是在ARM处理器上花了几个小时,试图让它工作,因为接受的答案对我不起作用。

Here are my findings: 以下是我的发现:

Apparently 显然地

gcc -static hello_curses.c -o curses -lncurses

works on an x64 processor, but not on an ARM processor. 适用于x64处理器,但不适用于ARM处理器。

When I tried with the above line, I still got all the "undefined reference errors" (and a lot more) of the OP. 当我尝试使用上面的行时,我仍然得到OP的所有“未定义的引用错误”(以及更多)。

You need to also link against libtinfo.a, and note that sequence matters. 您还需要链接libtinfo.a,并注意序列很重要。
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' 对'unctrl'的未定义引用

Of course this also works if you use the :lib syntax 当然,如果您使用:lib语法,这也有效

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... 哦,我喜欢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': (.te (.text + 0x2a8):对cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function init_pair':(。text + 0x2ac):对cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function init_pair':(。text + 0x50a):对tparm' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用tparm' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function init_pair':(。text + 0x518):对_nc_putp' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用_nc_putp' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function init_color':(。text + 0x552):对cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function init_color':(。text + 0x556):未定义的引用cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function init_color':(。te xt+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_ xt + 0x5e4):对tparm' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用tparm' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function init_color':(。text + 0x5f2):对_nc_putp' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用_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):对cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用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):对cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function未定义引用cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function has_colors':(。text + 0x768):未定义引用cur_term' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o): In function has_colors':(。text + 0x76c):未定义引用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 content':(。text + 0x7c2):对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' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):(.text+0x7c6): more undefined references to cur_term'跟随/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw。 a(lib_color.o):函数_nc_do_color': (.text+0x8de): undefined reference to tparm的_nc_do_color': (.text+0x8de): undefined reference to '/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm- linux-gnueabihf / libncursesw.a(lib_color.o):函数_nc_do_color': (.text+0x8e6): undefined reference to '/usr/lib/gcc/arm-linux-gnueabihf/4.8/../ .. /../arm-linux-gnueabihf/libncursesw.a(lib_color.o):函数_nc_do_color': (.text+0x958): undefined reference to '/usr/lib/gcc/arm-linux-gnueabihf/4.8 /../../../arm-linux-gnueabihf/libncursesw.a(lib_color.o):在函数set_foreground_color': (.text+0x62): undefined reference to '/ usr / lib / gcc / arm -linux-gnueabihf / 4.8 /../../../ arm-linux-gnueabihf / libncursesw.a(lib_color.o):在函数set_background_color': (.text+0xa2): undefined reference to 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 to acs_map' /usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_hline.o): In function whline': (.text+0xf0): undefined reference to acs_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 set_background_color': (.text+0xa2): undefined reference to '/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_hline .o):在函数whline': (.text+0xec): undefined reference to acs_map的whline': (.text+0xec): undefined reference to '/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf /libncursesw.a(lib_hline.o):在函数whline': (.text+0xf0): undefined reference to acs_map的whline': (.text+0xf0): undefined reference to '/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../ .. /arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):在函数wadd_wch': (.text+0x4fe): undefined reference to TABSIZE的wadd_wch': (.text+0x4fe): undefined reference to '/usr/lib/gcc/arm-linux-gnueabihf/4.8/ .. /../../arm-linux-gnueabihf/libncursesw.a(lib_add_wch.o):在函数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):在函数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):在函数wecho_wchar': (.text+0x6dc): undefined reference to 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 to unctrl' collect2: error: ld returned 1 exit status wecho_wchar': (.text+0x6dc): undefined reference to TABSIZE的wecho_wchar': (.text+0x6dc): undefined reference to '/usr/lib/gcc/arm-linux-gnueabihf/4.8/../../../arm-linux-gnueabihf/libncursesw.a(lib_wunctrl.o) :在函数wunctrl': (.text+0x30): undefined reference to unctrl的wunctrl': (.text+0x30): undefined reference to 'collect2:error:ld返回1退出状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM