cisco / ChezScheme

Chez Scheme
Apache License 2.0
6.95k stars 982 forks source link

Build/Embed Chez Scheme with larger C/C++ program #851

Open ValorZard opened 1 month ago

ValorZard commented 1 month ago

Hello! I've been diving into the world of Lisp/Scheme recently, and I wanted to see if I could embed chez scheme into a c++ program. I've see these two resources before on the web: https://www.reddit.com/r/scheme/comments/a3oogf/questions_on_embedding_chez_scheme_into_a_c_app/ed2b3us/ and https://cisco.github.io/ChezScheme/csug9.5/foreign.html#g34 but it's still not really clear to me how to build everything together.

I tried following the steps in the reddit post, and got far enough to link the dll mentioned but then I would get this error: image Is there an easier way to mix chez and C++ together? It would be nice if I could just have Chez as a git submodule or something, and build everything together with CMake.

mflatt commented 1 month ago

It looks like linking fails because zlib and lz4 libraries are missing. Those libraries are referenced by functions in csv1000mt.lib, and the librareies are created when building Chez Scheme from source — but I see that the libraries are not included in the boot directory of the release archive, and maybe they should be. Meanwhile, you can download or build those separately, you could build Chez Scheme from source to get them, or you can try using some copies that I put here: https://users.cs.utah.edu/~mflatt/tmp/ta6nt/

The bin directory of the distribution has a DLL and csv1000.lib for linking to the DLL. That DLL incorporates zlib and lz4, so there's no external dependency, but it means that you'd need to keep the DLL with your executable.

LiberalArtist commented 3 weeks ago

There is a somewhat-related problem on Unix-like systems (at least, it seems related to the very limited extent that I understand Windows linking) that programs linking to libkernel.a need different flags if Chez was built with ZLIB=-lz LZ4=-llz4 than if Chez was built with its own libraries.

My impression is that the idiomatic solution for Unix-like systems, at least, would be to generate and install a pkg-config file, which I believe would also be understood by CMake and other tools. I just haven't made time to try that, yet. Alternatively, I think there is some way of embedding these directives into libkernel.a using linker script, but that seems painful. A hypothetical libchezscheme.so.10.0.0 (see also https://github.com/cisco/ChezScheme/issues/811#issuecomment-2155689300 and https://github.com/cisco/ChezScheme/issues/836#issuecomment-2154264376) wouldn't have this problem, since it would record the dependencies if shared libraries were used, but we'd want to continue to support static linking anyway.

ValorZard commented 3 weeks ago

Personally, I would like if something like https://github.com/gwatt/chez-exe was included as part of Chez itself. It would make life a lot easily in my opinion.