Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while building a static Linux binary (with musl-libc) that includes LuaJIT

I've cloned the LuaJIT git repo and built it with:

make STATIC_CC="musl-gcc" BUILDMODE="static"

Then, I compiled a simple Lua "hello world" script into a C header file:

luajit -b test.lua test.h

test.h:

#define luaJIT_BC_test_SIZE 52
static const unsigned char luaJIT_BC_test[] = {
27,76,74,2,10,45,2,0,3,0,2,0,4,54,0,0,0,39,2,1,0,66,0,2,1,75,0,1,0,20,72,101,
108,108,111,32,102,114,111,109,32,76,117,97,33,10,112,114,105,110,116,0
};

After that, I wrote a simple C wrapper by following the official example, test.c:

#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include "test.h"

int main(void) {
    int error;
    lua_State *L = lua_open();
    luaL_openlibs(L);
    
    error = luaL_loadbuffer(L, (const char *) luaJIT_BC_test, luaJIT_BC_test_SIZE, "test") || lua_pcall(L, 0, 0, 0);
    if (error) {
        fprintf(stderr, "%s", lua_tostring(L, -1));
        lua_pop(L, 1);
    }
    
    lua_close(L);
    return 0;
}

But when I try to build it, it crashes with an error:

$ musl-gcc -static -ILuaJIT/src -LLuaJIT/src -o test test.c -lluajit
/usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/12.1.0/libgcc_eh.a(unwind-dw2-fde-dip.o): in function `_Unwind_Find_FDE':
(.text+0x1953): undefined reference to `_dl_find_object'
collect2: error: ld returned 1 exit status

It's related to libgcc, so I tried building everything with musl-clang, but still got the same error. Can someone explain what I'm missing here?


1 Answers

Figured it out - I needed to build LuaJIT with TARGET_XCFLAGS=-DLUAJIT_NO_UNWIND like so:

make STATIC_CC="musl-gcc" BUILDMODE="static" TARGET_XCFLAGS=-DLUAJIT_NO_UNWIND

I guess this just disables C++ exceptions support, but I'm not sure what the real implications are. Seems to work fine, for now.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!