今天在做一个c函数暴露给lua 时,出现这个问题。
大概代码是这样的,
头文件:
#ifndef LEVEL_DESIGNER_H
#define LEVEL_DESIGNER_H
extern "C" {
#include "lualib.h"
#include "tolua_fix.h"
}
static int saveFileDialog(lua_State *tolus_S);
static int openFileDialog(lua_State *tolus_S);
int open_windows_lua(lua_State *tolus_S);
#endif
源文件:
#include "LevelDesignerUtils.h"
#include <afxwin.h> // MFC 核心组件和标准组件
#include <afxext.h> // MFC 扩展
#include <afxdisp.h> // MFC 自动化类
//wchar to char
void TC2C(const PTCHAR tc, char * c)
{
#if defined(UNICODE)
WideCharToMultiByte(CP_ACP, 0, tc, -1, c, wcslen(tc), 0, 0);
c[wcslen(tc)] = 0;
#else
lstrcpy((PTSTR)c, (PTSTR)tc);
#endif
}
static int openFileDialog(lua_State *tolus_S)
{
// TODO: 在此添加命令处理程序代码
CFileDialog *dlg = new CFileDialog(true, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("json文件(*.json)|*.json|配置文件(*.dat)|*.dat|所有文件(*.*)|*.*||"));
INT_PTR nResponse = dlg->DoModal();
CString filPath = "";
if (IDCANCEL == nResponse)
{
delete dlg;
}
else if (IDOK == nResponse)
{
filPath = dlg->GetPathName();
delete dlg;
}
char filepathc[MAX_PATH];
TC2C(filPath.GetBuffer(), filepathc);
lua_pushstring(tolus_S, filepathc);
return 1;
}
static int saveFileDialog(lua_State *tolus_S)
{
CFileDialog *dlg = new CFileDialog(false, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("json文件(*.json)|*.json|配置文件(*.dat)|*.dat|所有文件(*.*)|*.*||"));
INT_PTR nResponse = dlg->DoModal();
CString filPath = "";
if (IDCANCEL == nResponse)
{
delete dlg;
}
else if (IDOK == nResponse)
{
filPath = dlg->GetPathName();
delete dlg;
}
char filepathc[MAX_PATH];
TC2C(filPath.GetBuffer(), filepathc);
lua_pushstring(tolus_S, filepathc);
return 1;
}
int open_windows_lua(lua_State *tolus_S)
{
lua_register(tolus_S, "win_openFileDialog", openFileDialog);
lua_register(tolus_S, "win_saveFileDialog", saveFileDialog);
return 0;
}
后来翻阅,查出原因:
静态函数只能在声明它的文件当中可见,不能被其他文件所调用,也就是说静态函数只能在声名它的文件中调用,在其他文件里是不能被调用的。
当然,其实我这里在头文件里做静态函数的声明也是完全没有必要的。去除后,就可以了。