0

I'm trying to get lua 5.1 to execute a line which is just a call to one of my c++ functions "Assail" I don't understand why it does not work, can anybody point out the mistakes?

this is the Assail Function:

static int Assail(lua_State *L)
{
    cout << "test" << endl;
    return 1;
}

I'm trying to call do_string like this:

L = lua_open();
luaL_openlibs(L);
lua_register(L, "Assail", Assail);
luaL_dostring(L, "s = Assail()");   
lua_close(L);

any help is appreciated.

1
  • What went wrong? Anyway, check the return code of luaL_dostring. Finally, note that Assail should probably return 0, since it does not push anything onto the stack. For a surprise, try s = Assail(10,20); print(s). Commented Jan 19, 2013 at 20:04

1 Answer 1

3

Since you're writing this in C++, name mangling is applied to the function, so it will have a special name in the final executable/library that is different from that Lua would expect. You can resolve this by changing its linkage to C using the extern keyword:

extern "C" int Assail(lua_State *L);

extern "C" int Assail(lua_State *L)
{
    // code here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.