lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi there, I'm screwing around with tolua++ and generating some code binds and so on. I have a small function call my_func() and it does almost nothing. This function is declared in a header, 'test.h'

---------------------------------------------------------
//tolua_begin
void my_func(int blah);
//tolua_end

---------------------------------------------------------
using tolua++, I generate a .cpp file for this --
---------------------------------------------------------
/*
** Lua binding: test
** Generated automatically by tolua++-1.0.92 on 09/06/06 00:26:55.
*/

#ifndef __cplusplus
#include "stdlib.h"
#endif
#include "string.h"

#include "tolua++.h"

/* Exported function */
TOLUA_API int  tolua_test_open (lua_State* tolua_S);

#include "test.h"

/* function to register type */
static void tolua_reg_types (lua_State* tolua_S)
{
}

/* function: my_func */
#ifndef TOLUA_DISABLE_tolua_test_my_func00
static int tolua_test_my_func00(lua_State* tolua_S)
{
#ifndef TOLUA_RELEASE
 tolua_Error tolua_err;
 if (
     !tolua_isnumber(tolua_S,1,0,&tolua_err) ||
     !tolua_isnoobj(tolua_S,2,&tolua_err)
 )
  goto tolua_lerror;
 else
#endif
 {
  int blah = ((int)  tolua_tonumber(tolua_S,1,0));
  {
   my_func(blah);
  }
 }
 return 0;
#ifndef TOLUA_RELEASE
 tolua_lerror:
 tolua_error(tolua_S,"#ferror in function 'my_func'.",&tolua_err);
 return 0;
#endif
}
#endif //#ifndef TOLUA_DISABLE

/* Open function */
TOLUA_API int tolua_test_open (lua_State* tolua_S)
{
 tolua_open(tolua_S);
 tolua_reg_types(tolua_S);
 tolua_module(tolua_S,NULL,0);
 tolua_beginmodule(tolua_S,NULL);
  tolua_function(tolua_S,"my_func",tolua_test_my_func00);
 tolua_endmodule(tolua_S);
 return 1;
}


#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 501
 TOLUA_API int luaopen_test (lua_State* tolua_S) {
 return tolua_test_open(tolua_S);
};
#endif

---------------------------------------------------------
and then after a bit of poking around, I have a main.cpp file
---------------------------------------------------------
#include <iostream>
#include <lua.hpp>
#include "test.h"
using namespace std;

extern "C"
{
    int  tolua_test_open (lua_State*);
}

void my_func(int blah)
{
    cout<<"blah is "<<blah<<endl;
}

int main()
{
    lua_State* L = lua_open();
    luaL_openlibs(L);
    tolua_test_open(L);
    luaL_dofile(L,"test.lua");
}

---------------------------------------------------------

It looks all fine and dandy, doesn't it? Well, I go to compile this in MSVC8.0 and you know what I get? An ugly "unresolved external symbol _my_func referenced in function _tolua_test_my_func00" linker error. What's causing it is this line (which, if I comment out, lets the program compile successfully)

  tolua_function(tolua_S,"my_func",tolua_test_my_func00);

What am I supposed to do make the program compile succesfully?

Also, another side question, let's say I have a game API that's exposed to lua and have a number of scripts which all have a number of functions.. something like OnStart(), where the lua script uses the API to create some objects.. and then another function OnUpdate() where the lua script uses the API to manipulate the object around. How do I call the right onstart/onupdate function? I have a list of the script file names... but how do I call a specific function defined in one file? I should probably use OO for this, huh? How would you go about doing that though?