lua-users home
lua-l archive

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


With all your help, I got it working.

It is really a pleasure to work with C/Lua combination as frequent C code modification can be avoided.

Austin


On Sat, Jun 28, 2014 at 10:31 AM, Sean Conner <sean@conman.org> wrote:
It was thus said that the Great Paige DePol once stated:
>
> Why are you hardcoding a Lua function in C instead of loading it from a
> file? If you are hardcoding a script then why not just use C itself to
> perform this action?

  There can be valid reasons.  At work, I preload a bunch of Lua code into
an executable simply to minimize the number of files required to install and
run the application.  Another reason might be to prevent modification of the
code.

  Now, how I handle such things is to use a simple program that converts the
Lua script into a C character array like:

const char my_lua_program[] =
{
  0x0A, 0x7A, 0x6C, 0x69, 0x62, 0x20, 0x3D, 0x20,
  0x72, 0x65, 0x71, 0x75, 0x69, 0x72, 0x65, 0x20,
  /* ... */
};

in its own C file, to make it easier to deal with in a Makefile:

%.lo : %.lua
        $(BIN2C) -o $(*F).c -t $(*F) $<
        $(CC) $(CFLAGS) -c -o $@ $(*F).c
        $(RM) $(*F).c

foobar : foobar.o blah.lo
        $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS)

In my case, I'm using GNU Make, so the $(*F) is just the base filename of
the source in the rules (in this example, $(*F) of "blah.lua" is "blah").

  -spc (It also doesn't hurt that Unix doesn't really care what the object
        file extentions are ... )