Skip to main content
gamedev.net
🔒 Locked

[Lua] Luabind problems...

Started by faijj 2 replies 1.5k views
Original Post
faijj
faijj
Hello. Followed a guide and compiled luabind. So, im able to use most of luabind, but unable to expose functions to lua. Heres the code:

extern "C"
{
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
}
#include "lua.hpp"
#include "luabind.hpp"
#include <iostream>

using namespace std;

class NumberPrinter {
  public:
    NumberPrinter(int number) :
      m_number(number) {}

    void print() {
      cout << m_number << endl;
    }

    int m_number;
};

int main()
{
    lua_State *L = lua_open();
    luaL_openlibs(L);
    luabind::open(L);

    luabind::module(L)
    [
        luabind::class_<NumberPrinter>("NumberPrinter")
            .def(luabind::constructor<int>())
            .def("print", &NumberPrinter::print)    //this is not working, getting errors here
            .def_readwrite("m_number", &NumberPrinter::m_number)
    ];

    NumberPrinter no(0);
    luabind::globals(L)["no"] = &no

    luaL_dostring(L, "no.m_number = 23\nprint(no.m_number)");

    lua_close(L);

    return 0;
}
Here are the errors im getting, if i comment out the line .def("print", &NumberPrinter::print) im not getting errors and everything is compiling perfectly.

)]+0x220)||undefined reference to `luabind::detail::class_base::add_method(char const*, luabind::detail::overload_rep const&)'|
 int)]+0x8a)||undefined reference to `luabind::detail::class_rep::convert_to(std::type_info const*, luabind::detail::object_rep const*, void*) const'|
||=== Build finished: 2 errors, 0 warnings ===|
Cygon
Cygon
Looks like you either didn't compile all of the LuaBind sources, or you didn't link the static library of LuaBind you compiled (by your linker error, it's definitely looking for a static library, so if you compiled LuaBind as a DLL, you probably need to set some #define constant to make it work).

LuaBind consists mostly of headers and only a few source files that you have to compile, so while I haven't tried it, most likely LuaBind would be working partially even if you left out the compiled source files. That's why I don't exclude the possibility that somehow, the linker doesn't have access to the compiled LuaBind sources at all.

- You can try to include the LuaBind source files directly in your project, if it works, you know something is missing in your static library.

- To find out what's missing, just run a text search in the LuaBind source files for the symbols the linker can't find, eg. "convert_to".
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
faijj
faijj
Quote:
Original post by Cygon
Looks like you either didn't compile all of the LuaBind sources, or you didn't link the static library of LuaBind you compiled (by your linker error, it's definitely looking for a static library, so if you compiled LuaBind as a DLL, you probably need to set some #define constant to make it work).

LuaBind consists mostly of headers and only a few source files that you have to compile, so while I haven't tried it, most likely LuaBind would be working partially even if you left out the compiled source files. That's why I don't exclude the possibility that somehow, the linker doesn't have access to the compiled LuaBind sources at all.

- You can try to include the LuaBind source files directly in your project, if it works, you know something is missing in your static library.

- To find out what's missing, just run a text search in the LuaBind source files for the symbols the linker can't find, eg. "convert_to".


Okay, so.. I included the luabind sources in my project and it worked perfectly. The text search on "convert_to" lead to class_rep.cpp, which i have included in my static library project.
faijj
faijj
How do i bind arrays to lua with luabind? So i can access them like this in lua:
print(testclass.name[2])


[Edited by - faijj on April 12, 2009 4:24:16 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.