<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>vacmf &#187; Lua</title>
	<atom:link href="http://vacmf.org/category/coding/lua/feed/" rel="self" type="application/rss+xml" />
	<link>http://vacmf.org</link>
	<description>there&#039;s more than one way to do it</description>
	<lastBuildDate>Mon, 07 May 2012 21:09:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Embedding Lua script into a C program</title>
		<link>http://vacmf.org/2010/12/05/embedding-lua-script-into-a-c-program/</link>
		<comments>http://vacmf.org/2010/12/05/embedding-lua-script-into-a-c-program/#comments</comments>
		<pubDate>Sun, 05 Dec 2010 10:37:22 +0000</pubDate>
		<dc:creator>shima</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Logbook]]></category>
		<category><![CDATA[Lua]]></category>
		<category><![CDATA[OS X]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[embedded language]]></category>
		<category><![CDATA[os x]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://vacmf.org/?p=132</guid>
		<description><![CDATA[Just for curiosity I wanted to try embedding a Lua script into a little C program and surprisingly it resulted in a pretty quick and easy task.
Lua provides C API and everything is done within a few lines of code.
Let&#8217;s assume that our Lua script just prints &#8220;hello world&#8221; once it is executed and  [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://vacmf.org/wp-content/uploads/2010/12/lua.gif"><img class="alignleft size-thumbnail wp-image-149" title="lua" src="http://vacmf.org/wp-content/uploads/2010/12/lua-150x150.gif" alt="" width="150" height="150" /></a>Just for curiosity I wanted to try embedding a Lua script into a little C program and surprisingly it resulted in a pretty quick and easy task.</p>
<p>Lua provides C API and everything is done within a few lines of code.</p>
<p>Let&#8217;s assume that our Lua script just prints &#8220;hello world&#8221; once it is executed and contains also a function to be called later.</p>
<p><em>Save a file as &#8220;hello.lua&#8221; and fill in with:</em></p>
<pre>print("Hello world!");

function foo()
 print("Hi I am the foo function!");
end</pre>
<p><em>Then create the C program that will run Lua ans save it as &#8220;embed_hello.c&#8221; with the following content:</em></p>
<pre>#include &lt;stdio.h&gt;
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

/* lua interpreter */
lua_State* l;

int main () {
  int dofile;

  /* initialize lua */
  l = lua_open();

  /* load lua libraries */
  luaL_openlibs(l);

  /* run the hello.lua script */
  dofile = luaL_dofile(l, "hello.lua");

  if (dofile == 0) {
    /* call foo */
    lua_getglobal(l,"foo");
    lua_call(l,0,0);
  }
  else {
    printf("Error, unable to run hello.lua\n");
  }

  /* cleanup Lua */
  lua_close(l);

  return 0;
}</pre>
<p><em>Compile:</em></p>
<pre>$ gcc -o embed_hello -Wall -L/opt/local/lib -I/opt/local/include \
-llua embed_hello.c</pre>
<p>That&#8217;s all. This example works for OS X and Lua installed from macports. For other OSs and configurations just adapt the gcc command.</p>
<p><em>Execute it</em></p>
<pre>$ ./embed_hello
Hello world!
Hi I am the foo function!</pre>
<p><em><br />
</em></p>
<h3>References<strong>:</strong></h3>
<ul>
<li><a href="http://www.lua.org/manual/5.1/manual.html#3" target="_blank"><em>Lua 5.1 Reference Manual: The Application Program Interface</em></a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://vacmf.org/2010/12/05/embedding-lua-script-into-a-c-program/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

