今天在做一個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;
}
后來翻閱,查出原因:
靜態函數只能在聲明它的文件當中可見,不能被其他文件所調用,也就是說靜態函數只能在聲名它的文件中調用,在其他文件里是不能被調用的。
當然,其實我這里在頭文件里做靜態函數的聲明也是完全沒有必要的。去除后,就可以了。
