从Luabind调用C++成员函数导致"找不到匹配的重载"

Jul*_*bot 1 c++ lua luabind

我在DLL中导出了一些导出到Luabind的类,一切都适用于这两个类(LuaScriptManager,EventManager).我可以从Lua调用它们的函数,一切都很好,但现在我正在尝试在我的客户端可执行文件中设置一个与DLL链接的新类,到目前为止还没有运气.

这是我为每个函数调用的错误消息:"找不到匹配的重载,候选:void loadResource(ResourceManager&,std :: string const&)"

类绑定来自http://www.nuclex.org/articles/5-cxx/1-quick-introduction-to-luabind:

struct Manager {
Manager() :
m_ResourceCount(0) {}

void loadResource(const std::string &sFilename) {
++m_ResourceCount;
}
size_t getResourceCount() const {
return m_ResourceCount;
}

size_t m_ResourceCount;
};
static Manager MyResourceManager;

void Bind(lua_State* l)
{
    // Export our class with LuaBind
    luabind::module(l) [
    luabind::class_<Manager>("ResourceManager")
    .def("loadResource", &Manager::loadResource)
    .property("ResourceCount", &Manager::getResourceCount)
    ];

    luabind::globals(l)["MyResourceManager"] = &MyResourceManager;
}
Run Code Online (Sandbox Code Playgroud)

这是相应的lua测试代码:

-- The following call will fail with the above error
MyResourceManager:loadResource("abc.res")
--MyResourceManager:loadResource("xyz.res")

-- If the function is commented out, this will work (accessing the property)
ResourceCount = MyResourceManager.ResourceCount

-- Calling my other classes' functions work fine
LuaScriptManager.GetInstance():WriteLine(ResourceCount)
Run Code Online (Sandbox Code Playgroud)

这种奇怪行为的原因是什么?

小智 5

这是我发送到Luabind邮件列表的邮件的副本. http://sourceforge.net/mailarchive/message.php?msg_id=27420879

我不知道这是否适用于Windows和DLL也一样,但是我已经和GCC类似的经历和共享Linux模块:类,如果跨越共享库边界使用Luabind注册,其中只有共享库中是有效的,但造成的分割故障.

解决方案是修补luabind :: type_id类,并使用typeid(T).name()而不是typeid(T):: operator =进行比较.对于GCC,这里解释了typeid运算符可能无法跨共享库工作的原因[1].在这种特殊情况下,我使用Lua的require()加载了共享库,遗憾的是,它没有将RTLD_GLOBAL传递给dlopen.

[1] http://gcc.gnu.org/faq.html#dso
Run Code Online (Sandbox Code Playgroud)

typeid等式问题出现在其他C++库中,例如在boost :: any [2]中,具有相同的fix [3],typeid(T).name()比较.

[2] https://svn.boost.org/trac/boost/ticket/754
[3] https://svn.boost.org/trac/boost/changeset/56168
Run Code Online (Sandbox Code Playgroud)

也许附加的补丁也有助于DLL的情况.

--- include.orig/luabind/typeid.hpp
+++ include/luabind/typeid.hpp
@@ -6,6 +6,7 @@
 # define LUABIND_TYPEID_081227_HPP

 # include <boost/operators.hpp>
+# include <cstring>
 # include <typeinfo>
 # include <luabind/detail/primitives.hpp>

@@ -33,17 +34,17 @@

     bool operator!=(type_id const& other) const
     {
-        return *id != *other.id;
+        return std::strcmp(id->name(), other.id->name()) != 0;
     }

     bool operator==(type_id const& other) const
     {
-        return *id == *other.id;
+        return std::strcmp(id->name(), other.id->name()) == 0;
     }

     bool operator<(type_id const& other) const
     {
-        return id->before(*other.id);
+        return std::strcmp(id->name(), other.id->name()) < 0;
     }

     char const* name() const
Run Code Online (Sandbox Code Playgroud)