grepsuzette / wcwidth

Haxe port of Markus Kuhn's mk_wcwidth() function for determining space occupied by a Unicode character in a monospace font in a terminal
3 stars 0 forks source link

C++: Package name conflicts with POSIX method #2

Closed JerwuQu closed 2 years ago

JerwuQu commented 2 years ago

Hello again!

When compiling this package for C++, it will throw an error due to the resulting namespace conflicting with the POSIX wcwidth.

Error: In file included from <haxelib>/hxcpp/4,2,1/include/hxcpp.h:345:
include/wcwidth/Wcwidth.h:8:19: error: 'namespace wcwidth { }' redeclared as different kind of entity
    8 | HX_DECLARE_CLASS1(wcwidth,Wcwidth)
      |                   ^~~~~~~
<haxelib>/hxcpp/4,2,1/include/hx/Macros.h:18:48: note: in definition of macro 'HX_DECLARE_CLASS1'
   18 | #define HX_DECLARE_CLASS1(ns1,klass) namespace ns1 { HX_DECLARE_CLASS0(klass) }
      |                                                ^~~
In file included from <haxelib>/hxcpp/4,2,1/include/hxcpp.h:55:
/usr/include/wchar.h:367:12: note: previous declaration 'int wcwidth(wchar_t)'
  367 | extern int wcwidth (wchar_t __c) __THROW;
      |            ^~~~~~~
In file included from ./src/__boot__.cpp:4:
include/wcwidth/Wcwidth.h:10:11: error: 'namespace wcwidth { }' redeclared as different kind of entity
   10 | namespace wcwidth{
      |           ^~~~~~~
In file included from <haxelib>/hxcpp/4,2,1/include/hxcpp.h:55:
/usr/include/wchar.h:367:12: note: previous declaration 'int wcwidth(wchar_t)'
  367 | extern int wcwidth (wchar_t __c) __THROW;
      |            ^~~~~~~
./src/__boot__.cpp: In function 'void __boot_all()':
./src/__boot__.cpp:61:3: error: '::wcwidth' is not a class, namespace, or enumeration
   61 | ::wcwidth::Wcwidth_obj::__register();
      |   ^~~~~~~
./src/__boot__.cpp:78:3: error: '::wcwidth' is not a class, namespace, or enumeration
   78 | ::wcwidth::Wcwidth_obj::__boot();
      |   ^~~~~~~
Error: Build failed

I'm not sure if there's any solution for this right now besides renaming the package (which works, but might not be desirable...?)

Do you have any ideas?

grepsuzette commented 2 years ago

Sorry it closed the bug automatically, it seems to work if we have an extern to use the original wcwidth in C. Can you try if it works for you?

JerwuQu commented 2 years ago

I'm not sure what you mean (though I am quite new to Haxe). Are you talking about some sort of conditional compilation to use the original function rather than this library for when targeting C++?

JerwuQu commented 2 years ago

Ah my bad, I just noticed you had made commits targeting this issue!

Slight issue now is that the API differs and the wclength method no longer exists: Class<wcwidth.Wcwidth> has no field wclength.

I think this might be the right track though (in the case that both your implementation and the POSIX one behave the same.)

grepsuzette commented 2 years ago

Yes it's true. Since this is such a small lib I just removed the namespace altogether. It is a lib that is not supposed to evolve once it works, so people can easily put it in a package if they want.

Example:

using Wclength;
"ありがとうございました".wclength();  // gives 22

The internal wcwidth() is still available of course, you can see in the README.

About them behaving the same, it's supposed to. Otherwise it's a bug or the original has been updated (but I think it hasn't been in years). It's not difficult to have something like a -D wcwidth_force_haxe_implementation, we can have it later if you need.

JerwuQu commented 2 years ago

Sounds great! Thank you