Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot compile examples/genuimacro/threads.nim #24

Open
AllenDang opened this issue Jan 22, 2021 · 9 comments
Open

Cannot compile examples/genuimacro/threads.nim #24

AllenDang opened this issue Jan 22, 2021 · 9 comments

Comments

@AllenDang
Copy link

AllenDang commented Jan 22, 2021

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
Copy link
Owner

PMunch commented Jan 22, 2021

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
Copy link
Author

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
Copy link
Owner

PMunch commented Jan 22, 2021

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
Copy link

joubertnel commented Jan 25, 2021

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:

  • Big Sur on M1
  • wxWidgets 3.1.5 (built from source)

@joubertnel
Copy link

joubertnel commented Jan 26, 2021

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
Copy link
Owner

PMunch commented Jan 26, 2021

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
Copy link
Author

@joubertnel .emit is a good solution!

@joubertnel
Copy link

joubertnel commented Jan 30, 2021

Found a solution for unicode: #23 (comment)

@nixfreak
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants