lua-users home
lua-l archive

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

Today, was my first stab at trying Lua and I had trouble
getting an embedded Lua hello world C program working.
All of the examples I found on the internet didn't work for me.

I'd also suggest including the command line to compile,
as that was also not obvious. Below is what worked for
me on my Linux box.

Make the linux version:
  $ make linux

Add the hello_world.c program below to the root of lua source tree
and compile:
  $ gcc hello_world.c -o hello_world -I src/ -L src -llua -ldl -lm

  $ ./hello_world
  Yo dude
  I'm here too
  Hello world, from Lua 5.1!

// The hello program
#include <stdio.h>
#include "src/lua.h"
#include "src/lualib.h"
#include "src/lauxlib.h"

int main(int argc, char *argv[]) {
  // Open lua
  lua_State *L = lua_open();

  // Load the libraries

  // Execution of a lua string
  luaL_dostring(L, "print \"Yo dude\"");

  // Load a string and then execute it.
  luaL_loadstring(L, "io.write(\"I'm here too\\n\")");
  lua_pcall(L, 0, LUA_MULTRET, 0);

  // Load from a file and then execute
  if (luaL_loadfile(L, "test/hello.lua") == 0) {
    // File loaded call it
    lua_pcall(L, 0, LUA_MULTRET, 0);

  // Close lua
  lua_close (L);

  return 0;