将 ncurses 静态链接到程序 [英] Statically link ncurses to program
本文介绍了将 ncurses 静态链接到程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在将 ncurses 静态链接到我的一个程序时遇到了一些问题
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
");
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 a little confused why this isn't working. What am I missing here?
推荐答案
需要在命令行末尾传递-l
选项:
You need to pass -l
options at the end of the command line:
gcc -static hello_curses.c -o curses -lncurses
当编译器遇到 -lfoo
时,它会从 foo
中链接上一个文件已请求的所有符号.如果你把 -lfoo
放在开头,还没有任何符号被请求,所以没有符号被链接.
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.
这篇关于将 ncurses 静态链接到程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文