关于MinGW Lua环境配置是本文要介绍的内容,主要是来学习lua中环境配置的问题,具体内容来看本文详解。
虽说VC是Windows下C++开发的***工具,但有时候写一些小程序,我还是更倾向于使用一些轻量级的开发工具。比如最近研究Lua嵌入C++,有时候只是写一些很小的程序,这时候Visual Studio就显得太过臃肿了,于是我选择了EditPlus+MinGW的方法。搞了两个晚上,终于搭建好了环境,在这里总结一下,以备将来查看。
MinGW的安装比较简单了。我偷懒直接下了个自动安装程序,他会自己去下载***的包,并解压到正确的位置。为了更方便的使用,还需要配置环境变量,google了一下,还挺多。要是每到一台机器都要这样配置的话,那不是很累。本着计算机能完成的工作绝不自己做的原则,我决定写个js脚本配置环境变量。最开始采用了修改注册表的方法,但是后来发现这种方式修改环境变量是不能立即生效的。又google了一下,改用WScript的shell对象来直接修改环境变量。
要配置的环境变量:
- MINGW_PATH = “MinGW的主目录”
- C_INCLUDE_PATH = "%MINGW_PATH%\\include;%MINGW_PATH%\\lib\\gcc\\mingw32\\3.4.5\\include ";
- CPLUS_INCLUDE_PATH = "%MINGW_PATH%\\include\\c++\\3.4.5;%MINGW_PATH%\\include\\c++\\3.4.5\\mingw32;%MINGW_PATH%
- \include\\c++\\3.4.5\\backward;%C_INCLUDE_PATH% ";
- LIBRARY_PATH = "%MINGW_PATH%\\lib;%MINGW_PATH%\\lib\\gcc\\mingw32\\3.4.5";
- PATH = "%MINGW_PATH%\\bin;%MINGW_PATH%\\libexec\\gcc\\mingw32\\3.4.5;";
写成js脚本,把这个脚本放到MinGW的主目录里,双击运行就可以了。
- function GetCurrPath ()
- {
- fso = new ActiveXObject ("Scripting.FileSystemObject" );
- currFolder = fso . GetFolder ("." );
- return currFolder . path
- }
- //取得当前路径
- MINGW_PATH = GetCurrPath ();
- C_INCLUDE_PATH = "%MINGW_PATH%\\include;%MINGW_PATH%\\lib\\gcc\\mingw32\\3.4.5\\include " ;
- CPLUS_INCLUDE_PATH = "%MINGW_PATH%\\include\\c++\\3.4.5;%MINGW_PATH%\\include\\c++\\3.4.5\\mingw32;%MINGW_PATH%\\include\
- \c++\\3.4.5\\backward;%C_INCLUDE_PATH% " ;
- LIBRARY_PATH = "%MINGW_PATH%\\lib;%MINGW_PATH%\\lib\\gcc\\mingw32\\3.4.5" ;
- PATH = "%MINGW_PATH%\\bin;%MINGW_PATH%\\libexec\\gcc\\mingw32\\3.4.5;" ;
- var WshShell = WScript . CreateObject ("WScript.Shell" );
- var WshSysEnv = WshShell . Environment ("SYSTEM" );
- WshSysEnv ("MINGW_PATH" ) = MINGW_PATH ;
- WshSysEnv ("C_INCLUDE_PATH" ) = C_INCLUDE_PATH ;
- WshSysEnv ("CPLUS_INCLUDE_PATH" ) = CPLUS_INCLUDE_PATH ;
- WshSysEnv ("LIBRARY_PATH" ) = LIBRARY_PATH ;
- PATHPATH = PATH + WshSysEnv ("PATH" );
- WshSysEnv ("PATH" ) = PATH ;
- WScript . Echo ("MinGW环境变量设置完成!" );
接下来是Lua环境,下载Lua的bin和lib以后,做成下面的目录结构:
- Lua
- │
- ├─bin
- │ bin2c.exe
- │ lua.exe
- │ lua5.1.dll
- │ lua51.dll
- │ luac.exe
- │ wlua.exe
- │
- ├─include
- │ lauxlib.h
- │ lua.h
- │ lua.hpp
- │ luaconf.h
- │ lualib.h
- │
- └─lib
- lua5.1.lib
- lua51.lib
然后仍然是js脚本配置环境变量:
- function GetCurrPath ()
- {
- fso = new ActiveXObject ("Scripting.FileSystemObject" );
- currFolder = fso . GetFolder ("." );
- return currFolder . path
- }
- //取得当前路径
- LUA_HOME = GetCurrPath ();
- LUA_BIN = "%LUA_HOME%\\bin;" ;
- LUA_INCLUDE = "%LUA_HOME%\\include;" ;
- LUA_LIB = "%LUA_HOME%\\lib;" ;
- var WshShell = WScript . CreateObject ("WScript.Shell" );
- var WshSysEnv = WshShell . Environment ("SYSTEM" );
- WshSysEnv ("LUA_HOME" ) = LUA_HOME ;
- //bin加入path
- WshSysEnv ("PATH" ) = LUA_BIN + WshSysEnv ("PATH" );
- //include加入MinGW
- WshSysEnv ("C_INCLUDE_PATH" ) = LUA_INCLUDE + WshSysEnv ("C_INCLUDE_PATH" );
- //lib加入MinGW
- WshSysEnv ("LIBRARY_PATH" ) = LUA_LIB + WshSysEnv ("LIBRARY_PATH" );
- WScript . Echo ("LUA环境变量设置完成!" );
- 有时候设置好了却不生效,重启一下就好了。先写个Hello World测试一下。
- #include <lua.hpp>
- #include <iostream>
- using namespace std ;
- int main ()
- {
- lua_State * L = lua_open ();
- lua_cpcall (L , luaopen_base , 0 );
- if (luaL_loadfile (L , "test.lua" ) || lua_pcall (L , 0 , 0 , 0 ))
- cout << lua_tostring (L , - 1 )<< endl ;
- lua_close (L );
- system ("pause" );
- return 0 ;
- }
- test.lua:
- print (" Hello World!" )
- makefile:
- all : main.exe
- #all:rebuild
- main.exe
- main.exe : main.o
- g++ -o $@ $^ -llua5.1
- main.o : main.cpp
- g++ -c main.cpp
- clear :
- -del *.exe *.o
- rebuild : clear main.exe
直接make,编译、连接、运行一次搞定。
- g++ -c main.cpp
- g++ -o main.exe main.o -llua5.1
- main.exe
- Hello World!
请按任意键继续。
小结:关于MinGW Lua环境配置学习教程的内容介绍完了,希望通过本文的学习能对你有所帮助!