Skip to contentSkip to conversation

wcwidth(3): wrong #define?

DISCUSSION Dirk Gouders 2 participants 6 messages Nov 5, 2025 – Nov 6, 2025 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore
#0 lore raw

Hi Alex,

I'm playing with a program to calculate wide character widths and gcc complains when I use wcwidth(3) according to the manpage:

------------------------------------------------------------------------
wcwidth(3) Library Functions Manual wcwidth(3)

NAME
wcwidth - determine columns needed for a wide character

LIBRARY
Standard C library (libc, -lc)

SYNOPSIS

       #define _XOPEN_SOURCE       /* See feature_test_macros(7) */
       #include <wchar.h>

------------------------------------------------------------------------

Looking at wchar.h explains the problem:

------------------------------------------------------------------------
/* The following functions are extensions found in X/Open CAE. */ #ifdef __USE_XOPEN
/* Determine number of column positions required for C. */ extern int wcwidth (wchar_t __c) __THROW;
------------------------------------------------------------------------

So, if I replace "#define _XOPEN_SOURE" with "#define _USE_XOPEN", gcc stops complaining.

I would have prepared a patch but somehow expect that there are situations when "#include _XOPEN_SOURCE" might be correct...

Regards,

Dirk

#1 lore raw

Hi Dirk,

Quoted 4 lines · Dirk Gouders

Hi Alex,

I'm playing with a program to calculate wide character widths and gcc complains when I use wcwidth(3) according to the manpage:

What's the exact diagnostic you're seeing? I'm not seeing any issues:

	alx@devuan:~/tmp$ cat foo.c 
	#define _XOPEN_SOURCE
	#include <wchar.h>
	int
	main(void)
	{
		wcwidth(0);
		return 0;
	}
	alx@devuan:~/tmp$ gcc -Wall -Wextra -std=c89 foo.c 
	alx@devuan:~/tmp$ gcc -Wall -Wextra foo.c 
	alx@devuan:~/tmp$ 
Quoted 23 lines

------------------------------------------------------------------------
wcwidth(3) Library Functions Manual wcwidth(3)

NAME
wcwidth - determine columns needed for a wide character

LIBRARY
Standard C library (libc, -lc)

SYNOPSIS

       #define _XOPEN_SOURCE       /* See feature_test_macros(7) */
       #include <wchar.h>

------------------------------------------------------------------------

Looking at wchar.h explains the problem:

------------------------------------------------------------------------
/* The following functions are extensions found in X/Open CAE. */ #ifdef __USE_XOPEN
/* Determine number of column positions required for C. */ extern int wcwidth (wchar_t __c) __THROW;
------------------------------------------------------------------------

Quoted 2 lines

So, if I replace "#define _XOPEN_SOURE" with "#define _USE_XOPEN", gcc stops complaining.

_USE_XOPEN is a glibc internal that users shouldn't set.

Quoted 7 lines

I would have prepared a patch but somehow expect that there are situations when "#include _XOPEN_SOURCE" might be correct...

Regards,

Dirk

Have a lovely night!
Alex

Signature · 2 lines
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).
#2 lore raw

Hi Alex,

Quoted 7 lines · Dirk Gouders
Quoted 4 lines

Hi Alex,

I'm playing with a program to calculate wide character widths and gcc complains when I use wcwidth(3) according to the manpage:

What's the exact diagnostic you're seeing? I'm not seeing any issues:

here's my program (meanwhile, I read that _XOPEN_SOURCE should appear before _any_ header and that indeed would help):

--------------------------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define _XOPEN_SOURCE
#include <wchar.h>
#include <locale.h>

int main(int argc, char *argv[])

{
	size_t i = 0;

	char buffer[1024 * 1024];

	setlocale(LC_ALL, "");

	ssize_t ret = read(STDIN_FILENO, buffer, 1024 * 1024);

	if (ret < 0) {
		perror("read(2): ");
		exit(1);
	}

	printf("Read %ld bytes.\n", ret);

	while (i < ret) {
		wchar_t pwc[64];
		size_t len = mbrtowc(pwc, buffer + i, ret - i, NULL);

		printf("len = %ld\n", len);

		if (buffer[i] == '\0')
			break;

		if (len == 0)
			break;

		if (len < 0) {
			fprintf(stderr, "mbrtowc failure: %ld\n", len);
			exit(1);
		}

		printf("Character %lc starting at %ld needs %d cols.\n",
		       pwc[0], i, wcwidth(pwc[0]));

		i += len;
	}

	exit(0);
}

--------------------------------------------------------------------------------

And compiling it gives:

--------------------------------------------------------------------------------

$ make
gcc -o wchar-cols wchar-cols.c
wchar-cols.c: In function ‘main’:
wchar-cols.c:43:35: error: implicit declaration of function ‘wcwidth’ [-Wimplicit-function-declaration]
   43 |                        pwc[0], i, wcwidth(pwc[0]));
      |                                   ^~~~~~~
make: *** [Makefile:2: wchar-cols] Error 1
--------------------------------------------------------------------------------

Regards,

Dirk

