- Install
msys2(because we needmingw64,gccandlibws2_32.a) - Open
msys2ormingw64and runpacman -S mingw64/mingw-w64-x86_64-mruby - Get MRuby and extract the folder
- Run
makeinside the foldermruby-3.3.0, there will be a new folder called/buildafter the compilation, we need the file at/build/host/lib/libmruby.a - Compile using the following command
gcc main.c -I"mruby-3.3.0/include" "mruby-3.3.0/build/host/lib/libmruby.a" "C:\msys64\mingw64\lib\libws2_32.a" -lm -o mainor substitue"C:\msys64\mingw64\lib\libws2_32.a"with-lws2_32 - Run the compiled binary
main.exe - Done
Using Docker
- Get MRuby and extract the folder
docker run --rm -it -v .:/app -w /app appplant/mruby-cli shin the folder- Now you're in the container shell, run
rake - In our case, looking at the path (on Windows explorer)
C:\Users\USER\Downloads\mruby-3.3.0\build\host\lib, you'll findlibmruby.a. That is what you are going to need when compiling. - Now when compiling (still in container shell), go to your other project where the
main_rb_file.cis and run the followinggcc main_rb_file.c -I"C:\Users\USER\Downloads\mruby-3.3.0\include" "C:\Users\USER\Downloads\mruby-3.3.0\build\host\lib\libmruby.a" -lm -pthread -o main(make sure to update the paths correctly) - Done, you should have your main file now which can be executed on a unix machine
- Extract the
include/folder andlibmruby.afrommruby-3.3.0\build\host\liband put these two into a new directory togheter withmain_rb_file.candcode.rb - Run
docker run --rm -it -v .:/app -w /app appplant/mruby-cli shin that folder - After that you can simply run
gcc main_rb_file.c -I"include/" libmruby.a -lm -pthread -o main_unix
The sweet thing here is that if you repeat the steps before the bonus but runned MRUBY_CONFIG=cross-mingw rake instead on step 3 and did the rest (including the bonus steps). You would be able to run gcc main_rb_file.c -I"include" win/lib/libmruby.a -lws2_32 -lm -o main_win to compile for windows.
To shorten the steps even further, you can simply wrap it in a empheral container: docker run --rm -it -v .:/app -w /app appplant/mruby-cli gcc main_rb_file.c -I"include" "unix/lib/libmruby.a" -lm -pthread -o main_unix
- Create a folder named something, in our case it is "mrubyyy"
- Add the files
main_rb_file.candcode.rb(you can find them below) to the folder "mrubyyy" - Download mruby-3.3.0 to mrubyyy and add it as subfolder, like so: mrubyyy/mruby-3.3.0
- Run
docker run --rm -it -v .:/app -w /app appplant/mruby-cli shin root folder (mrubyyy) - In the container cd into the folder mruby-3.3.0:
cd mruby-3.3.0, you should now be in the subfolder "mruby-3.3.0" - Compile using windows config
MRUBY_CONFIG=cross-mingw rake - This is optional but run the tests make sure everything went well,
rake test - change directory to the root folder "mrubyyy",
cd .. - Run the following command to compile the project to Windows binary
x86_64-w64-mingw32-gcc-posix main_rb_file.c -I"mruby-3.3.0/include" "mruby-3.3.0/build/cross-mingw/lib/libmruby.a" -lws2_32 -lm -o main_win - Done, the binary "main_win.exe" should be executable on a Windows machine now
Check out mruby-cli, it setups a project structure where you add your code and then it compiles your ruby code down to multiple platforms.
I also suggest adding the --static flag here https://github.com/hone/mruby-cli/blob/master/build_config.rb#L20 like so:
[conf.cc, conf.cxx, conf.linker].each do |cc|
cc.flags << "--static"
end