按照教程是可以成功的:http://cn.cocos2d-x.org/article/index?type=wiki&url=/doc/cocos-docs-master/manual/framework/native/wiki/how-to-use-bindings-generator/zh.md
谢谢大家的解答~用vs2012运行项目可以成功在lua上调用c++函数,但是player运行找不到c++函数。。。
我之前就是按这个的,报错了,脚本运行成功了,但是说找不到类,为nil、
怎么会在lua里面这样用呢?首先你用的到底是MyClass这个类还是CustomClass这个类?绑定完也应该是cc.CustomClass这样的形式啊,哪来的my这个前缀?
教程是可以的,但是player运行找不到函数~
player需要重新编译,把自定义的C++类加入编译下
具体怎么做呢?
打开player目录,里面有proj.mac和proj.win32,打开你要的平台项目,然后就和普通的工程一样了,把自定义的类和第三方框架什么的集成进去,编译运行会生成新的player执行文件
谢啦~成功了!
不行啊。。。。
我真机和player都找不到MyClass,我没有加namespace;
player也编译过了,MyClass.cpp MyClass.h ,lua_MyClass_auto.cpp和h都加入player工程了;
我试了好几个文章,官方的非官方的,就是lua 中找不到 对应的定义 。。。。。。。

环境:
mac,quick3.3
#include "cocos2d.h"
using namespace cocos2d;
class MyClass : public Ref
{
public:
MyClass() {};
~MyClass() {};
bool init() { return true; };
CREATE_FUNC(MyClass);
int foo(int i);
};
```
#include "MyClass.h"
int MyClass::foo(int i)
{
return i + 100;
}
```
MyClass.ini:
# the prefix to be added to the generated functions. You might or might not use this in your own
# templates
prefix = MyClass
# create a target namespace (in javascript, this would create some code like the equiv. to `ns = ns || {}`)
# all classes will be embedded in that namespace
target_namespace =
android_headers = -I%(androidndkdir)s/platforms/android-14/arch-arm/usr/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.7/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/include -I%(androidndkdir)s/sources/cxx-stl/gnu-libstdc++/4.8/include
android_flags = -D_SIZE_T_DEFINED_
clang_headers = -I%(clangllvmdir)s/lib/clang/3.3/include
clang_flags = -nostdinc -x c++ -std=c++11
cocos_headers = -I%(cocosdir)s/cocos -I%(cocosdir)s/my -I%(cocosdir)s/cocos/base -I%(cocosdir)s/cocos/platform/android
cocos_flags = -DANDROID
cxxgenerator_headers =
# extra arguments for clang
extra_arguments = %(android_headers)s %(clang_headers)s %(cxxgenerator_headers)s %(cocos_headers)s %(android_flags)s %(clang_flags)s %(cocos_flags)s %(extra_flags)s
# what headers to parse
headers = %(cocosdir)s/../runtime-src/Classes/MyClass.h
# what classes to produce code for. You can use regular expressions here. When testing the regular
# expression, it will be enclosed in "^$", like this: "^Menu*$".
classes = MyClass
# what should we skip? in the format ClassName::
# ClassName is a regular expression, but will be used like this: "^ClassName$" functions are also
# regular expressions, they will not be surrounded by "^$". If you want to skip a whole class, just
# add a single "*" as functions. See bellow for several examples. A special class name is "*", which
# will apply to all class names. This is a convenience wildcard to be able to skip similar named
# functions from all classes.
skip =
rename_functions =
rename_classes =
# for all class names, should we remove something when registering in the target VM?
remove_prefix =
# classes for which there will be no "parent" lookup
classes_have_no_parents =
# base classes which will be skipped when their sub-classes found them.
base_classes_to_skip =
# classes that create no constructor
# Set is special and we will use a hand-written constructor
abstract_classes =
# Determining whether to use script object(js object) to control the lifecycle of native(cpp) object or the other way around. Supported values are 'yes' or 'no'.
script_control_cpp = no
```
AppDelegate.cpp注册:
#include "lua_MyClass_auto.hpp"
lua_State* state = stack->getLuaState();
lua_getglobal(state, "_G")
register_all_MyClass(state);
lua_pop(state, 1);
```
lua:
local test = MyClass.new()
```