Quoted 52 lines
	alx@devuan:~/tmp$ cat foo.c 
	#define _XOPEN_SOURCE
	#include <wchar.h>
	int
	main(void)
	{
		wcwidth(0);
		return 0;
	}
	alx@devuan:~/tmp$ gcc -Wall -Wextra -std=c89 foo.c 
	alx@devuan:~/tmp$ gcc -Wall -Wextra foo.c 
	alx@devuan:~/tmp$ 
Quoted 23 lines

------------------------------------------------------------------------
wcwidth(3) Library Functions Manual wcwidth(3)

NAME
wcwidth - determine columns needed for a wide character

LIBRARY
Standard C library (libc, -lc)

SYNOPSIS

       #define _XOPEN_SOURCE       /* See feature_test_macros(7) */
       #include <wchar.h>

------------------------------------------------------------------------

Looking at wchar.h explains the problem:

------------------------------------------------------------------------
/* The following functions are extensions found in X/Open CAE. */ #ifdef __USE_XOPEN
/* Determine number of column positions required for C. */ extern int wcwidth (wchar_t __c) __THROW;
------------------------------------------------------------------------

Quoted 2 lines

So, if I replace "#define _XOPEN_SOURE" with "#define _USE_XOPEN", gcc stops complaining.

_USE_XOPEN is a glibc internal that users shouldn't set.

Quoted 7 lines

I would have prepared a patch but somehow expect that there are situations when "#include _XOPEN_SOURCE" might be correct...

Regards,

Dirk

Have a lovely night!
Alex

#3 lore raw

Hi Dirk,

Quoted 8 lines · Dirk Gouders
Quoted 5 lines · Dirk Gouders
Quoted 2 lines

I'm playing with a program to calculate wide character widths and gcc complains when I use wcwidth(3) according to the manpage:

What's the exact diagnostic you're seeing? I'm not seeing any issues:

here's my program (meanwhile, I read that _XOPEN_SOURCE should appear before _any_ header and that indeed would help):

Yup, it should be the first thing. See feature_test_macros(7):

$ MANWIDTH=72 man ftm | head -n20
featur..._macros(7) Miscellaneous Information Manualfeatur..._macros(7)

NAME
feature_test_macros - feature test macros

DESCRIPTION
Feature test macros allow the programmer to control the defini‐ tions that are exposed by system header files when a program is compiled.

NOTE: In order to be effective, a feature test macro must be de‐ fined before including any header files. This can be done either in the compilation command (cc -DMACRO=value) or by defining the
macro within the source code before including any headers. The
requirement that the macro must be defined before including any header file exists because header files may freely include one an‐ other. Thus, for example, in the following lines, defining the _GNU_SOURCE macro may have no effect because the header <abc.h> itself includes <xyz.h> (POSIX explicitly allows this):

Cheers,
Alex

Signature · 2 lines
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).
#4 lore raw

Hi Alex,

Quoted 7 lines · Alejandro Colomar

Hi Dirk,

Quoted 4 lines · Dirk Gouders
Quoted 4 lines · Dirk Gouders
Quoted 2 lines

I'm playing with a program to calculate wide character widths and gcc complains when I use wcwidth(3) according to the manpage:

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

What's the exact diagnostic you're seeing? I'm not seeing any issues:

here's my program (meanwhile, I read that _XOPEN_SOURCE should appear before _any_ header and that indeed would help):

Yup, it should be the first thing. See feature_test_macros(7):

thank you, you helped me with two problems:

1) If you think you read enough and should talk, go back and read twice as much ;-)

2) For a very long time, I always read patterns like

#define FTM
#include <example.h>

as "Put FTM as close as possible to example.h." but it seems that pattern actually means "Put FTM as far away as possible from example.h."

Thanks for your kind explanation.

Regards,

Dirk

#5 lore raw

Hi Dirk,

Quoted 10 lines · Dirk Gouders
Quoted 10 lines · Dirk Gouders
Quoted 8 lines
Quoted 5 lines · Dirk Gouders
Quoted 2 lines

I'm playing with a program to calculate wide character widths and gcc complains when I use wcwidth(3) according to the manpage:

What's the exact diagnostic you're seeing? I'm not seeing any issues:

here's my program (meanwhile, I read that _XOPEN_SOURCE should appear before _any_ header and that indeed would help):

Quoted 6 lines
Quoted 1 line

Yup, it should be the first thing. See feature_test_macros(7):

thank you, you helped me with two problems:

1) If you think you read enough and should talk, go back and read twice as much ;-)

Yup. Although there's so much written information that sometimes it's hard to find the one you need. Sometimes, asking the right person is easier. :)

Quoted 8 lines

2) For a very long time, I always read patterns like

#define FTM
#include <example.h>

as "Put FTM as close as possible to example.h." but it seems that pattern actually means "Put FTM as far away as possible from example.h."

Yup, there's no order. I use:

	// feature test macros
	...

	// libc headers
	...  // In alphabetic order

	// other libraries
	...  // In alphabetic order

	// project includes
	...  // In alphabetic order
Quoted 1 line

Thanks for your kind explanation.

You're welcome!

Quoted 3 lines

Regards,

Dirk

Have a lovely day!
Alex

Signature · 2 lines
<https://www.alejandro-colomar.es>
Use port 80 (that is, <...:80/>).