1

テスト用に次の C++ コードがあります。

#include <lua.hpp>
#include <iostream>

static int dummy(lua_State * L)
{
   std::cout << "Test";
   return 0;
} 

int luaopen_testlib(lua_State * L)
{
   lua_register(L,"dummy",dummy);
   return 0;
}

コマンドでコンパイルしましたが、エラーは発生しません。

g++  -Wextra -O2 -c -o testlib.o main.cpp
g++ -shared -o testlib.so testlib.o 

しかし、lua にロードしようとすると、次のような未定義のシンボル エラーが発生します。

Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> require"testlib"
error loading module 'testlib' from file './testlib.so':
./testlib.so: undefined symbol: _Z16lua_pushcclosureP9lua_StatePFiS0_Ei

g ++コマンドに何かが欠けているようですが、午前中ずっと解決策を探していて、この簡単な例をコンパイルできません。

編集:

いくつかの再コンパイルの後、次のように戻りました。

error loading module 'testlib' from file './testlib.so':
./testlib.so: undefined symbol: luaopen_testlib

追加することで解決されました:

extern "C" 
{
int luaopen_testlib(lua_State *L)
{
  lua_register(L,"dummy",dummy);
  return 0;
}
}
4

3 に答える 3

0

Luabindを使用してみてください。これがHello Worldの例です

#include <iostream>
#include <luabind/luabind.hpp>

void greet()
{
    std::cout << "hello world!\n";
}

extern "C" int init(lua_State* L)
{
    using namespace luabind;

    open(L);

    module(L)
    [
        def("greet", &greet)
    ];

    return 0;
}
于 2015-01-25T18:31:55.333 に答える