vendredi 29 mai 2015

Eclipse C lua integration

I'm trying to get lua to integrate into my eclipse C environment. This should be a straightforward process of just including a few files, however I am getting an 'undefined reference' to every lua function called. Code: (keep in mind this is directly copied from http://ift.tt/1z3Ditl)

/*
 * test.c
 * Example of a C program that interfaces with Lua.
 * Based on Lua 5.0 code by Pedro Martelletto in November, 2003.
 * Updated to Lua 5.1. David Manura, January 2007.
 */

#include "C:/Users/Dylan/workspace/C in Lua/lua/lua.h"
#include "C:/Users/Dylan/workspace/C in Lua/lua/lauxlib.h"
#include <stdlib.h>
#include <stdio.h>


int
main(void)
{
    int status, result, i;
    double sum;
    lua_State *L;

    /*
     * All Lua contexts are held in this structure. We work with it almost
     * all the time.
     */
    L = luaL_newstate();

    luaL_openlibs(L); /* Load Lua libraries */

    /* Load the file containing the script we are going to run */
    status = luaL_loadfile(L, "script.lua");
    if (status) {
        /* If something went wrong, error message is at the top of */
        /* the stack */
        fprintf(stderr, "Couldn't load file: %s\n", lua_tostring(L, -1));
        exit(1);
    }

    /*
     * Ok, now here we go: We pass data to the lua script on the stack.
     * That is, we first have to prepare Lua's virtual stack the way we
     * want the script to receive it, then ask Lua to run it.
     */
    lua_newtable(L);    /* We will pass a table */

    /*
     * To put values into the table, we first push the index, then the
     * value, and then call lua_rawset() with the index of the table in the
     * stack. Let's see why it's -3: In Lua, the value -1 always refers to
     * the top of the stack. When you create the table with lua_newtable(),
     * the table gets pushed into the top of the stack. When you push the
     * index and then the cell value, the stack looks like:
     *
     * <- [stack bottom] -- table, index, value [top]
     *
     * So the -1 will refer to the cell value, thus -3 is used to refer to
     * the table itself. Note that lua_rawset() pops the two last elements
     * of the stack, so that after it has been called, the table is at the
     * top of the stack.
     */
    for (i = 1; i <= 5; i++) {
        lua_pushnumber(L, i);   /* Push the table index */
        lua_pushnumber(L, i*2); /* Push the cell value */
        lua_rawset(L, -3);      /* Stores the pair in the table */
    }

    /* By what name is the script going to reference our table? */
    lua_setglobal(L, "foo");

    /* Ask Lua to run our little script */
    result = lua_pcall(L, 0, LUA_MULTRET, 0);
    if (result) {
        fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1));
        exit(1);
    }

    /* Get the returned value at the top of the stack (index -1) */
    sum = lua_tonumber(L, -1);

    printf("Script returned: %.0f\n", sum);

    lua_pop(L, 1);  /* Take the returned value out of the stack */
    lua_close(L);   /* Cya, Lua */

    return 0;
}

The paths for the headers are correct, and I had to add the path to lua.h, after that Eclipse no longer complained about missing files, however it marks all lua functions and says this in the debugger after building: Description Resource Path Location Type

Description Resource    Path    Location    Type
undefined reference to `lua_close'  hello-world.c   /C in Lua   line 82 C/C++ Problem
undefined reference to `lua_createtable'    hello-world.c   /C in Lua   line 43 C/C++ Problem
undefined reference to `lua_pcall'  hello-world.c   /C in Lua   line 70 C/C++ Problem
undefined reference to `lua_pushnumber' hello-world.c   /C in Lua   line 61 C/C++ Problem
undefined reference to `lua_pushnumber' hello-world.c   /C in Lua   line 62 C/C++ Problem
undefined reference to `lua_rawset' hello-world.c   /C in Lua   line 63 C/C++ Problem
undefined reference to `lua_setfield'   hello-world.c   /C in Lua   line 67 C/C++ Problem
undefined reference to `lua_settop' hello-world.c   /C in Lua   line 81 C/C++ Problem
undefined reference to `lua_tolstring'  hello-world.c   /C in Lua   line 34 C/C++ Problem
undefined reference to `lua_tolstring'  hello-world.c   /C in Lua   line 72 C/C++ Problem
undefined reference to `lua_tonumber'   hello-world.c   /C in Lua   line 77 C/C++ Problem
undefined reference to `luaL_loadfile'  hello-world.c   /C in Lua   line 30 C/C++ Problem
undefined reference to `luaL_newstate'  hello-world.c   /C in Lua   line 25 C/C++ Problem
undefined reference to `luaL_openlibs'  hello-world.c   /C in Lua   line 27 C/C++ Problem

(as seen the program source is called 'helloworld.c') Please help.

Aucun commentaire:

Enregistrer un commentaire