wxGo icon indicating copy to clipboard operation
wxGo copied to clipboard

Debugging WxGo

Open araoko opened this issue 6 years ago • 5 comments

I wrote a class to wrap a wx.Window, i must be doing something wrong because my program stopped running. all i got was exit status 2. i cant write to stdout or stderr so its difficult for me to trace the problem. i decided to run a debugger and that failed too with error "go build github.com/dontpanic92/wxGo/wx: invalid flag in #cgo LDFLAGS: -Wl,--subsystem,windows". i am stuck. how do we debug our code if it fails before showing a window?

araoko avatar Apr 08 '18 01:04 araoko

I guess you are using Windows - on Linux, the stdout & stderr should be available by default. On Windows, if you want to print to stdout / stderr in a "Windows" subsystem (instead of the "Console" subsystem), you can use AllocConsole + freopen. Basically,

(untested code)

#include <windows.h>

void EnableStdOut()
{
  AllocConsole();
  freopen("CONOUT$", "w", stdout);
  freopen("CONOUT$", "w", stderr);
}

Regarding the debugger issue, I think it seems related with #44

dontpanic92 avatar Apr 08 '18 02:04 dontpanic92

I built (go build) and ran the program on 64bit windows 10 PC. how do i use your code in a Go application? i don't know AllocConsole or freopen. i am using the Go debugging feature of VSCode on windows 10

araoko avatar Apr 08 '18 02:04 araoko

You can use cgo to embed C code in a Go program. For example,

package main

// #include <...>
// void EnableStdOut()
// {
// ... /*More C Code Here*/
// }
import "C"

func main() {
  C.EnableStdOut()
}

VSCode supports adding environment variables when starting programs (I found the VSCode doc here). Try to add {"CGO_LDFLAGS_ALLOW": ".*"} to the env field

dontpanic92 avatar Apr 08 '18 04:04 dontpanic92

I could not get the debugging or writing to console work satisfactorily. i decided to use the log package to log to file with worked well. from the log i discovered that the error is from my call to a NewWindow func. the function returns just window and no error object, in fact the entire API has no error component. How do we detect/handle errors?

araoko avatar Apr 13 '18 11:04 araoko

Could you put together a small repo demonstrating your issue, @araoko, such that we can take a look at it ourselves?

Asday avatar Aug 17 '18 20:08 Asday