How to properly compile and link Lua C API with MinGW?

348 Views Asked by At

When compiling my code:

#include <stdio.h>
#include ".\luac_headers\lua.h"
#include ".\luac_headers\lauxlib.h"
#include ".\luac_headers\lualib.h"
int main() {
    const char* lua_code_buff = "print(\"hello from C.\")";
    lua_State* l_state = lua_open();
    luaL_openlibs(l_state);
    int l_error = luaL_loadbuffer(l_state, lua_code_buff, sizeof(lua_code_buff), "@code_buff") || lua_pcall(l_state, 0, 0, 0);
    if(l_error) {
        printf("Error from Lua: %s", lua_tostring(l_state, -1));
    }
    printf("yo");
    return 0;
}

using MinGW:

.\g++ -o .\luaC.exe .\luaC_test.c C:\Users\lux\lua_src\luac_libs\lua51.dll

In visual studio code, I get the following errors:

undefined reference to `luaL_newstate'
undefined reference to `luaL_openlibs'
undefined reference to `luaL_loadbuffer'
undefined reference to `lua_pcall'
undefined reference to `lua_tolstring'

lua51.dll is the compiled lua library dll which I am linking with(or is supposed to link with) my source file.

My source file is inside of MinGW's bin folder, and so is the luac_headers folder which contains the headers which contain the declarations of the functions I require.

0

There are 0 best solutions below