您的位置:首页 > 编程语言 > Lua

How to embed Lua in C++ on Visual Studio

2015-03-09 15:40 399 查看

How to embed Lua in C++ on Visual Studio

Posted on
December 17, 2012 by shriek

These instructions are geared toward those programmers that are primarily interested in making video games and who wants to embed a scripting language (lightweight language) in their programming language (heavyweight language) for faster development process.
Therefore, some prior knowledge of some programming language is recommended. However, much effort will be done to make even the beginner feel comfortable with the process.

If you have ever worked on huge project, especially on video games, then making few changes in your programming language to fine tune the game will often mean repeating the compiling process (turning your code into machine code) over and over again. This
can cause headaches and unnecessary waste of time. To get past this issue developers often embed scripting language to their programming language to make this process hassle-free. Here, Lua is a scripting language and C++ is a programming language. After the
setup you can run your Lua scripts inside C++.

These instructions will tell you how to 1) download Lua source code 2) extract Lua archive 3) setup proper environment variables 4) build/compile Lua source files 5) configure Visual Studio 6) setup project folders

Resources

Windows 7 or up
Visual Studio 2010 or up
Any file archiver / compressor (7-zip preferred)
Stable Internet Connection
Notepad or any text editor

Download Lua source code. There are thousands of websites that offers Lua source code in the Internet butmake sure you get it from the official site.

Open up your favorite internet browser and go to http://www.lua.org. Click on the download link.
You will be provided with binary files or the source files. Since we are building Lua from scratch we don’t need binary files so click on the download link under “Source” section.
Listed will be all the versions of Lua. Click on the most recent one, usually listed at the top under .gz file extension.
Click on that link and wait for the download to finish.

Extract Lua archive. What we have so far from the download is just a compressed file. Make sure you have your file archiver/compressor installed for these following steps.

Un-compress/extract the download file from your favorite file archiver twice. We need to do it two times because Lua source code usually comes with .tar.gz with double compression.
After the extracting process enter the recently extracted folder. There should be two directories named “src” and “doc”

Setting up Environment Variable. Before you go ahead and type anything, make sure your environment variable for Visual Studio is setup properly. To do this follow the following steps. If you already know it has been setup then skip this
section.

Locate the bin folder of your Visual Studio, usually in (C:\Program Files\Microsoft Visual Studio 11.0\VC\bin). I happen to have Visual Studio 11.0 but this can be different for different version and copy the location folder.

Open up your control panel and look for “System” settings.(If your view is set as “View by: Category” which the default setting usually is, then change is to “View by: Large icons”
Click the “System” and then a new window should pop up and click on “Advance System Settings” on the left hand side.
Another windows should pop up and this time click on the “Environment Variables” button at the bottom and on the “System Variables” section under you should see “Path” Variable.
Double click on the Path’s value and here you will be able to edit the values.
Add your recently copied location to this box. VERY IMPORTANT: If there are already some other values there thenDO NOT DELETE THEM.

Press OK.
Go inside the “src” folder in your recently extracted Lua source files.
Press shift and right click on any blank area in that window (this should bring up extra menu options) and look for “Open Command Window Here”. Click that option.
A console window should open. Type “vcvars32.bat” and hit enter. This is to tell your console window that path of Visual Studio from your Environment Variables is ready.

Building Lua library. Libraries in programming are collection of codes that your program can take advantage of without the need to write your own methods/functions making development process even faster. In our previous steps although we
extracted the source files we still need to compile/build them to get our single library file.

Open up a notepad or your favorite text editor and type the following:-

cl /MD /O2 /W3 /c /DLUA_BUILD_AS_DLL *.c
del *.o
ren lua.obj lua.o
ren luac.obj luac.o
ren print.obj print.o
link /DLL /IMPLIB:lua5.2.lib /OUT:lua5.2.dll *.obj
link /OUT:lua.exe lua.o lua5.2.lib
lib /out:lua5.2-static.lib *.obj
link /OUT:luac.exe luac.o print.o lua5.2-static.lib


Save this file as build.bat. MAKE SURE you select “All types” in the “Save as Type” section if you are doing this from Notepad. Or else Notepad will assume this is just another text file.
The above .bat file has necessary commands and operations that needs for the compilation to take place. WARNING: Those who are using in Operating System other than Windows this will not work.
In the above code I have my version set at 5.2. Make sure you change yours according to the version you downloaded.
After that go ahead and type “build.bat” to execute the recently made batch (.bat) file.
You should see a lot of text scrolling up. This is perfectly normal.
After the scrolling you should have your library file with some additional files, primarily the Lua executable file and dynamic library (.dll) file.

Configuring Visual Studio. We need to configure Visual Studio to let it know where and what libraries and header files to look for when compiling our project. Configuring Visual Studio after you have Lua library file is really simple.

Open up Visual Studio and create a new project. (File->New->Project)
Select an Empty Project and give it any name.
Go to Projects->”Your project name” Settings.

Navigate to Configuration Properties -> C/C++ -> General and under Additional Include Directories type the location to your src folder of your Lua directory.Alternatively, you could have copied lua.h, lua.lib, lua.exe in single folder but I have left
managing your folders up to you

Again, under Configuration Properties->Linker->General, Additional Library Directories copy the same src location.

In the same Linker section go to Input and add lua5.2.lib under “Additional Dependencies”.

Setup the project folder. We are not quite done yet. One last step before you can start embedding your Lua library is to copy “lua.dll” file that was generated in the “src” directory and paste it in your project folders. This file must be
in the same folder where your program’s executable file loads, usually in Debug folder.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: