You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OCaml项目dune构建报错:Unbound value "string_of_sexp"

问题:配置ppx_jane后dune编译报错“Unbound value 'string_of_sexp'”

在utop中执行#require "ppx_jane"后代码可正常运行,但在dune文件里配置(preprocess (pps ppx_jane))后,执行dune build时出现错误:Unbound value "string_of_sexp"。

dune文件配置

(library
 (preprocess (pps ppx_jane))
 (name raftml)
 (modules raft rpc types)
 (libraries 
   core 
   core_unix 
   proto 
   grpc 
   grpc-lwt 
   ocaml-protoc 
   lwt 
   lwt.unix 
   h2 
   h2-lwt-unix 
   domainslib
   yojson
   ppx_jane
   ppx_sexp_conv
   ppx_deriving_yojson
   ppx_deriving
   ppx_deriving_yojson.runtime))

类型定义

type log = {
    mutable command: string;
    mutable term: int32;
    mutable index: int32
} [@@deriving sexp]

代码调用

let persist () = Out_channel.write_all "file_name" ~data:(Sexp.to_string (sexp_of_log { command = "hello"; term = (10l); index = (24l); }))

解决办法

问题根源是:utop中#require "ppx_jane"会自动打开Core库的相关模块,所以Sexp可以直接使用;但dune编译时不会自动打开这些模块,必须显式指定Core.Sexp。

  1. 修改代码调用中的Sexp.to_string为Core.Sexp.to_string:
let persist () = Out_channel.write_all "file_name" ~data:(Core.Sexp.to_string (sexp_of_log { command = "hello"; term = 10l; index = 24l }))
  1. 精简dune配置:ppx_jane已经包含了ppx_sexp_conv、ppx_deriving等常用ppx扩展,无需单独列出,优化后的libraries部分如下:
(libraries 
   core 
   core_unix 
   proto 
   grpc 
   grpc-lwt 
   ocaml-protoc 
   lwt 
   lwt.unix 
   h2 
   h2-lwt-unix 
   domainslib
   yojson
   ppx_jane
   ppx_deriving_yojson.runtime)

内容的提问来源于stack exchange,提问作者Murphy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 06:30:48