How to call C ++ functions in Lua

Source: Internet
Author: User

LuaCallC ++ FunctionsImplementation teaching is the content to be introduced in this article, mainly to learnLuaFor more information about language calls, see this document.

Environment: Visual C ++ 2008 Express Edition + Lua 1.5 + SciTE (lua editor)

Download Visual C ++ 2008 Express Edition + Lua, download and install it, and omit N words.

Environment Configuration:

1. Open Visual C ++ 2008 (hereinafter referred to as VC2008), create a C ++ project, File-> New Project-> Empty Project, and enter the project Name in the Name column, then OK.

2. Set the DLL file to be generated, right-click the project name, and choose Properties> Configuration Properties> General> Configuration Type> Dynamic Library (. dll)

3. Set C/C ++ and Lua. C/C ++-> General-> Additional Include Directories-> select include in the installation path of Lua. I installed Lua in "C: \ Program Files \ Lua ", my complete path: "C: \ Program Files \ Lua \ 5.1 \ include ".

4. Set Linker: Linker-> General-> Additional Library Directories-> select the lib directory under the Lua installation directory. My complete path: "C: \ Program Files \ Lua \ 5.1 \ lib ".

5. Set Input: Input-> Additional Dependencies-> here, manually enter "lua5.1.lib. Click OK.

OK. Now you can configure the module definition file. Wait.

Test Lua calls the C ++ Function

1. Select Source Files, right-click Add-> New Item-> select C ++ File (. cpp)-> Add-> Name, and enter hello-> Add.

2. Enter the following Code in the hello. cpp file.

 
 
  1. // Dependent File
  2.  
  3. // Because the C ++ Project is created, extern "C" must be added here. If this is not added, it cannot be compiled.
  4.  
  5. Extern "C "{
  6. # Include "lua. h"
  7. # Include "lualib. h"
  8. # Include "lauxlib. h"
  9. }
  10. # Include <windows. h>
  11. # Include <wincrypt. h>
  12. // Because the C ++ Project is created, extern "C" must be added here"
  13. Extern "C" int hello (lua_State * L ){
  14. Printf ("------> Hi! % S \ n ", (LPTSTR) lua_tostring (L, 1 ));
  15. Return 0;
  16. }
  17.  
  18. // -------- Register the function -----------
  19. Struct luaL_reg lrLibs [] =
  20. {
  21. {"Hello", hello },
  22. {NULL, NULL}/* sentinel */
  23. };
  24. // This function is the library entry function, which must be called in Lua to register the library function list
  25. // Because Lua is written in C language, the export function must comply with the C language call specification.
  26. // Return value: the registered function library is actually a table
  27. Extern "C" int luaopen_hello (lua_State * L)
  28. {
  29. // Register a function library named hello
  30. LuaL_register (L, "hello", lrLibs );
  31. Return 1;
  32. }

3. Create a module definition file. As mentioned in Environment configuration, we will not describe how to configure it here.

1) at this time, you need to create a file under the current Project. The file suffix is. def. Here I define a file named "lua_c.def.

2) This file is very simple. First add EXPORTS to the top of the file, and then add the entry you have defined in the C ++ file.FunctionYou can add the function name. (That is, the above luaopen_hello ).

3) add the module definition file to the Project, right-click Resource Files-> Add Existing Item-> select the new lua_c.def file, and add.

4. Configure the module definition file to the Project. The environment configuration is not completed, properties-> Configuration Properties-> Linker-> Input-> Module Definition File-> enter the lua_c.def File created above, and click OK.

OK! It is basically configured here.

Compile Project:

1. Right-click Project> Build and you can view the Build information on the console.

2. In the current Project \ Debug \, you can find the hello with the same Project name. dll (here my Project Name is hello) file, this file can be used in Lua.

Lua file creation test:

1. Open the SciTE Editor, create a new file, and enter the following content in the file:

-- "Hello" is the module Name, which is the same as the Project Name.

 
 
  1. require("hello");  

-- Name registered in the C ++ File

 
 
  1. local f = hello;  

-- Call the method defined in the C ++ file. "Lua" is the parameter.

 
 
  1. f.hello("Lua");  

2. Save and get a name. This is LuaVC. lua.

3. Run. Note that you need to copy the hello. dll file to the path that Lua can find. I put it together with the Lua file.

If no problem exists, you can see the SciTE console output.

 
 
  1. Hi!Lua  

Summary:LuaCallC ++ FunctionsThe implementation of the content of the tutorial is complete, hope to learn through this article can help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.