Document directory
- 1 Preface
- 2. compile in the Visual Studio Environment
- 3 code: compilation in the blocks + GCC (mingw) Environment
- 4. Write simple extensions
- 5 postscript
1 Preface
The basic compilation instructions of Lua are clearly stated in the install file in the source code package. Here we will focus on how to compile it in the IDE environment.
2. compile in the Visual Studio Environment
Create a solution "Lua", which contains three projects: two console Projects "Lua" and "luac", and one DLL project "lua51 ", add the source file to the three projects according to the method in the install file. It is easy to remember which files are added. Remove "Lua" from the "lua51" project. c "," luac. C and print. c "," LUA "project only needs" LUA. c "," luac "project requires" luac. C and print. C ".
Project dependencies: "Lua" and "luac" both depend on "lua51 ".
You must add the custom compilation option "/dlua_build_as_dll" to the three project options "! Otherwise, the compiled DLL does not have any export function (this is because. in the "h" header file, the "lua_build_as_dll" macro controls the definitions of "_ declspec (dllexport)" and "_ declspec (dllimport, vs compiler needs this definition when compiling DLL, but GCC does not ).
When the vs compiler is used, the "luac" link will fail, prompting that some function definitions cannot be found. This is actually a bug. You can use SVN to download the latest Lua source code, however, using GCC is a better method.
3 code: compilation in the blocks + GCC (mingw) Environment
The above is a better way to use GCC, and another reason is to avoid the problem of God damned fucking VC runtime in different versions of Visual Studio (see "http://blog.koroirc.com/2008/06/visual-cpp-woes/", static connections can solve this problem, however, the compiled files must be larger, and it is uncomfortable to think that the same code is inserted in each compiled EXE and DLL !).
Creating a solution in CB (http://www.codeblocks.org/) is similar to the way the project is created (in CB), dependency can be directly adjusted up and down order of the project without setting (lua51 projects are placed at the top ), if you want to set dependencies, set them in the attributes of any project:
Figure 3 configure project dependencies in CB
In the "lua51" project attribute, change the target file name and remove the "lib" Prefix:
Figure 3 Release 2 remove the prefix of the DLL project target file
The link to the Lib library should be added to the compilation options of the "Lua" and "luac" projects:
Figure 3 connecting database 3 to lua51
In this way, the compiled lua.exe, luac.exe, and lua51.dll are quite small, because they are dynamically linked to "msvcrt. dll" instead of the disgusting "msvcrxx. dll ".
Figure 3 dependency 4
4. Write simple extensions
With reference to the http://blog.csdn.net/wzhwkent/archive/2009/07/20/4363367.aspx, I have simplified the content here.
In fact, it is to compile a DLL that Lua can recognize. In the GCC environment, everything is so simple: add the "extern" keyword before the function or variable to be exported (you can also do this without adding it ), do not need to export, add the "static" keyword-Standard C Specification!
# Include "../src/Lua. H"
# Include "../src/lualib. H"
# Include "../src/lauxlib. H"
# Include <math. h>
Static int l_sin (lua_state * l ){
Double D = lual_checknumber (L, 1 );
Lua_pushnumber (L, sin (d ));
Return 1;/* Number of Results */
}
Static struct lual_reg lrlibs [] = {
{"Sin", l_sin
}, {Null, null}/* Sentinel */
};
Extern int luaopen_mylib (lua_state * l ){
Lual_register (L, "mylib", lrlibs );
Return 1;
}
The compiled DLL is also very small!
Figure 4 dependency
Figure 4 Procedure 2 program size
Write a simple Lua script and execute it:
Require ("mylib ");
Print (mylib. Sin (10 ));
Execution result:
D:/Lua/release> Lua test. Lua
-0.54402111088937
Well done!
5 postscript
The set of rules in Linux is still good, but it is also normal according to Microsoft's "simple problem complicate" philosophy.
Http://blog.csdn.net/shajunxing/article/details/5131069