From: Florent Michel <florent.m42@gmail.com>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: [NTG-context] Calling C code from ConTeXt LMTX
Date: Sat, 14 Sep 2024 21:46:59 +0100 [thread overview]
Message-ID: <CALOhoe+AT7_r6SvsfJB1VGFkFJzoRdEjyWrZ+iVGX_oqeFNhXw@mail.gmail.com> (raw)
[-- Attachment #1.1: Type: text/plain, Size: 2457 bytes --]
Hello,
TL;DR: Could building LuaMetaTeX with the `-fPIC` flag for the Lua library
have negative consequences? Is there a better way to be able to call
external C functions from ConTeXt?
I am working on a small project where it would be convenient to call
functions written in C (possibly pre-compiled in a shared library) from
ConTeXt. After looking at the Lua FFI, I found a way that seems to work
(with ConTeXt LMTX 2024-08-16) but requires a small tweak to the cmake file
used for building the Lua library. I would be grateful to have the opinion
of people more knowledgeable than I am about whether this change could have
negative consequences and whether there is a more straightforward way to
call external C code.
What I am trying to achieve is:
* Compile some C code in a shared library (for instance, it may contain a
numerical solver for some partial differential equation).
* Have this code run when compiling a document with ConTeXt. (For instance,
solving the differential equation with parameters defined in the .tex file
and retrieving the results to plot a figure with MetaPost.)
One easy way to do that is:
* Write wrappers for the functions that will need to be called from ConTeXt
using the Lua C API.
* Compile the code and wrappers in a shared library and linking to the
static lua library used by LuaMetaTeX (liblua.a on Linux).
* In the .tex file, import the new library using, e.g.,
```
\startluacode
mylib = require("mylib")
-- additional code using the C functions
\stopluacode
```
* Run ConTeXt with the `--permitloadlib` flag.
However, the second step requires (at least on Linux) liblua.a to be
compiled with the `-fPIC` flag. I could do that by adding
```
set_property(TARGET lua PROPERTY POSITION_INDEPENDENT_CODE ON)
```
at line 47 in lua.cmake. Is there any known negative effect of doing that
(apart from a possible small performance drop)?
(Just to be clear, this is not a feature request; I'm just wondering
whether doing it on my side is fine.)
On a related note, is there a standard way to call external C functions
(either in a shared library or in a .c file) from ConTeXt LMTX? The SwigLib
library described in
https://www.pragma-ade.nl/general/manuals/swiglib-mkiv.pdf looks very
promising (and maybe could be a way to achieve what I'm trying to do in a
much cleaner way), but I (maybe for lack of trying hard enough) have not
been able to make it work with ConTeXt LMTX so far.
Best regards,
Florent
[-- Attachment #1.2: Type: text/html, Size: 2708 bytes --]
[-- Attachment #2: Type: text/plain, Size: 511 bytes --]
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive : https://github.com/contextgarden/context
wiki : https://wiki.contextgarden.net
___________________________________________________________________________________
next reply other threads:[~2024-09-14 20:51 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-14 20:46 Florent Michel [this message]
2024-09-15 20:29 ` [NTG-context] " Bruce Horrocks
2024-09-15 21:11 ` Florent Michel
2024-09-16 10:59 ` Bruce Horrocks
2024-09-16 11:15 ` Henning Hraban Ramm
2024-09-16 12:06 ` Bruce Horrocks
2024-09-16 12:58 ` Florent Michel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CALOhoe+AT7_r6SvsfJB1VGFkFJzoRdEjyWrZ+iVGX_oqeFNhXw@mail.gmail.com \
--to=florent.m42@gmail.com \
--cc=ntg-context@ntg.nl \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).