3

im trying out other languages. Got VB2013 and LuaForWindows 5.1 What is the most basic file structure to run a .lua file in my program?? I have currently done http://www.youtube.com/watch?v=w51pftzS1_8 includes part, made a .h file that looks like this

#ifndef __LUA_INC_H__
#define __LUA_INC_H__


#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"


#endif // __LUA_INC_H__

and a Run.cpp like this

#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;
}

the test.lua file is in the vb213 projects dir/MYPROJECT/MYPROJECT

and looks like this

function helloWorld ()
 io.write ("hello World")
end
4

2 回答 2

2

From VS2013 and Lua for Windows, you are going to have some pain related to getting the right C Runtime Library version in play. Lua for Windows was compiled against an older version of the CRT, that came with VS2005. It may or may not be possible to get VS2013 to link against that older version. Mixing CRT versions is a recipe for much confusion.

The easiest way out is to get a version of the Lua core built for your version of Visual Studio. There are two ways to do that.

  1. Download a version from Lua Binaries. There are pre-built versions of Lua available from the "official" Lua Binaries distribution. It can be had for 32-bit and 64-bit builds, for Windows, and for other platforms.

  2. Build Lua yourself as part of your solution. Building your own Lua51.dll is straightforward, the default configuration is sensible. It mostly amounts to including almost all the .c files in a DLL project. Note that lua.c is not part of the DLL, that is the source to lua.exe. Similarly, luac.c is not part of the DLL, it is the source to luac.exe which does require some care to build yourself; but you aren't likely to need it.

Either way, you need to pay attention to some details.

The Lua API is a C API, not C++. So if you insist on making your application be a C++ application, you should wrap the inclusion of the Lua header files inside of an exern "C" block:

extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}

In principle, you can build the Lua core as C++ instead. The core is written in a clean C flavor that is also a subset of C++, and is tested when compiled as C++. However, if you go that route then you must build any binary modules yourself against your C++ linkage, and that way can lie madness if you depend on many community supplied modules.

I believe that all of the builds available at Lua Binaries are compiled as C, not C++, so the extern "C" declaration will be required with those.

于 2013-11-08T22:43:02.340 回答
0

Well, for starters, I would recommend scratching c++ and switching to lua as your primary programming language. It is quite messy to call lua functions from with c++, at least that is my experience. Once you understand enough of Lua, THEN call c/c++ functions from Lua via dll or other file/system formats. I was torn between learning python or c/c++ and I chose python. From there I learned about Lua and loved it more than python. Again this is just my experience and suggestion. Now to answer your question, why run Lua in C++? Lua is based on the C API, so I would just use raw C or just stick with C++. I say this only because Lua is technically no different than a watered down version of C. However, Lua is not "watered down" any more, as it can be used as a fully operational standalone programming language.

于 2014-12-06T21:06:55.993 回答