lua-vs编译lua-cjson的正确姿势

编译lua-cjson库,用的是云风fork后修改的支持lua53 integer64的库,传送门:lua-cjson库


在vs中新建一个工程,导入lua-json的源码

(只需要几个源码文件)
这里写图片描述


修改部分源码

  1. 所有 static inline 的函数修改为 static,或者直接 #define inline
  2. 把一些不存在的api添加为windows下带下划线的版本,和strncasecmp 替换为 strncmp
  3. fpconv.h 修改成下面的样子
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    #define FPCONV_G_FMT_BUFSIZE 32
    #define USE_INTERNAL_FPCONV
    #define inline __inline
    #ifdef USE_INTERNAL_FPCONV
    static inline void fpconv_init()
    {
    /* Do nothing - not required */
    }
    #else
    extern inline void fpconv_init();
    #endif

    extern int fpconv_g_fmt(char*, double, int);
    extern double fpconv_strtod(const char*, char**);
  4. 导出 luaopen_cjson 函数
  5. 主工程中使用这个库

导出的库名必须和导出函数的后缀名一致,也就是 cjson.lib
这样才能在lua中 require

1
2
3
4
5
6
local cjson = require "cjson"
local tab = {
a = "hello",
b = 123456,
}
local json = cjson.encode(tab)