2

I thought of compiling Lua from source code and then create a C module. I compiled Lua with success but I can't build my C module.

So, I compiled Lua like this:

gcc -o Lua *.c -Os -std=c99

Compiled my module like this:

gcc -Wall -shared -fPIC -o module.so -I. module.c

But there are a few errors here:

Undefined symbols for architecture x86_64:
  "_lua_pushcclosure", referenced from:
      _luaopen_module in module-fb0b1f.o
  "_lua_pushnumber", referenced from:
      _super in module-fb0b1f.o
  "_lua_setglobal", referenced from:
      _luaopen_module in module-fb0b1f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see  invocation)

The C module itself:

#include "lua.h"


static int super(lua_State* L) {
    lua_pushnumber(L, 5);
    return 1;
}

int luaopen_module(lua_State* L) {
    lua_register(L, "super", super);
    return 0;
}

My Lua script:

require("module")
print(super())

I'm on Unix based system (Mac), but I want it to work on Linux as well.

Edit:

Problem to compile C module was fixed by entering -bundle -undefined dynamic_lookup instead of -shared (Thanks lhf). But I can't import the module in Lua.

> require("module")
error loading module 'module' from file './module.so':
    dynamic libraries not enabled; check your Lua installation

Another thing: This seems to be a quick fix only; -bundle -undefined dynamic_lookup. This does not work on Linux. How can I do this on linux? I wanted a solution for Unix based systems.

2
  • Try -bundle -undefined dynamic_lookup instead of -shared. Commented Nov 28, 2016 at 11:15
  • Awesome, I can now build the C module. But I can't import it in Lua. Commented Nov 28, 2016 at 11:16

1 Answer 1

2
  • Download Lua from lua.org and build Lua with make macosx. See Getting started.

  • Use -bundle -undefined dynamic_lookup instead of -shared to build module.so.

  • Use require"module" to load it into Lua.

  • Call super.

Make sure you're running the lua program that you have built above, not some other version that is installed.

Sign up to request clarification or add additional context in comments.

4 Comments

So I did. And I could build C module, but not include it in the Lua script.
I want to build it myself, without MakeFile.
@Azinum use -DLUA_USE_MACOSX when building Lua.
Without the MakeFile?

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.