Idlcpp Tutorials for C + + mixed programming Lua (2)

Source: Internet
Author: User

The use of the Idlcpp tool is described in the previous Idlcpp tutorial (i) of C + + hybrid programming. Here's an example of the Lua language that is now being explained in the sample tutorial Idlcpp. First Look at the first example program LuaTutorial0. Like the first example in many languages, it's a program that prints Hello world. Open Solution file Tutorials\luatutorials\luatutorials.sln with Visual Studio 2015, which already has multiple project files.

in the engineering LuaTutorial0, three files have been added, namely LuaTutorial0.cpp, tutorial0.i, Tutorial0.lua. First Look at the tutorial0.i content as follows:

// Tutorial  <stdio.h>namespace  tutorial{    struct  Test    {          staticvoid  Run ();    };    $*    void  test::run ()    {        printf ("Hello world! " );    }     *$}

looks similar to C + + code. Compiling the tutorial0.i will generate a TUTORIAL0.H,TUTORIAL0.MH,TUTORIAL0.IC,TUTORIAL0.MC of four files. The contents of Tutorial0.h are as follows:

//Do not EDIT the This FILE, it's generated by Idlcpp//http://www.idlcpp.org#pragmaOnce#include<stdio.h>#line4 "d:/github/idlcpp/tutorials/common/tutorial0.i"namespaceTutorial#line5 "d:/github/idlcpp/tutorials/common/tutorial0.i"{#line6 "d:/github/idlcpp/tutorials/common/tutorial0.i"structTest#line7 "D:/GITHUB/IDLCPP/TUTORIALS/COMMON/TUTORIAL0.I"    {     Public:#line8 "D:/GITHUB/IDLCPP/TUTORIALS/COMMON/TUTORIAL0.I"Static voidRun ();#line9 "d:/github/idlcpp/tutorials/common/tutorial0.i"    }; InlinevoidTest::run () {printf ("Hello world!"); }    #line"D:/github/idlcpp/tutorials/common/tutorial0.i"}

Because the-LD option was specified when compiling the. i file, there are many #line directives in the generated. h file, which is for the next C + + compile time to locate the error in the. I location instead of the. h. Modify the compilation options, remove the-LD option, recompile, and get the following results:

//Do not EDIT the This FILE, it's generated by Idlcpp//http://www.idlcpp.org#pragmaOnce#include<stdio.h>namespacetutorial{structTest { Public:        Static voidRun ();    }; InlinevoidTest::run () {printf ("Hello world!"); }    }

This looks more refreshing, please compare with the above tutorial0.i content, basically the content is similar. Here is a detailed explanation, first of all, the first line

Tutorial

This is a comment, idlcpp and C + + with//represents a single line of comments, and/**/represents a piece of comment.

$$ #include <stdio.h>

Idlcpp only parses the declaration of an interface, and other content typically appears in the C + + header file. Here Idlcpp provides a way to copy parts of the. i file directly to the. h method, a total of three kinds

    1. $$, which means that a subsequent entire row is copied to the corresponding location in. h, similar to the//comment line in C + +.
    2. $* and *$, the contents between the two symbols are copied to the corresponding location in. h, similar to/* in C + + and a large chunk of the comment.
    3. $, which indicates that a subsequent identifier or integer is copied to the appropriate location.

This line represents the copy of # include <stdio.h> to. h, and the following printf uses this header file.

namespace Tutorial and the corresponding {}.

The concepts in namespace and C + + are identical and are exported as-is to. h.

struct Test and the corresponding {};.

This is similar to the concept in C + + and is exported as-is to. h.

One more line in the. h file below

Public

Data members and member functions declared in Idlcpp are considered public, so there is no brain added to this line.

static void Run (); This line is also the same on both sides, declaring a static member function.

$*

inline void Test::run ()

{

printf ("Hello world!");

}

*$

As described above, idlcpp copies the contents between $* and *$ into. H. Because Idlcpp only handles function declarations and cannot process its implementation code, it cannot put its implementation code in the declaration of a class like C + + and can only be written out. Here in order to write less a. cpp file, it is written in the header file in the form of an inline function.

There is no substantive content in document Tutorial0.ic.

Files Tutorial0.mh and TUTORIAL0.MC are used to construct corresponding metadata information, which involves too much and does not explain for the time being.

Take a look at the contents of LuaTutorial0.cpp.

#include <tchar.h>#include<string>#include<windows.h>#include"lua.hpp"#include".. /.. /.. /paf/src/paflua/luawrapper.h"#include".. /.. /common/tutorial0.h"#include".. /.. /COMMON/TUTORIAL0.MH"#include".. /.. /common/tutorial0.ic"#include".. /.. /COMMON/TUTORIAL0.MC"#ifDefined (_DEBUG)#pragmaComment (lib, "Pafcore_d.lib")#pragmaComment (lib, "Paflua_d.lib")#pragmaComment (lib, "Lua53_d.lib")#else#pragmaComment (lib, "Pafcore.lib")#pragmaComment (lib, "Paflua.lib")#pragmaComment (lib, "Lua53.lib")#endifvoidGetexepath (std::string&path) {    CharFilename[max_path]; GetModuleFileName (0, FileName,sizeof(FileName)); Const Char* end = _TCSRCHR (FileName,'\\'); Path.assign (fileName, End+1);}int_tmain (intARGC, _tchar*argv[]) {    interror; Lua_state*l =lual_newstate ();    Lual_openlibs (L);    Luaopen_paflua (L); STD::stringpath;    Getexepath (path); Path+="Tutorial0.lua"; Error= Lual_loadfile (L, Path.c_str ()) | | Lua_pcall (L,0,0,0); if(Error) {fprintf (stderr,"%s\n", Lua_tostring (L,-1)); Lua_pop (L,1);    } lua_close (L); return 0;}

#include "lua.hpp"

This line introduces the LUA header file

#include ". /.. /.. /paf/src/paflua/luawrapper.h "

This line introduces the LUA plug-in header file

#include ". /.. /common/tutorial0.h "

#include ". /.. /COMMON/TUTORIAL0.MH "

#include ". /.. /common/tutorial0.ic "

#include ". /.. /COMMON/TUTORIAL0.MC "

These lines are included in the results compiled by tutorial0.i so that the corresponding metadata information is registered with the system so that it can be accessed by the Footstep language.

the main () function is a basic process of running a LUA footstep. Which

Luaopen_paflua (L);

This line loads the plug-in in the Lua virtual machine.

Finally, check out the contents of the Tutorial0.lua file

Paf.tutorial.Test.Run ();

This code represents the call in C + +:: Tutorial::test::run ();

All data types generated by Idlcpp are under the PAF name and can be understood as the name PAF in LUA equivalent to the global namespace in C + +. In C + +, the full name of the Run function can be thought of as: Tutorial::test::run, which is paf.tutorial.Test.Run in Lua.

After the link is compiled, the results are as follows:

You can see that LUA correctly calls functions in C + +.

Idlcpp Tutorials for C + + mixed programming Lua (2)

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.