What's the difference between -lcurses and -ln

2019-01-26 10:42发布

问题:

I'm learning C and playing with the ncurses lib. I have seen references to both -lcurses and -lncurses but I have yet to find any differences (both work when compiling).

Appreciate the help!

回答1:

ncurses is an open-source clone of the original Unix curses library. libcurses.* usually points to libncurses.* to provide compatibility with the original library, so there would be no practical difference between using one over the other.

If you do in fact have more than one 'curses-type' library installed, -lcurses would essentially link your program to the default one, whereas -lncurses would explicitly choose the ncurses implementation.



回答2:

On my OpenSUSE 12.3 box there are no links to libcurses with ncurses installed. Any C programs that attempt to use the -lcurses flag will fail until you change the flag to -lncurses.

OpenSUSE 12.3 > ls -al /usr/lib64/*curses*
-rw-r--r-- 1 root root 2225910 Jan 25  2013 /usr/lib64/libncurses.a
-rw-r--r-- 1 root root  780540 Jan 25  2013 /usr/lib64/libncurses++.a
-rw-r--r-- 1 root root      69 Jan 25  2013 /usr/lib64/libncurses.so
-rw-r--r-- 1 root root  782884 Jan 25  2013 /usr/lib64/libncurses++w.a
-rw-r--r-- 1 root root 2768222 Jan 25  2013 /usr/lib64/libncursesw.a
-rw-r--r-- 1 root root      70 Jan 25  2013 /usr/lib64/libncursesw.so

The links are also missing on Fedora 17. However, on Ubuntu 13.04 the links are present:

Ubuntu 13.04 > ls -al /usr/lib/x86_64-linux-gnu/*curses*
lrwxrwxrwx 1 root root     12 Feb  8  2013 /usr/lib/x86_64-linux-gnu/libcurses.a -> libncurses.a
lrwxrwxrwx 1 root root     13 Feb  8  2013 /usr/lib/x86_64-linux-gnu/libcurses.so -> libncurses.so
-rw-r--r-- 1 root root 294180 Feb  8  2013 /usr/lib/x86_64-linux-gnu/libncurses.a
-rw-r--r-- 1 root root 158798 Feb  8  2013 /usr/lib/x86_64-linux-gnu/libncurses++.a
-rw-r--r-- 1 root root     31 Feb  8  2013 /usr/lib/x86_64-linux-gnu/libncurses.so

So compiling with -lcurses will fail on OpenSUSE and Fedora but work on Ubuntu. Compiling with -lncurses will work for all three distros.

The takeaway: If you want your code to compile on different Linux distros, you should use -lncurses.



回答3:

On my system (Slackware64 13.0), libcurses.so and friends are just symbolic links to the ncurses equivalent, so there is no difference. The libcurses.so (-lcurses) name is probably just to provide backwards compatibility with code designed for other systems which have a curses implementation other than ncurses.



回答4:

On my (fedora 11) PC /usr/lib/libcurses.so contains: "INPUT(-lncurses)". I think it means that the two form (-lcurses, -lncurses) is equivalent.



标签: c ncurses