Reference the Lua interpreter in Visual Studio, compile the Lua source code, and execute the Lua script


Preface

What is this article talking about

Introduce lua interpreter in Visual Studio
Use C++ to call Lua files

What is this article suitable for

Suitable for beginners beginner Lua
Suitable for people who need to combine development of C/C++ and lua

What is needed for this article

Have a simple understanding of Lua syntax
Have a simple understanding of C/C++ syntax
Rely on the environment of Lua5.1
Rely on VS 2017 editor

Features of this article

Full-process graphic teaching
Emphasis on practice, light theory, quick start
Provide the whole process source code content





Improve reading experience


? Level 1 title

? Secondary title

? Third-level title

? Level 4 title

Table of Contents

  • ? Introduce local Lua environment
    • ? Confirm whether the local environment exists
    • ? Find the Lua installation directory
    • ? Description file and lib library
    • ? New C + + empty project
    • ? Import header files and libraries
    • ? Test case
  • ? Cited externally downloaded Lua environment
    • ? Download file
    • ? Import file
  • ? Import after compiling the Lua source code
    • ? Download Lua source code
    • ? Create a static library project
    • ? Copy source code
    • ? Do not use precompiled headers
    • ? Annotate the main method
    • ? Add preprocessor definition tags
    • ? Generate compiled files
    • ? Integrate compiled files
  • ? Push
  • ? Conclusion

? Introduce local Lua environment

If your computer has already installed Lua, then there is a ready-made Lua interpreter that can be imported. This solution can only be used when Lua has been installed in your local environment

? Confirm whether the local environment exists

Open cmd, enter Lua, if there is a version output, Lua has been installed

? Find the Lua installation directory

If you forget the path, you can find the path of Lua in the environment variable

? Description file and lib library

  • include: contains several important header files

  • lib: compiled lua source library

? New C + + empty project

We create a C++ empty project through Visual Studio

Note: need to install c++ desktop development related environment

? Import header files and libraries

The first step is to right-click the directory under the solution and open the property page

The second step is to find General under C/C++ , and add the include we just added under lua in Additional include directory folder directory

The third step, under the Input option of Connector, fill in lua5.1 in the lib folder in Additional dependencies. The name of the lib

The fourth step, under the General option of the linker, add the directory of the lib file in the Additional library directory

? Test case

We prepare a lua file in the project directory with a simple output


Refer to the relevant environment in the C++ code and load the lua file

Note:lua.hpp file is in the include directory, the actual code is as follows

Execute the debugger and successfully output the content of the lua file

? Reference the externally downloaded Lua environment

If the Lua environment is not installed locally, or if you want to use the specified Lua version, you can directly download the ready-made header files and libraries from the official website

? Download file

Enter the lua official website download page, select the binaries option

http://www.lua.org/download.html

Choose the version you want in the history

Choose the download file according to the platform version


After downloading and decompressing, it will look like the picture below. There is a lib library in the directory, and the header file is in the include file

? Import file

After creating the C++ empty project above, we directly create two files in the directory, which are include and lib files

Then put the library in the downloaded directory into the lib folder, and put the header files under include in the include directory of the project directory

The next step is the same as the above steps to introduce header files and libraries, add the name and directory in the property page, because it is directly in the project directory, so the relative path is used


? Import after compiling the Lua source code

The above solutions are to directly import the compiled Lua interpreter. Another solution is to download the source code of the corresponding version of Lua from the official website, and then compile it yourself. The advantage of this is that you can expand the functions of Lua on the basis of the source code.

? Download Lua source code

The same goes to the Lua official website download page, select the download option

Select the corresponding version to download and decompress

The decompression directory is as follows

? Create a static library project

Create a C++ static library project in VS, named lua5.1

Delete automatically created files

? Copy source code

Copy the source code src directory to the project directory


Right-click the project in the solution, select Add Existing Item, and add all the .h and .c files in the src directory to the project


? Do not use precompiled headers

Right-click the project in the solution, precompiled header under C/C++ in the property page, change to not use precompiled header

? Annotate the main method

Comment out the main method in lua.c and luac.c, we don’t need it to have its own execution entry strong>


? Add preprocessor definition tags

Right-click the project in the solution, preprocessor under C/C++ in the property page, add the _CRT_SECURE_NO_DEPRECATE tag, otherwise it will report an error< /strong>

? Generate compiled files

Right-click the project in the solution, select Generate, and you can see the output of the successful generation in the output column. So far, the operation of generating the compiled file by ourselves has ended

? Integrate compiled files

Go back to the project directory of our static library, enter the Debug directory, and find the lua5.1.lib file. This generated file is the library we referenced above

Note: The name of this lib file is the same as our static library project name, here I use the same name as the official static library, so the generated file is consistent with the downloaded file


In the src directory of the source code, the four selected header files are the same as the files in the include directory we used before


Find these files, and the rest of the steps are the same as before

? Push

  • Github
https://github.com/KingSun5

? Conclusion

If you think the blogger’s article is well written, you may wish to pay attention to the blogger and like the blog post. In addition, the blogger’s ability is limited. If there are any mistakes in the article, you are welcome to comment and criticize.


This article is an original article, please comment and leave a message for reprinting, and the source of the famous author at the head of the reprinted article
syntaxbug.com © 2021 All Rights Reserved.