• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

EmbeddingLuainC:UsingLuafrominsideC.

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Requirments: 

-- 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. 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua和C++交互详细总结发布时间:2022-07-22
下一篇:
Lua学习教程发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap