0

I'm trying to make ncurses work with unicodes. I found a nice tutorial here. However I'm having troubles with the following code:

#include <locale.h>
#include <curses.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  setlocale(LC_ALL, "");

  initscr();

  printw("Euro\n");

  printw("€\n");              // literal Unicode
  printw("\u20ac\n");         // escaped Unicode (C99)

  printw("%lc\n", L'€');      // wint_t
  printw("%ls\n", L"€");      // wchar_t
  addwstr(L"\u20AC\n");       // wchar_t

  printw("\xe2\x82\xac\n");   // utf-8 encoded
  addstr("\xe2\x82\xac\n");   // utf-8 encoded

  for (int i = 0; i < 10; i++)
  {
    printw("%c %lc\n", '0' + i, L'0' + i);
  }

  getch();
  endwin();

  return EXIT_SUCCESS;
}

I can summarize the problems in two points

  1. When I compile with gcc -c -Wall -Wextra -g -o build/main.o src/main.c I get the message

    src/main.c:19:3: warning: implicit declaration of function ‘addwstr’; did you mean ‘addstr’? [-Wimplicit-function-declaration]
    

    I do not know why I'm getting such a message. In fact, in the curses.h I can see the line

    #define addwstr(wstr)                   waddwstr(stdscr,(wstr)) 
    [...]    
    extern NCURSES_EXPORT(int) waddwstr (WINDOW *,const wchar_t *);         /* generated:WIDEC */
    

    I use Arch as a distro and ncurses package is installed regularly.

  2. After linking with gcc -lncursesw -D_GNU_SOURCE -D_DEFAULT_SOURCE -o bin/main build/main.o if a run the program on some consoles (i.e. KDE Konsole) the output is ugly

    Euro
     ~B
     ~B
    
     ~B
     ~B
    0 1 2 3 4 5 6 7 8 9 
    

    while on some other (i.e. the one embedded in vscode), the output is the desired one:

    Euro
    €
    €
    €
    €
    €
    €
    €
    0 0
    1 1
    2 2
    3 3
    4 4
    5 5
    6 6
    7 7
    8 8
    9 9
    

What is going on? Can you tell me how to make my code compile without warning and work on every console?

EDIT

I think I found a solution to issue 2). It is enough to use

setlocale(LC_ALL, "en_US.UTF-8");

to make things work properly

MaPo
  • 613
  • 4
  • 9
  • I've had some issues with `curses` before. It isn't a standard Unix library, so portability is limited. Combining it with Unicode just makes it worse, as not everything supports it. Your issue could be with one of these being incompatible. – anonmess Oct 07 '19 at 15:36
  • Are you referring to issue 1 or issue 2? – MaPo Oct 07 '19 at 15:42
  • Issue 2. I'm not sure why you're getting issue 1, although the one you reference from `curses.h` is `waddwstr`, not `addwstr` as in the error in 1. – anonmess Oct 07 '19 at 15:45
  • See edits. The second issue seem solved. The first remain. `addwstr` is defined in terms of `waddwstr`, as I edited. – MaPo Oct 07 '19 at 15:52
  • Possible duplicate of [Curses library doesn't support wide char on OS X high sierra](https://stackoverflow.com/questions/48042203/curses-library-doesnt-support-wide-char-on-os-x-high-sierra), alternatively [How does ncurses output non-ascii characters?](https://stackoverflow.com/questions/43671649/how-does-ncurses-output-non-ascii-characters/43680801#43680801). Either way, the manual pages are the place to start. – Thomas Dickey Oct 08 '19 at 07:53
  • As you can see from the edit the problem now is the warning I have during compilation because not it outputs correctly unicode characters. Then, as I specified I'm running Linux and, as @anonmess said, there are some issues about portability; therefore I think that the specific installation is important. And my enveirnonment is different from OSX high Sierra. – MaPo Oct 08 '19 at 11:32

0 Answers0