Requirments:

    1: The Lua Sources.
    2: A C compiler - cc/gcc/g++ for Unix, and Visual C++ for Windows.

Other compilers should under Windows, basically any C compiler - I will cover compiling under Windows at the end of the toturial.
This tutorial will work under any OS which has a C compiler, and which Lua has compiled succesfully on. Though the process for compiling with different compilers on different platforms may be different - In this tutorial I will cover compiling on Unix systems and Windows systems.

For those who don't know, the Lua programming language was made to be a smimple and small scripting language which people could embed into larger applications, especially applications written in C. The advantage of embedding any scripting language (ie. Perl, Python, Ruby, etc) into a larger application was to give the user using your program a 'macro' language in which they could customize the program to suit them. Many developers even create their own 'macro' languages specifically for their applications. For a real world example of this in action - Take a look at the Microsoft Office software on the Windows platform. Microsoft Office applications like Excel and Word use a macro language called 'VBA' which enables the user to create... You guessed it, macros. Of course you could do what you wanted with it, like any other language. VBA ('Visual Basic for Applications') is basically a cut down version of Visual Basic.

Another real world example would be the famous text editor (On Unix platforms) Emacs, which uses Lisp as it's macro language.

So in this tutorial I will show you how to embed the Lua scripting language into your C applications. 
To start with lets create a very basic Lua script and save it as "script.lua":

-- Start
-- Script: script.lua
print("I am using Lua from within C")
-- End

There, told you it was a very basic script!
When we embed Lua into C, we will ask Lua to open and run that file. For more examples, see the end of the tutorial. Now for the C code.
Create a new text file called and save it as "embed.c":

#include <stdlib.h>
#include <stdio.h> /* Include the Lua API header files. */
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h> int main(void)
{
/* Declare the Lua libraries we wish to use. */
/* Note: If you are opening and running a file containing Lua code */
/* using 'lua_dofile(l, "myfile.lua") - you must delcare all the libraries */
/* used in that file here also. */
static const luaL_reg lualibs[] =
{
{ "base", luaopen_base },
{ NULL, NULL }
}; /* A function to open up all the Lua libraries you declared above. */
static void openlualibs(lua_State *l)
{
const luaL_reg *lib; for (lib = lualibs; lib->func != NULL; lib++)
{
lib->func(l);
lua_settop(l, 0);
}
} /* Declare a Lua State, open the Lua State and load the libraries (see above). */
lua_State *l;
l = lua_open();
openlualibs(l); /* You can do what you want here. Note: Remember to update the libraries used (see above) */
/* if you add to your program and use new Lua libraries. */
/* In the lines below, I load and run the Lua code contained in the file */
/* "script.lua". */
/* Plus print some text directly from C. */
printf("This line in directly from C\n\n");
lua_dofile(l, "script.lua");
printf("\nBack to C again\n\n"); /* Remember to destroy the Lua State */
lua_close(l); return 0;
}

To compile this, do the following (Unix):

cc -o embed embed.c \
-I/usr/local/include \
-L/usr/local/lib \
-llua -llualib

To compile under Windows (Visual C++ GUI) do the following:

1: Create a new project.
2: Add the embed.c file.
3: Add the 2 Lua libraries (*.lib) - Standard library and the Core library.
4: Add the locations of the Lua include files to the project options ("Directories tab").
5: You may also have to add the locations of the library files - the same way as above.
6: Compile and Build - that's it.

Now run the compiled file ("./embed" on Unix, "embed.exe" on Windows) and you should see the following output:

This line in directly from C

I am using Lua from within C

Back to C again

And there it is. Of course this is only an extremely basic example. One thing I actually do myself is embed a scripting language which is good at text processing (Python or Perl) and when I need to do text processing, use the embedded scripting language, instead of C - as it can take a lot more work in C.
You can also send values to and from between C and Lua when embedded, which makes the possibilities endless. 
I have seen a good tutorial on this, and will link to it. 
#NOTE_TO_MYSELF: Add link.

I might also write my own tutorial on this later on, as well as how to embed other scripting languages into C. 
Remember to read the Official Lua API manual for loads more informtaion. Or even read through the Lua source code to see what happens under the hood and how the language is actually made, this will give you an even better understanding.

You don't even have to run the Lua code from a file. You can use the function "lua_dostring(l, 'code') instead of the "lua_dofile" function.

Note on distrobuting programs:

