首先建立一个export_class的DLL工程,然后建立一个TestClass测试类
TestClass.h
【原】luabind 导出C++类#pragma once
【原】luabind 导出C++类#include 
<string>
【原】luabind 导出C++类#include 
<iostream>
【原】luabind 导出C++类
using namespace std;
【原】luabind 导出C++类
【原】luabind 导出C++类
class TestClass
;


TestClass.cpp
【原】luabind 导出C++类#include "TestClass.h"
【原】luabind 导出C++类
【原】luabind 导出C++类TestClass TestClass::m_Singleton 
= TestClass("default string");
【原】luabind 导出C++类
【原】luabind 导出C++类TestClass::TestClass(
string s)
}

export_class.cpp
【原】luabind 导出C++类 // export_class.cpp : Defines the entry point for the DLL application.
【原】luabind 导出C++类
#include <luabind/luabind.hpp>
【原】luabind 导出C++类
【原】luabind 导出C++类
extern "C"
}

脚本如下:
【原】luabind 导出C++类require "export_class"
【原】luabind 导出C++类
【原】luabind 导出C++类g_print()
【原】luabind 导出C++类
【原】luabind 导出C++类
--testClass = Singleton()
【原】luabind 导出C++类
testClass = TestClass("haha~~")
【原】luabind 导出C++类
【原】luabind 导出C++类
【原】luabind 导出C++类
if testClass == nil then
【原】luabind 导出C++类    
print "testClass is a null value!"
【原】luabind 导出C++类
else
【原】luabind 导出C++类    testClass:Print()
【原】luabind 导出C++类
end

脚本运行结果:
 >lua -e "io.stdout:setvbuf 'no'" "main.lua"
TestClass::TestClass()
hello, Eric Xiang!
TestClass::TestClass()
haha~~
>Exit code: 0

现在还有一点问题,就是用Singleton的静态成员函数调用后再Print有问题,目前我也没有搞明白是怎么回事。太奇怪了。。。。

相关文章: