lua调用c 模块linux,nginx lua 调用c/c++详细流程

一、nginx相关

1、nginx 动态添加lua模块

可能需要的依赖 yum install -y pcre-devel openssl-devel zlib-devel

2、安装必须模块,版本问题可能保持nginx编译时就会报错

wget https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz

wget https://nginx.org/download/nginx-1.14.0.tar.gz

wget https://github.com/vision5/ngx_devel_kit/archive/v0.3.1.tar.gz

wget https://luajit.org/download/LuaJIT-2.0.5.tar.gz

3、LuaJIT解压后直接make & make install即可

4、nginx编译参考命令,需根据实际情况调整,这里为动态添加模块,所以保留参数较长

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock \

--http-client-body-temp-path=/var/cache/nginx/client_temp \

--http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \

--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio \

--with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module \

--with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module \

--with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail \

--with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module \

--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \

--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/fastdfs-nginx-module/src \

--add-module=/root/luatest/lua-nginx-module-0.10.15 --add-module=/root/luatest/ngx_devel_kit-0.3.1

5、make && make install(动态安装不必install)

6、需要操作的内容,下面两行避免share object错误

ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/liblua-5.1.so.2

echo "/usr/local/lib" >> /etc/ld.so.conf

7、nginx安装完成后调用lua可能报错

unknown directive "access_by_lua_file",这种情况必须重启nginx,reload无效 systemctl nginx restart

8、还需要配置初始化lua模块才能不报错

http {

...

#lua模块路径,其中”;;”表示默认搜索路径,默认到/usr/servers/nginx下找

#lua_package_path "/usr/example/lualib/?.lua;;"; #lua 模块

lua_package_cpath "/usr/example/lualib/?.so;;"; #c模块

...

#不初始化的话只有第一次调用才能成功,后面调用都会报错,power模块须在lua_package_cpath中指定

init_by_lua_block {

require "power";

}

}

二、c/c++c相关

为了简单,这里采用的是c,若c++也可以用gcc编译成.so模块,需要注意的是引入和方法名等需要加上 extern "C" 比如

extern "C"

{

#include "lua.h"

#include "lauxlib.h"

#include "lualib.h"

}

extern "C" LUA_API (static会报错) int luaopen_mLualib(lua_State *L);//定义导出函数 示例代码:

#include

#include

#include

static int isquare(lua_State *L){

float rtrn = lua_tonumber(L, -1);

printf("Top of square(), nbr=%f\n",rtrn);

lua_pushnumber(L,rtrn*rtrn);

return 1;

}

int luaopen_power(lua_State *L){

lua_register(L,"square",isquare);

return 0;

}

编译命令

gcc -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1 hellofunc.c -I 为指定模块路径,若找不到路径可自行下载并指定,注意要同版本 wget https://www.lua.org/ftp/lua-5.1.4.tar.gz

三,lua相关

lua代码

#!/usr/bin/lua

require("power")

ngx.say(square(1.414213598))

四、nginx调用

server {

listen 8080;

location /lua {

default_type 'text/html';

content_by_lua_file /home/admin/luatest/lua-5.1.4/src/test.lua;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值