1

I'm learning to use Lua to embed in my C programs. I was wondering what they best way make sure that my C program can find the Lua files after it's built. I'm trying to make sure this works with an out of source CMake build.

Here is what I have so far.

In my c file, I have...

int runLuaHelloWorld() {

    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaL_dofile(L, "helloWorld.lua");
    lua_close(L);

    return 0;
}

In my CMakeLists.txt I have.

set(EMBEDDED_LUA_SRC embeddedLua.c embeddedLua.h)
add_library(embeddedLua ${EMBEDDED_LUA_SRC})
if(LUA_FOUND)
    target_include_directories(embeddedLua PRIVATE ${LUA_INCLUDE_DIR})
    target_link_libraries(embeddedLua ${LUA_LIBRARIES})
    add_custom_command(TARGET embeddedLua POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:embeddedLua> helloWorld.lua)
endif()

This successfully copies it to my build directory. But it seems to load my lua script file relative to my current working directory. In other words, I can only get the script to run if I'm running it from my build folder.

What's the best practice for including and running external lua files post build?

Perhaps I'm going about this completely the wrong way. Maybe this isn't a build issue at all. The lua script needs to run relative to the executable (or better yet, relative to a library) rather than to my working directory.

Any idea?

loneraver
  • 1,282
  • 2
  • 15
  • 22
  • Probably [this is](https://stackoverflow.com/questions/16865040/luajit-compiling-bytecode-into-object-format) the best practice ever – Egor Skriptunoff Dec 29 '16 at 22:40
  • See also http://stackoverflow.com/questions/194520/creating-standalone-lua-executables. – lhf Dec 30 '16 at 01:12
  • Not looking to create standalone Lua executables. I'm writing a library in C and want to include an application that use Lua scripts to interact with this library. I want to make it easy to edit the lua file so that I don't have to recompile the entire library every time I make a change to the script. – loneraver Dec 30 '16 at 05:51

1 Answers1

2

In our software we define a path to a directory where we store extensions written in Lua. Then we use the opendir(), readir() etc. to find files in that directory, and when they end in '.lua' we load and execute them. So use just need to copy their scripts to that location.

In some applications we even store the Lua scripts in a text column of a (PostgreSQL) database table.

Marc Balmer
  • 1,780
  • 1
  • 11
  • 18
  • Great idea! This works wonderfully. The only problem I have is that my Lua scripts doesn't find any other Lua modules in the same folder. I write mostly Python at work and I'm new to Lua, so please bare with me. I think I read that I have to set a path variable to get it to know where to look for other supporting Lua files. Is this correct or is there a better way? – loneraver Jan 07 '17 at 20:59
  • 1
    Yes, this is correct. You can very flexibly set the path where Lua will look for modules. Actually, if you dig a bit deeper, you have even more options up to writing you own module loaders. Check the docs around lua_load for details. – Marc Balmer Jan 08 '17 at 09:14
  • That is awesome! After posting my reply, I figured out how to do just this. Thank you for confirming that I was on the right track. – loneraver Jan 09 '17 at 16:42