参考了很多网上的资料,但大部分都是直接调用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,