Author Topic: Implicit wxString to char* conversion in src/sdk/projectloader.cpp  (Read 5758 times)

Offline jdx

  • Multiple posting newcomer
  • *
  • Posts: 11
For the first time in my life I am trying to build C::B. My setup is Windows 10,  current MSYS2/MinGW (including wxWidgets 3.2.2.1) and current nightly C::B build. I started C::B, opened CodeBlocks_wx32_64.cbp (BTW. info in <CB_ROOT>\BUILD is outdated), modified wxWidgets include paths and after some time I got the following strange error(s):

Code
g++.exe -Wall -std=gnu++11 -m64 -g -pipe -mthreads -fmessage-length=0 -fexceptions -Winvalid-pch -DHAVE_W32API_H -D__WXMSW__ -DWXUSINGDLL -DcbDEBUG -DCB_PRECOMP -DwxUSE_UNICODE -D_WIN64 -Woverloaded-virtual -DEXPORT_LIB -DEXPORT_EVENTS -DWXMAKINGDLL_SCI -iquote.objs32_64\include -I.objs32_64\include -I. -IC:\Tools\MSYS\mingw64\include\wx-3.2 -IC:\Tools\MSYS\mingw64\lib\wx\include\msw-unicode-3.2 -Isdk\wxscintilla\include -Iinclude\tinyxml -Iinclude -Iinclude\tinyxml -Iinclude\scripting\include -Isdk\mozilla_chardet\include -Isdk\mozilla_chardet\include\mfbt -Isdk\mozilla_chardet\include\nsprpub\pr\include -Isdk\mozilla_chardet\include\xpcom -Isdk\mozilla_chardet\include\xpcom\base -Isdk\mozilla_chardet\include\xpcom\glue -Isdk\scripting\bindings -c D:\Works\CodeBlocks\src\sdk\projectloader.cpp -o .objs32_64\sdk\projectloader.o
D:\Works\CodeBlocks\src\sdk\projectloader.cpp: In member function 'bool ProjectLoader::ExportTargetAsProject(const wxString&, const wxString&, TiXmlElement*)':
D:\Works\CodeBlocks\src\sdk\projectloader.cpp:1776:30: error: no matching function for call to 'TiXmlElement::SetAttribute(const char [9], wxString)'
 1776 |         element->SetAttribute("wildcard", glob.GetWildCard());
      |         ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from D:\Works\CodeBlocks\src\include\configmanager.h:15,
                 from D:\Works\CodeBlocks\src\include\sdk_common.h:121,
                 from D:\Works\CodeBlocks\src\include\sdk_precomp.h:13:
include\tinyxml/tinyxml.h:1062:14: note: candidate: 'void TiXmlElement::SetAttribute(const char*, const char*)'
 1062 |         void SetAttribute( const char* name, const char * _value );
      |              ^~~~~~~~~~~~
include\tinyxml/tinyxml.h:1062:59: note:   no known conversion for argument 2 from 'wxString' to 'const char*'
 1062 |         void SetAttribute( const char* name, const char * _value );
      |                                              ~~~~~~~~~~~~~^~~~~~
include\tinyxml/tinyxml.h:1082:14: note: candidate: 'void TiXmlElement::SetAttribute(const char*, int)'
 1082 |         void SetAttribute( const char * name, int value );
      |              ^~~~~~~~~~~~
include\tinyxml/tinyxml.h:1082:51: note:   no known conversion for argument 2 from 'wxString' to 'int'
 1082 |         void SetAttribute( const char * name, int value );
      |                                               ~~~~^~~~~
D:\Works\CodeBlocks\src\sdk\projectloader.cpp:1778:30: error: no matching function for call to 'TiXmlElement::SetAttribute(const char [3], wxString)'
 1778 |         element->SetAttribute("id", wxString::Format("%lld", glob.GetId()));
      |         ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include\tinyxml/tinyxml.h:1062:14: note: candidate: 'void TiXmlElement::SetAttribute(const char*, const char*)'
 1062 |         void SetAttribute( const char* name, const char * _value );
      |              ^~~~~~~~~~~~
include\tinyxml/tinyxml.h:1062:59: note:   no known conversion for argument 2 from 'wxString' to 'const char*'
 1062 |         void SetAttribute( const char* name, const char * _value );
      |                                              ~~~~~~~~~~~~~^~~~~~
include\tinyxml/tinyxml.h:1082:14: note: candidate: 'void TiXmlElement::SetAttribute(const char*, int)'
 1082 |         void SetAttribute( const char * name, int value );
      |              ^~~~~~~~~~~~
include\tinyxml/tinyxml.h:1082:51: note:   no known conversion for argument 2 from 'wxString' to 'int'
 1082 |         void SetAttribute( const char * name, int value );
      |                                               ~~~~^~~~~

