lua ffi

testlib.c
#include <stdio.h>
#include <string.h>

char* fun_strcat(char* str1,char* str2){
    return strcat(str1,str2);
}


生成动态库:
gcc -g -o testLib.so -fpic -shared testlib.c


local ffi = require("ffi")

ffi.cdef[[
typedef struct { double x, y; } A;
char* fun_strcat(char* str1,char* str2);
enum enum_type
{
  type1 = 1,
  type2,
};;
]]

local A = ffi.metatype("A",{})

local a = A(3, 4)
ngx.say(a.x, a.y)

local clib = ffi.load("/usr/local/code/testLib/testLib.so")
local str1 = ffi.new("char[?]", 10)
local str2 = ffi.new("char[?]", 10)
local s1 = "abc"
local s2 = "def"
ffi.copy(str1, s1, string.len(s1))
ffi.copy(str2, s2, string.len(s2))

local str = ffi.string(clib.fun_strcat(str1,str2))
ngx.say(str)

ngx.say(ffi.C.type1)



[root@localhost bin]# curl 127.0.0.1:6699/test
34
abcdef
1

猜你喜欢

转载自xiangjie88.iteye.com/blog/2399911
LUA