将 ncurses 静态链接到程序
我在将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在命令行末尾传递
-l
选项:当编译器遇到
-lfoo
时,它会链接来自foo
的所有符号> 之前的文件已请求。如果将-lfoo
放在开头,则尚未请求任何符号,因此不会链接任何符号。You need to pass
-l
options at the end of the command line:When the compiler encounters
-lfoo
, it links in all the symbols fromfoo
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.编辑:
我认为真正的问题是您需要在命令末尾指定
-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
.我只是在 ARM 处理器上花了几个小时,试图让它工作,因为接受的答案对我不起作用。
以下是我的发现:
显然
适用于 x64 处理器,但不适用于 ARM 处理器。
当我尝试使用上面的行时,我仍然收到OP的所有“未定义的引用错误”(以及更多)。
您还需要链接 libtinfo.a,并注意顺序很重要。
这是有效的正确命令行:
如果你混淆了顺序,那么它将无法工作......
当然,如果您使用 :lib 语法,这也有效
这可以编译
这不能编译
哦,我多么喜欢 gcc...
这个项目根本不应该被允许从幼儿园毕业
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
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:
If you mix up the sequence, then it won't work...
Of course this also works if you use the :lib syntax
This compiles
This does not compile
Oh how I like gcc...
This program should never have been allowed to graduate from kindergarden