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
When I compile with
gcc -c -Wall -Wextra -g -o build/main.o src/main.c
I get the messagesrc/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.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 uglyEuro ~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