The error is "strange" because it is so obvious that I am wondering why it is still there. It looks like the author(s) must have relied on some implicit wxString -> char* conversion. Anyway, mb_str() seems to solve the issue:
Code
--- src/sdk/projectloader.cpp	(revision 13254)
+++ src/sdk/projectloader.cpp (working copy)
@@ -1773,9 +1773,9 @@
     for (const ProjectGlob& glob : m_pProject->GetGlobs())
     {
         TiXmlElement *element = AddElement(prjnode, "UnitsGlob", "directory", glob.GetPath());
-        element->SetAttribute("wildcard", glob.GetWildCard());
+        element->SetAttribute("wildcard", glob.GetWildCard().mb_str());
         element->SetAttribute("recursive", glob.GetRecursive() ? 1 : 0);
-        element->SetAttribute("id", wxString::Format("%lld", glob.GetId()));
+        element->SetAttribute("id", wxString::Format("%lld", glob.GetId()).mb_str());
         element->SetAttribute("addToProject", glob.GetAddToProject() ? 1 : 0);
         const wxArrayString targets = glob.GetTargets();
         if (targets.size() > 0)

Offline PB

  • Multiple posting newcomer
  • *
  • Posts: 57
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #1 on: April 10, 2023, 09:38:42 am »
It looks like the author(s) must have relied on some implicit wxString -> char* conversion.

For better or worse, wxString is convertable to char* by default, see https://docs.wxwidgets.org/stable/overview_string.html#overview_string_implicitconv

Perhaps you built wxWidgets with wxUSE_UNSAFE_WXSTRING_CONV defined to 0?
« Last Edit: April 10, 2023, 09:43:56 am by PB »

Offline jdx

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #2 on: April 10, 2023, 11:29:49 am »
I did not build wxWidgets. I use standard MinGW64 packages:
Code
$ pacman -Ss wxwidgets|grep mingw64
mingw64/mingw-w64-x86_64-wxPython 4.2.0-1
mingw64/mingw-w64-x86_64-wxPython4.0 4.0.7.2-4
mingw64/mingw-w64-x86_64-wxwidgets3.0-msw 3.0.5.1-16
mingw64/mingw-w64-x86_64-wxwidgets3.0-msw-libs 3.0.5.1-16
mingw64/mingw-w64-x86_64-wxwidgets3.1-msw 3.1.7-6
mingw64/mingw-w64-x86_64-wxwidgets3.1-msw-libs 3.1.7-6
mingw64/mingw-w64-x86_64-wxwidgets3.2-common 3.2.2.1-2 [installed]
mingw64/mingw-w64-x86_64-wxwidgets3.2-common-libs 3.2.2.1-2 [installed]
mingw64/mingw-w64-x86_64-wxwidgets3.2-gtk3 3.2.2.1-2
mingw64/mingw-w64-x86_64-wxwidgets3.2-gtk3-libs 3.2.2.1-2
mingw64/mingw-w64-x86_64-wxwidgets3.2-msw 3.2.2.1-2 [installed]
mingw64/mingw-w64-x86_64-wxwidgets3.2-msw-cb_headers 3.2.2.1-2 [installed]
mingw64/mingw-w64-x86_64-wxwidgets3.2-msw-libs 3.2.2.1-2 [installed]

Also I did not modify preprocessor defines in C::B project settings, only search paths and library names (MinGW64 does not use monolithic wxWidgets). Either way, relying on implicit conversion is rather bad practice.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #3 on: April 10, 2023, 01:56:05 pm »
How did you try to build Code::Blocks?

Using CB and CB Projects?
Or configure/make?
Or other method?

I have not tried to build C::B for about an year; and, I forget if the build had error or not.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline jdx

  • Multiple posting newcomer
  • *
  • Posts: 11
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #4 on: April 10, 2023, 02:06:10 pm »
How did you try to build Code::Blocks?

Using CB and CB Projects?
Yes.

Or other method?
Is there another method? I'm dreaming of cmake.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #5 on: April 10, 2023, 02:11:31 pm »
How did you try to build Code::Blocks?

Using CB and CB Projects?
Yes.

Or other method?
Is there another method? I'm dreaming of cmake.

Another poster was trying to add cmake as a build method (I have no idea which OS); I never got configure/make build to result in a working C::B.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #6 on: April 10, 2023, 02:16:46 pm »
What patches are you using on the CB project? And/or, which CB Projects are you using?

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #7 on: April 10, 2023, 02:42:15 pm »
I got the same error after this patch/change. Tim S.

Now researching the proper code fix.

Tim S.

Code
Index: src/CodeBlocks_wx32_64.cbp
===================================================================
--- src/CodeBlocks_wx32_64.cbp  (revision 13254)
+++ src/CodeBlocks_wx32_64.cbp  (working copy)
@@ -649,7 +649,7 @@
                        <Add option="-DwxUSE_UNICODE" />
                        <Add option="-D_WIN64" />
                        <Add directory="$(#WX32_64.include)" />
-                       <Add directory="$(#WX32_64.lib)/gcc_dll$(WX_CFG)/msw$(WX_SUFFIX)" />
+                       <Add directory="$(#WX32_64.lib)/wx/include/msw-unicode-3.2" />
                        <Add directory="sdk/wxscintilla/include" />
                        <Add directory="include/tinyxml" />
                </Compiler>
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #8 on: April 10, 2023, 03:22:31 pm »
Looks like the changes are too much for me to figure out and do quickly.

Edit: Found some 3 year old work updating it to wxWidgets 3.2.x.y; could not find the more recent work likely was on my hard-drive that crashed.

Tim S.
« Last Edit: April 11, 2023, 04:11:49 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #9 on: April 11, 2023, 04:42:05 am »
It looks like the author(s) must have relied on some implicit wxString -> char* conversion.

For better or worse, wxString is convertable to char* by default, see https://docs.wxwidgets.org/stable/overview_string.html#overview_string_implicitconv

Perhaps you built wxWidgets with wxUSE_UNSAFE_WXSTRING_CONV defined to 0?

Noop, wxUSE_UNSAFE_WXSTRING_CONV is 1; but both wxUSE_STL and wxUSE_STD_STRING_CONV_IN_WXSTRING are also one.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #10 on: April 11, 2023, 05:55:05 am »
I plan to add the patch files I am using to this post.
I also used the patch to src/sdk/projectloader.cpp that the OP posted. I forgot the wxcode stuff I knew it might be correct or it might not.

msys2_wx_CodeBlocks_wx32_64.cbp.patch contains a patch to modify CodeBlocks_wx32_64.cbp so it uses MSys2 MINGW64 wxwidgets 3.2 package.

MSys2 Packages that are likely needed; only tested using MINGW64 command window.
Code
pacman -S --noconfirm --needed zip;
pacman -S --noconfirm --needed rsync;
pacman -S --noconfirm --needed subversion;
pacman -S --noconfirm --needed ${MINGW_PACKAGE_PREFIX}-gcc;
pacman -S --noconfirm --needed ${MINGW_PACKAGE_PREFIX}-cppcheck;
pacman -S --noconfirm --needed ${MINGW_PACKAGE_PREFIX}-boost;
pacman -S --noconfirm --needed ${MINGW_PACKAGE_PREFIX}-drmingw;
pacman -S --noconfirm --needed ${MINGW_PACKAGE_PREFIX}-wxwidgets3.2-msw;
pacman -S --noconfirm --needed ${MINGW_PACKAGE_PREFIX}-wxwidgets3.2-msw-cb_headers;

Commands used after CB core project CodeBlocks_wx32_64.cbp builds
Code
./update 32_msys2
cd output32_msys2
./codeblocks.exe

Link to the normal build directions https://wiki.codeblocks.org/index.php/Installing_Code::Blocks_from_source_on_Windows
Note: Those directions are not correct for use with these patch files; but, they might help. And, if you have never built CB the normal way it will be hard to build it this very unusual way.

Build finished;
Code
Name                    : Code::Blocks
Version                 : svn-r13254
SDK Version             : 2.24.0
Scintilla Version       : 3.7.5
Author                  : The Code::Blocks Team
E-mail                  : info@codeblocks.org
Website                 : https://www.codeblocks.org
OS                      : Windows 10 (build 19045), 64-bit edition
Scaling factor          : 1.250000
Detected scaling factor : 1.250000
Display PPI             : 120x120
Display count           : 2
Display 0 (\\.\DISPLAY1): XY=[0,0]; Size=[1280,1024]; Primary
Display 1 (\\.\DISPLAY2): XY=[1280,0]; Size=[1920,1080];

wxWidgets Library (wxMSW port)
Version 3.2.2 (Unicode: wchar_t, debug level: 1),
compiled at Mar  8 2023 20:18:16

Runtime version of toolkit used is 10.0.

Tim S.
« Last Edit: April 11, 2023, 04:57:25 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #11 on: April 11, 2023, 04:48:19 pm »
Compilation fails when wxUSE_STL == 1, this must be fixed because 1 will be the default value sooner or later.
 I will do it in a while.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #12 on: April 12, 2023, 01:24:34 pm »
For me, I think the code

Code
glob.GetWildCard().mb_str()

will depends on the system's Local setting. For example, either in Chinese or other languages, the mb_str() will return different byte arrays. Is that correct?
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #13 on: April 12, 2023, 02:58:37 pm »
For me, I think the code

Code
glob.GetWildCard().mb_str()

will depends on the system's Local setting. For example, either in Chinese or other languages, the mb_str() will return different byte arrays. Is that correct?

I was thinking that; but, since a different solution using c_str() was used in CB SVN the issue is moot.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #14 on: April 12, 2023, 09:32:07 pm »
So suggestions to fix this?
Can someone do this? I have at the moment no time to look into this....

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: Implicit wxString to char* conversion in src/sdk/projectloader.cpp
« Reply #15 on: April 13, 2023, 10:33:23 am »
wxWidgets documentation for wxString states:
Quote
If you built wxWidgets with wxUSE_STL set to 1, the implicit conversions to both narrow and wide C strings are disabled and replaced with implicit conversions to std::string and std::wstring.
The implicit conversions uses c_str(). If the call to TinyXML needs multibyte then mb_str() should have been used from start.