Just a simple example:

--The  c file:

#include <stdio.h>

#include "lua.h"

#include "luaconf.h"

#include "lualib.h"

#include "lauxlib.h"

#include "math.h"

static
int l_sin (lua_State *L) {

double d = lua_tonumber(L,
);  /* get argument */

lua_pushnumber(L, sin(d)); 
/* push result */

return

/* number of results */

}

int main()

{

float pi =
3.1415926;

float pidiv6 = pi /
;

float pidiv4 = pi /
;

float rt =
;

lua_State *L = luaL_newstate();

luaL_openlibs(L);

lua_pushcfunction(L, l_sin);
//Lua know the address of l_sin in c context

lua_setglobal(L,
"mysin");
//map l_sin to mysin will be called in lua context

if ( !luaL_dofile(L,
"./cal.lua") ) {

printf("load cal.lua successful\n");

}
else {

printf());

;

}

lua_getglobal(L,
"lsin");

lua_pushnumber(L, pidiv4);

if ( !lua_pcall(L,
, , ) ) {

rt = lua_tonumber(L, -);

}
else {

printf());

;

}

printf("rt = %f\n", rt);

return
0;

}

--cal.lua

print("start...")

function lsin ( angle )

return mysin(angle)

end

print("end...")

==output==

start...

end...

load cal.lua successful

rt = 0.707107

//Actually a more simpler way is to call mysin directly without cal.lua

lua_getglobal(L, "mysin");

lua_pushnumber(L, pidiv4);

, , )
) {

rt = lua_tonumber(L, -);

} else {

printf());

;

}

call lua function from c and called back to c的更多相关文章

  1. lua function

    This is the main reference for the World of Warcraft Lua Runtime. Note that these are mostly standar ...

  2. Lua function 函数

    Lua支持面向对象,操作符为冒号‘:’.o:foo(x) <==> o.foo(o, x). Lua程序可以调用C语言或者Lua实现的函数.Lua基础库中的所有函数都是用C实现的.但这些细 ...

  3. First Lua function running in C

    这是我在C里面跑出来的第一个Lua 文件, 纪念一下. 1.Set up envirnonment: Mac下面 Lua的src (即include) 和lib(binary)是分开的, 所以需要分别 ...

  4. lua执行字节码的过程介绍

    前面一篇文章中介绍了lua给下面代码生成最终的字节码的整个过程,这次我们来看看lua vm执行这些字节码的过程. foo = "bar" local a, b = "a& ...

  5. lua解释执行脚本流程

    #include "lua.hpp" #include <iostream> using namespace std; #pragma comment(lib, &qu ...

  6. Lua热更系统

    1.介绍 使用脚本开发游戏业务逻辑其中一个好处就是代码可线上热更,不停机修复bug.而热更代码的写法与需要被热更的文件的代码又有着密切的关系,本文介绍一种热更方法. 2.热更原理 Lua提供一个叫re ...

  7. 《The Evolution of Lua》读书笔记 1

    lua的优点: 可移植性 容易嵌入 体积小 高效率 这些优点都来自于lua的设计目标:简洁.从Scheme获得了很多灵感,包括匿名函数,合理的语义域概念   lua前身: 巴西被禁运,引入计算机软件和 ...

  8. 用VC编译lua源码,生成lua语言的解释器和编译器

    用VC编译lua源码,生成lua语言的解释器和编译器 1.去网址下载源码 http://www.lua.org/download.html 2.装一个VC++,我用的是VC6.0 3.接下来我们开始编 ...

  9. Lua与C的交互

    Lua 与 C 的交互 Lua是一个嵌入式的语言,它不仅可以是一个独立运行的程序,也可以是一个用来嵌入其它应用的程序库. C API是一个C代码与Lua进行交互的函数集,它由以下几部分构成: 1.  ...

随机推荐

  1. Shell脚本中循环语句for,while,until用法

    循环语句: Bash Shell中主要提供了三种循环方式:for.while和until. 一.for循环 for循环的运作方式,是讲串行的元素意义取出,依序放入指定的变量中,然后重复执行含括的命令区 ...

  2. The Three Day

    函数基础-练习 #.写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作 # def modify_file(filename,old,new): # import os # w ...

  3. RabbitMQ 初体验

    概述 RabbitMQ是一款消息队列中间件.他提供了几乎覆盖所有语言的SDK与文档,简直强大的不的了.要详细的去了解学习RabbitMQ,我建议还是看官方文档吧.http://www.rabbitmq ...

  4. VC++中char和TCHAR之间转换

    char:计算机编程语言(c.c++.java等)中可容纳单个字符的一种基本数据类型. TCHAR:为了满足Unicode编码,对char的扩展,即_T(“str”)表示TCHAR类型 C++支持两种 ...

  5. 编写个makefile文件测试patsubst 的作用

    1 SRCS := $(wildcard *.c) 2 OBJS := $(patsubst %.c,%.o,$(SRCS) ) //把$(SRCS)中的文件.c全部换成.o文件 3 all: 4 @ ...

  6. LeetCode(103) Binary Tree Zigzag Level Order Traversal

    题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...

  7. LeetCode(104) Maximum Depth of Binary Tree

    题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...

  8. android 之 View

    在进行游戏开发时,需要自定义各种控件和界面. 自定义View的使用: 绘制屏幕 刷新屏幕:后台数据发生了变化,需要开发人员自己刷新屏幕以显示最新数据 例子: MyView开发,绘制界面View内容: ...

  9. OSPF 一 基础

    本节介绍ospf路由选择协议    为链路状态  路由选择协议 一  分类 open shortest path first   开放最短路优先   公有协议 单区域的ospf实施  运行在一个自治系 ...

  10. ubuntu ssh连接服务器保持长时间不断

    方法: ssh -o serveraliveinterval=60 username@ip