If you embed Lua (Or any other scripting language) into a larger application, you still need to ship with it the scripting languages library files (standard library and core library in Lua's case).
In Lua's case, it is not really a downside if your application is big in the first place, because Lua is so small - the library files should not take up anymore than 1mb. This is why I recommend using Lua, I think it is a great embeddable, extensible, stand-alone scripting language.

I hope you have enjoyed reading this tutorial, and have got lots of exciting ideas!
Remember to thank the team behind Lua for creating a wonderful language.

Embedding Lua in C: Using Lua from inside C.的更多相关文章

  1. (转) Lua使用心得一 LUA和VC整合

    这几天研究了一下lua,主要关注的是lua和vc之间的整合,把代码都写好放在VC宿主程序里,然后在lua里调用宿主程序的这些代码(或者叫接口.组件,随便你怎么叫),希望能用脚本来控制主程序的行为.这实 ...

  2. cocos2dx lua binding ,cocos2dx 绑定lua测试

    前面2篇分别简单介绍 手动bind C++ 类和lua:http://blog.csdn.net/chenee543216/article/details/12074771 使用tolua++简化工作 ...

  3. 整理Lua和Unity和Lua交互文章链接

    重点文章: 1.[Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(上) 2.[Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(中) 3.Lua和C++交互详细总结 4. ...

  4. lua脚本之lua语言简介以及lua的安装

    本博主不擅于进行文字创作,所以,相当一部分文字皆复制于其他博文.还希望能够得到谅解.  一.Lua语言简介 Lua是一个免费的开源软件,可以免费用于科研及商业.Lua具有一个专家团队在维护和升级,其设 ...

  5. Android错误:can not get file data of lua/start_v2.op [LUA ERROR] [string "require "lua/start_v2””] 已解决

    错误: can not get file data of lua/start_v2.op [LUA ERROR] [string "require "lua/start_v2””] ...

  6. 【Lua】linux下lua+mod_lwt环境搭建

    Lua 是一个小巧的脚本语言.它具有轻量级.可扩展等优势.它可以作为一个强大.轻量的脚本语言,供任何需要的程序使用. LWT (Lua Web Tools) 可让你使用 Lua 开发 Web 应用,并 ...

  7. [Lua快速了解一下]Lua运行

    -Lua的Hello World print("Hello World") 分号可选 -类似python,进入Lua后再shell中打命令执行语句也可 > print(&qu ...

  8. lua学习:使用Lua处理游戏数据

    在之前lua学习:lua作配置文件里,我们学会了用lua作配置文件. 其实lua在游戏开发中可以作为一个强大的保存.载入游戏数据的工具. 1.载入游戏数据 比如说,现在我有一份表单: data.xls ...

  9. lua三底漆:lua转让c/c++库(动态链接模式)

    dll按功能luaL_openlib出口,然后lua使用package.loadlib导入库函数,基本就是这么个过程,以下上代码来说明一切. #include "stdafx.h" ...

随机推荐

  1. 超炫酷web前端的jQuery/HTML5应用搜罗

    作为前端开发者,我们肯定都使用过非常多的jQuery插件,毋庸置疑,jQuery非常流行,尤其是结合HTML5和CSS3以后,让这些jQuery插件有了更多地动画效果,更为绚丽多彩. 1.HTML5/ ...

  2. IPv6协议介绍

    IPv6是为了解决基于IPv4的TCP/IP协议簇遇到的问题而推出的下一代IP协议.由于IPv4中采用的编制方式使得可用的网络地址和主机地址的数目远低于理论数目,随着全球互联网的快速发展,现有的IPv ...

  3. 【风马一族_Java】9*9口诀

    public class arithmetic { public static void main(String[] args){ sows(9,9); } private static void s ...

  4. 《LDAP服务器和客户端的加密认证》RHEL6——第二篇 运维工程师必考

    服务端的配置: (基于原先配好的ldap服务器)打开加密认证: Iptables –F  setenforce 0 1.编辑ldap的配置文件:slapd.conf 2.启动ldap服务器: 3.切换 ...

  5. 【原】WinForm中的DataGridView加入Combbox或者DropDownButton后,操作变慢

    DataGridView里加入了DropDownButto列,加载数据后点击这一列,反应很慢;要点击三到四次才会展示下拉列表; 原因是DataGridView的EditMode设置问题; 将DataG ...

  6. windows程序消息机制(Winform界面更新有关)

    windows程序消息机制(Winform界面更新有关) 转自:http://www.cnblogs.com/blosaa/archive/2013/05/31/3109586.html 1. Win ...

  7. [大牛翻译系列]Hadoop(7)MapReduce:抽样(Sampling)

    4.3 抽样(Sampling) 用基于MapReduce的程序来处理TB级的数据集,要花费的时间可能是数以小时计.仅仅是优化代码是很难达到良好的效果. 在开发和调试代码的时候,没有必要处理整个数据集 ...

  8. C程序调用shell脚本共有三种方法

    C程序调用shell脚本共有三种法子 :system().popen().exec系列函数call_exec1.c ,内容为:system() 不用你自己去产生进程,它已经封装了,直接加入自己的命令e ...

  9. Hadoop2安装

    http://wenku.baidu.com/view/fe1b2f22de80d4d8d15a4f6e.html http://wenku.baidu.com/view/e4607031581b6b ...

  10. CentOS 6.0 设置IP地址、网关、DNS

    切忌:    在做任何操作之前先备份原文件,我们约定备份文件的名称为:源文件名称+bak,例如原文件名称为:centos.txt    那么备份文件名称为:centos.txtbak 引言:linux ...