liunx 下 C/C++调用luajit

本文介绍如何在Linux环境下使用C/C++调用LuaJIT,结合Torch7处理图像并计算欧式距离。程序通过GCC编译,需包含luajit库及相应路径,同时注意数学库的链接。
摘要由CSDN通过智能技术生成

参考了很多网上的资料,但大部分都是直接调用lua的 ,因为lua和luajit 的API相互兼容,所以很多程序都可以直接调用。
参考网址:http://www.apihome.cn/api/lua/lua_newthread.html
http://www.jb51.net/article/55843.htm
http://blog.sina.com.cn/s/blog_932db1950101l5wy.html

先上代码:

这是test.c 的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "luajit.h"
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
/*
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luajit.h"
};*/
//如果使用C++编译的。不屏蔽这几条语句,但是要把上面几个相同的头文件屏蔽
//并把.C修改为.cpp
const char str1[]= "/home/lyz/opencv测试/firstopencv/1.jpg";
const char str2[]= "/home/lyz/opencv测试/firstopencv/2.jpg";
static lua_State *globalL = NULL;
lua_State* L;
int luaadd(const char str1[],const char str2[])
{

        luaL_openlibs(L);
    float sum;                   
        lua_getglobal(L,"test");         //the function name           
        lua_pushstring(L,str1);    
    lua_pushstring(L,str2);          
    lua_call(L, 2, 1);          //call 2 arguments, return 1 result.         
    sum = (float)lua_tonumber(L, -1);//get the result.        
        lua_pop(L,-1);                   //cleanup the return
        return sum;
}

int main(int argc, 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值