DEV Community

Discussion on: Managing Windows windows within mruby Part 2: Creating a window from mruby

Collapse
 
lukestuts profile image
Luke Stutters

Thanks for this very interesting series. I've followed along up to this point but I can't build it after adding #include "mruby.h". I get lots of these errors:

C:\workdir>cl /Zi /std:c++latest /GR- /nologo /analyze main.cpp /Imruby /link /debug user32.lib /SUBSYSTEM:windows /ENTRY:mainCRTStartup libmruby.lib libmruby_core.lib
main.cpp
C:\workdir\mruby\string.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
C:\workdir\mruby\string.h(26): error C3646: 'len': unknown override specifier
Enter fullscreen mode Exit fullscreen mode

I've tried mruby 3 and mruby 2.1.2 with the same result.

It looks like the C headers are not being parsed correctly by cl.exe but I don't know enough to work out the underlying problem.

Collapse
 
lukestuts profile image
Luke Stutters

I got the mruby headers and .lib files working on my Windows 10 PC using the following process:

  • After downloading the mruby source from github, check out the 2.1.2 version as follows:
git checkout tags/2.1.2
Enter fullscreen mode Exit fullscreen mode
  • Build mruby as described in the article in a x64 Native Tools Command Prompt for VS2019 window (ruby minirake)
  • Copy libmruby.lib and libmruby_core.lib to the working directory as described in the article
  • Copy mruby\include to the working directory as described in the article. The mruby.h file and the mruby folder should be in the same directory as the main.cpp file.

Then, I needed to do some things differently to get the article code to compile. The differences needed on my system were:

  • Add /MD to the compiler options, since the ruby minirake task is compiling libmruby.lib and libmruby_core.lib with this option. (I worked this out by looking at the ruby minirake --verbose output. The compiler messages were not helpful.)
  • Add ws2_32.lib to the end of the compile command

My minimal test program for mruby is:

// test.cpp
#include "mruby.h"
#include "mruby/compile.h"
int main() {
    mrb_state *mrb = mrb_open();
    mrb_load_string(mrb, "5.times { puts 'derp' }");
    mrb_close(mrb);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

and the .bat file to compile it is:

rem Build test.cpp
cl /Zi /MD /GR- /nologo /I"%~dp0." test.cpp /link user32.lib libmruby.lib libmruby_core.lib ws2_32.lib
Enter fullscreen mode Exit fullscreen mode

I needed to specify the absolute path of the working directory with the /I option on my system using the %~dp0.

The last thing that confused me is that adding /SUBSYSTEM:windows /ENTRY:mainCRTStartup to the end of the compile command will hide all printf or puts output from mruby, so I recommend leaving these out while debugging.