Home > Mobile >  How to correctly debug ncurses ?
How to correctly debug ncurses ?

Time:07-18

I wanna learn something from ncurses, so that I can use assembly to write some simple functions to draw graphics.

For example, when I step into initscr, then I'm right here /lib/x86_64-linux-gnu/libncurses.so.6, I can't see any c code. Do you have some any ways to solve this problem?

I use below commands to install ncurses!

wget https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.2.tar.gz

tar xvzf ncurses-6.2.tar.gz

cd ...

./configure --with-shared --with-normal --with-debug --enable-overwrite

make

sudo make install

CodePudding user response:

You need linker libncurses_g.a

gcc -g ... -o ... /usr/lib/libncurses_g.a

CodePudding user response:

./configure ...
make
sudo make install

99% of the time this installs to /usr/local.

/usr/local/include and /usr/local/lib are normally consulted before the usual /usr/include and /usr/lib when compiling and linking. However /usr/local is completely ignored when loading shared libraries at run time. You need to do something special in order to accommodate this.

There are several ways to do so.

  1. Add -Wl,-rpath=/usr/local/lib to your link command.
  2. Set LD_LIBRARY_PATH environment variable to something that contains /usr/local/lib.
  3. Add /usr/local/lib to your /etc/ld.so.conf (to be done with caution).

CodePudding user response:

Note that basically all Linux terminal emulators support Unicode (using UTF-8) by default, as well as ANSI escape codes.

So, if you wanted to print a small red double-walled rectangle around the bright green letter "B" in the upper left corner, your program would simply write the following string (in C or assembly notation, i.e. the double quotes are not part of the string contents):

"\033[s\033[1;1H\033[0;31m╔═╗\033[0m\033[2;1H\033[0;31m║\033[1;32mB\033[0;31m║\033[0m\033[3;1H\033[0;31m╚═╝\033[0m\033[u"

where the two bytes (27,91 = \033[) comprise the control sequence introducer, CSI.

Do remember that write syscall is allowed to return a short count, especially when writing to a tty (terminal-like device). If the return value is positive but smaller than the length of the string, you need to advance the pointer to the data by that amount, subtract the return value from the length, and do the syscall again, until it returns either the desired length or a value less than 1. (0 should not occur, and negative values are errno error codes.)

What ncurses does, is support such codes in their various forms for basically all terminal-like devices, detects the correct one to be used via the TERM environment variable, and provides a lot of useful abstractions (via helper functions, like windows and such). But the above, UTF-8 and ANSI escape codes, works in the majority of cases, and is a viable minimal implementation.

  • Related