lua-users home
lua-l archive

Re: How to do a Lua<->C hello world in Lua 5.1?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Steve Litt once stated:
> Hi all, 
> 
> I've been RTFW all night but no joy when it comes to either running C from Lua 
> or running Lua from C. I tried this:
> 
> http://heavycoder.com/tutorials/lua_embed.php
> 
> but kept getting "embed.c:23: error: invalid storage class for function 
> openlualibs"
> 
> It seems like my computer has /usr/lib/liblualib50.so.5 but no corresponding 
> liblualib51.*, and I'm using Lua version 5.1.4. My Ubuntu is 9.10. If I knew 
> the old version was the problem I'd upgrade, but I'd hate to go through all 
> that mess on anything but a very educated guess.
> 
> Is there anywhere on the web that has an up todate, for Lua5.1, bare bones 
> absolutely minimal example of Lua interfacing with C?
 In the Lua distribution for 5.1 in the etc/ directory is a bare bones,
absolutely minimal example of Lua interfacing with C---min.c. 
 Now, if you want to see how a project will embed Lua and don't mind a
rather sizable example (but it's still single C file), you can check out:
	http://www.conman.org/software/syslogintr/
 It's a non-trivial example of embedding Lua in a C application. If you do
want to look at it, most of the Lua stuff is in the functions main(),
process_msg(), load_script(), syslogintr_alarm(), syslogintr_ud__toprint(),
syslogintr_host(), syslogintr_lsock(), syslogintr_relay(),
call_optional_luaf() and add_lua_path(). Think of this as a demo of
embedding Lua, and extending Lua, in a Unix daemon (in this case, syslog).
 It is extensively commented and it may be a bit more than you want to look
through for a simple example, but it is self-contained (and a decent example
of a Unix daemon as well). 
 To compile it (given that you have Lua 5.0 installed, but not necessarily
5.1), you'll need to download Lua 5.1 (which you may have already); you can
then compile at least my example with:
	gcc -std=c99 -rdynamic -g -o syslogintr -I path/to/lua5.1/src \
		syslogintr.c -L path/to/lua5.1/src -llua -lm -ldl
 The options, in order:
	-std=c99	Use C99 features (my program uses them)
	-rdynamic	Allow shared libraries to use code from program
	-g		debugging information
	-o syslogintr	name of executable program
	-I ...		The path to the Lua source code for the includes
	-L ...		The path to the Lua liblua.a
	-llua		link to liblua.a
	-lm		link in the math libraries (Lua requires this)
	-ldl		link in the dynamic loader (so you can use require
			"blah.so" in your scripts)
 I hope this points you in the right direction.
 -spc

AltStyle によって変換されたページ (->オリジナル) /