PMunch / wxnim

Nim wrapper for wxWidgets.
MIT License
94 stars 11 forks source link

Cannot compile examples/genuimacro/threads.nim #24

Open AllenDang opened 3 years ago

AllenDang commented 3 years ago

I cloned this repo. cd to exmaples/genuimacro/threads.nim, invoke nim cpp -r threads.nim, and got following error.

/Users/allendang/Downloads/wxnim/examples/genuimacro/threads.nim(7, 11) Error: undeclared identifier: 'Thread'

I googled the reason, and confirmed that nim.cfg has --threads:on.

I've tried to add --threads:on directly in compiler option like this, nim cpp -r threads.nim --threads:on, error remains.

My os is MacOS big sur.

PMunch commented 3 years ago

Not sure why the cfg file doesn't work, but the --threads:on should go before -r. The way you have it now you're passing it as an option to the example program.

AllenDang commented 3 years ago

nim cpp -r --threads:on threads.nim reports new error...

/Users/allendang/.cache/nim/threads_d/@mthreads.nim.cpp:496:11: error: cannot initialize a variable of type 'NCSTRING' (aka 'char *') with an rvalue of type 'const char *'
        NCSTRING T1_ = entryText.c_str().AsChar();
                 ^     ~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Users/allendang/.cache/nim/threads_d/@mthreads.nim.cpp:17:
In file included from /usr/local/include/wx-3.1/wx/wx.h:24:
/usr/local/include/wx-3.1/wx/event.h:479:30: error: cannot initialize a parameter of type 'wxCommandEvent *' with an rvalue of type 'wxEventFunctorFunction<int, wxCommandEvent>::EventClass *' (aka 'wxEvent *')
        CheckHandlerArgument(static_cast<EventClass *>(NULL));
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PMunch commented 3 years ago

Hmm, that's weird. Running the exact same thing works just fine for me. Might be a difference between the OSX and Linux versions of WxWidgets? This is related to your other issue with the strings by the way.

joubertnel commented 3 years ago

While I've added macOS installation/usage instructions to my fork of wxnim (https://github.com/joubertnel/wxnim), I've also encountered the string issue with the threads example. Starting to dig in to figure out what's up.

My setup:

joubertnel commented 3 years ago

The C++ compiler rejects implicit conversions from const char* to char* because it is unsafe. Here's one discussion talking about it: https://stackoverflow.com/questions/48554625/vs-2017-doesnt-implicitly-convert-const-char-to-char/48554786 @PMunch I wonder whether you don't see this issue because your compiler is set to a more permissive setting.

@AllenDang here's a solution that takes advantage of Nim's emit pragma (embedding C++ code):

func stringFromWxString(s: WxString): cstring =     
    # See wxString::c_str documentation - https://docs.wxwidgets.org/3.0/classwx_string.html#a6418ec90c6d4ffe0b05702be1b35df4f
    # C++ const_cast conversion - https://en.cppreference.com/w/cpp/language/const_cast
    # Nim emit pragma - https://nim-lang.github.io/Nim/manual.html#implementation-specific-pragmas-emit-pragma

    {.emit: """
    `result` = const_cast<char*>((const char*)s.c_str());
    """.}  

proc buttonClicked(e: var WxCommandEvent)  =
    var                  
        theText = stringFromWxString(textCtrl.getValue())

    echo theText

Note: the above works for ASCII, but doesn't work for Unicode characters. I'm still trying to figure out how best to accomplish that. So far, have gotten this code but need to fix type issues or come up with a different solution.

func unicodeStringFromWxString(s: WxString): string =    
    var 
        size = s.len()           

    {.emit: """          
    wchar_t* temp = const_cast<wchar_t*>((const wchar_t*)s.wc_str());        
    wscpy(`result`, temp);
    """.}
PMunch commented 3 years ago

Yes, this is a Clang vs. GCC thing where Clang is a bit more strict with const pointers. I tried to look around for some flag you could pass to Clang to make it behave more like GCC, but to no avail. I think a solution similar to what you're doing there is the closest we can get to a proper solution.

AllenDang commented 3 years ago

@joubertnel .emit is a good solution!

joubertnel commented 3 years ago

Found a solution for unicode: https://github.com/PMunch/wxnim/issues/23#issuecomment-770243987

nixfreak commented 2 years ago

I am able to compile using nim cpp -r controlgallery.nim , this is after building wx myself