# Generating C code from OCaml ppx

**URL:** <https://discuss.ocaml.org/t/generating-c-code-from-ocaml-ppx/14329>\
**Category:** Learning\
**Created:** [March 19, 2024, 1:36pm UTC](https://discuss.ocaml.org/t/generating-c-code-from-ocaml-ppx/14329 "2024-03-19T13:36:24Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jbeckford](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/jbeckford/32/3027_2.png) [@jbeckford](https://discuss.ocaml.org/u/jbeckford)\
**Post date:** [March 19, 2024, 3:46pm UTC](https://discuss.ocaml.org/t/generating-c-code-from-ocaml-ppx/14329/2 "2024-03-19T15:46:44Z")

</div>

My assumption is that you can’t use PPXes to write a separate C file. Well … as a side-effect of running the PPX you can write to a file but that sounds brittle.

Although I didn’t document my use cases in my recent DkCoder scripting article ([[ANN] DkCoder 0.1.0](https://discuss.ocaml.org/t/ann-dkcoder-0-1-0/14327)), I have one use case where I need to compile OCaml into larger C executables while simultaneously not requiring a C compiler. Eventually I’ll be adding a feature to do linking (irrelevant to you) and do some light symbol modifications (perhaps relevant) inside DkCoder.

I have been looking into LIEF to perform those symbol modifications on existing ELF/Mach-O/PE libraries: [04 - ELF Hooking — LIEF Documentation](https://lief.re/doc/stable/tutorials/04_elf_hooking.html). Kinda like patchelf but cross-platform and not as buggy. (The linked example is Python but it has a C++ API and a limited C API). So …

1. Perhaps a small `make_nbkit_ocaml_plugin` executable can take your users’ OCaml-compiled libraries and add/rename/duplicate the symbols you need to make it a full-fledged nbdkit plugin. LIEF can help do that.
2. If your plugin needs are light enough that bytecode compiled plugins are sufficient, ping me offline.

---

_[View the full topic](https://discuss.ocaml.org/t/generating-c-code-from-ocaml-ppx/14329)._
