lua-users home
lua-l archive

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


I am building a C++ app that calls scripts, which many times require C modules.
I've researched the list on this topic and I've also looked at the
wiki but I still have
a few questions (pls note I'm new to Lua). This is my first message, I'll post a
second one with more questions.

I built a simple module compiled in C++ which has the following
luaopen_ function.
I've tried two different approaches to run the script, the first one
works and the second
doesn't. Can somebody help me understand why the second approach fails?
I also don't understand why in the second script prints twice the package.cpath



extern "C"  {

int luaopen_testlib(lua_State *L)
	std::cout << "luaopen_testlib" << std::endl;
	luaL_register(L, "testlib", testlib);
	return 1;


Approach 1:  Works

local mylib = package.loadlib("./", "luaopen_testlib")
print "initializing"
print (package.cpath)

* Output:

Approach 2: Fails

print (package.cpath)
require 'testlib'

* Output:
lua: ./testlib.lua:14: loop or previous error loading module 'testlib'
stack traceback:
        [C]: in function 'require'
        ./testlib.lua:14: in main chunk
        [C]: in function 'require'
        testlib.lua:14: in main chunk
        [C]: ?