onyx-lang / onyx

✨ The compiler and developer toolchain for Onyx
https://onyxlang.io
BSD 2-Clause "Simplified" License
547 stars 19 forks source link

Example raylib: error on Windows 10 #141

Open renlite opened 3 months ago

renlite commented 3 months ago

Tried raylib pkg to install but get following errors.

$> onyx package add raylib
$>        ←[32mAdded  ←[39m'raylib' version 0.0.7
$> ←[0m

$> onyx package sync
$>        ←[32mFetch  ←[39mhttp://github.com/onyx-lang/pkg-raylib  0.0.7
$> ←[0m     ←[32mInstall  ←[39mRunning installation of './lib\github.com\onyx-lang\pkg-raylib'
$> ←[0m←[31m       Error  ←[39mFailed to build native library in ./lib\github.com\onyx-lang\pkg-raylib.
$> ←[0m←[31m       Error  ←[39mAborting sync.
$> ←[0m

The project structure and .kdl files seem to be correct.

image

renlite commented 3 months ago

Does this mean I could fetch the raylib.dll and put it into a special dirctory? What would be the steps to try it? image

brendanfh commented 3 months ago

Natively compiled packages are currently not really supported on Windows, for the reason in the comment you found. That will be addressed at some point when a good solution is found.

To get this working for now, you will have to go into the build.onyx file in the raylib package and comment out the code that compiles and deletes the C file. Then, from within the pkg-raylib folder, run the following command:

$ onyx run --generate-foreign-info build.onyx

That should make a C file in the current directory. You then need to compile that C file using cl.exe or msvc, along with the ray lib.lib file in the windows folder, into a DLL. That DLL can then be move to a folder called bin at the root of your project, and has to be called onyx_raylib.dll. I think everything should work then?

I've never actually done this on Windows, but I believe that should do the trick. Let me know if anything needs clarification.

renlite commented 3 months ago

Some notes: In build.onyx the path := module_path(#file); seems not to work. No file was created. After I changed the path manually for onyx_raylib.c in build.onyx the generate_c_bindings created the file.

Then I started Developer PowerShell for VS 2022. After some #include errors I changed the form of Lib-Path to local include: eg #include "raylib.h" and copied required files into raylib directory.

$ cd ->\lib\github.com\onyx-lang\pkg-raylib\raylib
$ cl.exe /LD .\onyx_raylib.c .\windows\raylib.lib
$
$ many errors for all referenced functions in raylib:
$ onyx_raylib.obj : error LNK2019: Reference to external symbol "_InitWindow" in function "___onyx_internal_onyx_raylib_InitWindow".
$ ...
$ onyx_raylib.dll : fatal error LNK1120: 499 no solved extern

An onyx_raylib.dll could not be created.

image

A lot of C libs offer a dynamic lib for many platforms that could be used to load dynamically and bind to the .dll (.so) from Onyx. In the module.onyx file of raylib pkg there are the C bindings already defined (#foreign block). Why is a separate generation of onyx_raylib.c necessary to include the C lib? Usually a language with C FFI loads a dynamic lib and uses bind coding in the other language to find and call the C functions.