[Switch]
After learning Lua for a while, it is obviously inconvenient to use it directly in the project. After Google, it seems that everyone is interested in luabind, The Lua packaging class, and I want to use it as soon as possible.
First, make preparations and download the luabin 0.8.1 source code, boost 1.3.8 source code and Lua 5.1.4 source code. The compiling environment is vs2008 SP1. I have seen some articles on the internet saying that version dependencies between these open-source software are sensitive, and there may be such problems. I am a little scared before I start.
Fortunately, the compilation process is smooth as follows:
1. Compile Lua 5.1.4
Go to the command line tool of vs2008, locate the Lua source code directory, and execute the Command etc \ luavs. bat. If there is no problem, you can quickly compile Lua and get lua51.lib and lua51.dll.
2. Compile luabind
Decompress the downloaded luabind compressed package, assuming that decompress to D: \ luabind-0.8.1 \, boost decompress to D: \ boost unzip 38_0 \, Lua decompress to D: \ Lua 5.1.4 \
Create a static library project in Vs, add all the source code under D: \ luabind-0.8.1 \ SRC to the project, and then create the luabind and luabind \ detail \ virtual folders in the project, add files under D: \ luabind-0.8.1 \ luabind and D: \ luabind-0.8.1 \ luabind \ detail to the folder.
Add the include directory to the project, right-click the project node, choose Properties> Configure Properties> C \ c ++, and enter D in the Add include directory: \ luabind-0.8.1 \; D: \ boost listen 38_0 \ K; D: \ Lua 5.1.4 \ SRC \. Modify the project character set to multi-byte character set.
Prepare and generate the project. The compilation in my environment is smooth and there is no problem. After the compilation is successful, you will get luabind. Lib.
Next, write a hello World Program as the first step to use luabind.
Create a new console project in vs. The type is DLL, name the project as Hello world, and type the following code:
#include "stdafx.h" 2#include <iostream> 3#include <luabind/luabind.hpp> 4 5void greet() 6{ 7 std::cout << "hello world!\n"; 8} 910extern "C" int __declspec(dllexport) init(lua_State* L)11{12 using namespace luabind;1314 open(L);1516 module(L)17 [18 def("greet", &greet)19 ];2021 return 0;22}
Note: before using the init function in windows, add _ declspec (dllexport) to export the function. The environment in the luabind document is Linux, by default, you do not need to add _ declspec (dllexport) or export (because of this, I ran Hello word successfully for half a day ).
Compile the project. (remember to add luabind. lib and lua51.lib to the link options: Project properties-> connector-> input-> Add dependency files, and add luabind. lib and lua51.lib ).
Put Hello world.dllto the directory where lua51.dlland lua.exe are located.
Open the Lua command line and type:
From: http://www.cppblog.com/eros/archive/2009/04/29/81508.aspx