C++ c++;lua怎么开始?
我正在尝试其他语言。 获得VB2013和LuaForWindows 5.1在我的程序中运行.lua文件最基本的文件结构是什么?? 我现在已经完成了includes部分,制作了一个.h文件,如下所示C++ c++;lua怎么开始?,c++,lua,C++,Lua,我正在尝试其他语言。 获得VB2013和LuaForWindows 5.1在我的程序中运行.lua文件最基本的文件结构是什么?? 我现在已经完成了includes部分,制作了一个.h文件,如下所示 #ifndef __LUA_INC_H__ #define __LUA_INC_H__ #include "lua.h" #include "lauxlib.h" #include "lualib.h" #endif // __LUA_INC_H__ #include <iostrea
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#endif // __LUA_INC_H__
#include <iostream>
#include <conio.h>
#include <iostream>
#include "LuaInc.h"
using namespace std;
int main()
{
int iErr = 0;
lua_State *lua = lua_open(); // Open Lua
luaopen_io(lua); // Load io library
if ((iErr = luaL_loadfile(lua, "test.lua")) == 0)
{
// Call main...
if ((iErr = lua_pcall(lua, 0, LUA_MULTRET, 0)) == 0)
{
// Push the function name onto the stack
lua_pushstring(lua, "helloWorld");
// Function is located in the Global Table
lua_gettable(lua, LUA_GLOBALSINDEX);
lua_pcall(lua, 0, 0, 0);
}
}
lua_close(lua);
_getch();
return 0;
}
function helloWorld ()
io.write ("hello World")
end
还有像这样的Run.cpp
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#endif // __LUA_INC_H__
#include <iostream>
#include <conio.h>
#include <iostream>
#include "LuaInc.h"
using namespace std;
int main()
{
int iErr = 0;
lua_State *lua = lua_open(); // Open Lua
luaopen_io(lua); // Load io library
if ((iErr = luaL_loadfile(lua, "test.lua")) == 0)
{
// Call main...
if ((iErr = lua_pcall(lua, 0, LUA_MULTRET, 0)) == 0)
{
// Push the function name onto the stack
lua_pushstring(lua, "helloWorld");
// Function is located in the Global Table
lua_gettable(lua, LUA_GLOBALSINDEX);
lua_pcall(lua, 0, 0, 0);
}
}
lua_close(lua);
_getch();
return 0;
}
function helloWorld ()
io.write ("hello World")
end
在VS2013和LUAForWindows中,您将遇到一些与使用正确的C运行时库版本相关的问题。LUAForWindows是根据VS2005附带的旧版本CRT编译的。让VS2013链接到该旧版本可能是可能的,也可能是不可能的。混合使用CRT版本会造成很多混乱 最简单的方法是为您的VisualStudio版本构建一个Lua核心版本。有两种方法可以做到这一点
Lua51.dll非常简单,默认配置是合理的。它主要包括DLL项目中几乎所有的.c文件。请注意,lua.c
不是DLL的一部分,DLL是lua.exe
的源代码。类似地,luac.c
不是DLL的一部分,它是luac.exe
的源代码,这确实需要谨慎地构建自己;但你不太可能需要它
LuaAPI是一个C API,不是C++。因此,如果你坚持让你的应用程序是一个C++应用程序,你应该把Lua头文件包含在<代码>后边“C”< /代码>块中:
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
原则上,你可以用C++来构建Lua内核。核心是用C语言编写的,也是C++的子集,并在C++编译时进行测试。但是,如果你走那条路线,你必须建立你自己的二进制模块来对抗你的C++链接,如果你依赖于许多社区提供的模块,那么这种方式就可能疯狂。
<>我相信所有在Lua二进制文件中可用的编译都是编译成C++,而不是C++,所以<>代码>外“C”<代码>声明将需要那些.,首先,我建议擦除C++并切换到Lua作为你的主要编程语言。用C++调用Lua函数是非常麻烦的,至少这是我的经验。一旦您对Lua有了足够的了解,就可以通过dll或其他文件/系统格式从Lua调用c/c++函数。我在学习python或c/c++之间左右为难,于是选择了python。从那里我了解了Lua,并且比python更喜欢它。这也是我的经验和建议。现在回答你的问题,为什么在C++中运行Lua?LUA是基于C API的,所以我只会使用原始C或者只使用C++。我之所以这样说,只是因为Lua在技术上与C语言的淡化版本没有什么不同。然而,Lua不再是“淡化”的,因为它可以作为一种完全可操作的独立编程语言使用。您是否要求提供一个比上述内容更简单的示例?