C ++ idlcpp tutorial Python (2), idlcpppython

Source: Internet
Author: User
Tags one more line

C ++ idlcpp tutorial Python (2), idlcpppython

In the last tutorial on idlcpp in C ++ mixed programming (I), I introduced the use of idlcpp. Now we will explain the sample tutorial in idlcpp. Here is an example of the Python language. First, let's take a look at the first example program PythonTutorial0. Like the first example in many languages, it is a program that prints Hello world. Use Visual Studio 2015 to open the solution file tutorials \ PythonTutorials. sln. There are already multiple project files:

// Tutorial $ # include <stdio. h> namespace tutorial {struct Test {static void Run () ;}; $ * inline void Test: Run () {printf ("Hello World! ");} * $}

Similar to C ++ code. Compile Tutorial0. I and generate four files: Tutorial0.h, Tutorial0.mh, Tutorial0.ic, and Tutorial0.mc. The content of Tutorial0.h is as follows:

//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include <stdio.h>#line 5 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"namespace tutorial#line 6 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"{#line 7 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"    struct Test#line 8 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"    {    public:#line 9 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"        static void Run();#line 10 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"    };    inline void Test::Run()    {        printf("Hello World!");    }    #line 17 "D:/GitHub/idlcpp/tutorials/Common/Tutorial0.i"}

Because in the compilation. the-ld option is specified when the I file is created. there are many # line commands in the hfile, which can be located during the next C ++ compilation. i, instead of locating. h. Modify the compilation option, remove the-ld option, and re-compile. The result is as follows:

//DO NOT EDIT THIS FILE, it is generated by idlcpp//http://www.idlcpp.org#pragma once#include <stdio.h>namespace tutorial{    struct Test    {    public:        static void Run();    };    inline void Test::Run()    {        printf("Hello World!");    }    }

This looks refreshing. Please compare it with the above Tutorial0. I content. Basically, the content is similar. The first line is as follows:

// Tutorial

This is a comment. Like C ++, idlcpp uses // to represent a single line comment and/**/to represent a comment.

$ # Include <stdio. h>

Idlcpp only analyzes the declaration of the interface, and other content will usually appear in the C ++ header file. Here, idlcpp provides a method to copy Part Of The. I file directly to. h. There are three methods

This line indicates copying # include <stdio. h> to. h. The following printf uses this header file.

 

Namespace tutorial and corresponding {}.

The concepts in namespace and C ++ are the same and will be output to. h as is.

 

Struct Test and corresponding {};.

This is similar to the concept in C ++ and will be output to. h as is.

 

The following. h file contains one more line

Public:

The data members and member functions declared in idlcpp are regarded as public, so this line is left empty.

Static void Run ();

The two sides of this line are the same. Declare a static member function.

$ *

Inline void Test: Run ()

{

Printf ("Hello World! ");

}

* $

 

As described above, idlcpp copies content between $ * and * $ to. h. Because idlcpp only processes the function declaration and cannot process its implementation code, it is impossible to put the modern code in the declaration of the class like C ++ and can only be written outside. To write a. cpp file less, you can write it in the header file using inline functions.

The Tutorial0.ic file does not contain any substantive content.

The Tutorial0.mh and Tutorial0.mc files are used to construct the corresponding metadata information. The specific content involves too much and is not explained for the time being.

Let's take a look at the content of PythonTutorial0.cpp.

#include <tchar.h>#include <windows.h>#include "Python.h"#include "../../../paf/src/pafpython/PythonWrapper.h"#include "../../Common/Tutorial0.h"#include "../../Common/Tutorial0.mh"#include "../../Common/Tutorial0.ic"#include "../../Common/Tutorial0.mc"#if defined(_DEBUG)#pragma comment(lib,"pafcore_d.lib")#pragma comment(lib,"pafpython_d.lib")#else#pragma comment(lib,"pafcore.lib")#pragma comment(lib,"pafpython.lib")#endifint _tmain(int argc, _TCHAR* argv[]){    const char* path = "tutorial0";    PyImport_AppendInittab("pafpython", &PyInit_PafPython);    Py_Initialize();    PyObject* pName = PyUnicode_FromString(path);    PyObject* pModule = PyImport_Import(pName);    Py_DECREF(pName);    if(pModule != NULL)    {        Py_DECREF(pModule);    }    else    {        PyErr_Print();        fprintf(stderr, "Failed to load \"%s\"\n", path);    }    Py_Finalize();     return 0;}

# Include "Python. h"

This line introduces the Python header file

 

# Include ".../paf/src/pafpython/PythonWrapper. h"

This line introduces the Python Plug-In header file

 

# Include ".../../Common/Tutorial0.h"

# Include ".../Common/Tutorial0.mh"

# Include ".../../Common/Tutorial0.ic"

# Include ".../../Common/Tutorial0.mc"

These lines are included by the Tutorial0. I compilation results. After compilation, the corresponding metadata information is registered to the system, so that the footstep language can be accessed.

The main () function is a basic process for running a Python script. Where

PyImport_AppendInittab ("pafpython", & PyInit_PafPython );

This line loads the plug-in Python.

Finally, let's take a look at the content of the tutorial0.py file.

import pafpython;paf = pafpython.paf;paf.tutorial.Test.Run();

The final Code indicates that: tutorial: Test: Run () in C ++ is called ();

All data types generated by idlcpp are under the name of paf. It can be understood that the name pafpython. paf in Python is equivalent to the global namespace in C ++. In C ++, the full name of the Run function can be considered as: tutorial: Test: Run. In lua, It is pafpython. paf. tutorial. Test. Run. After the link is compiled, the execution result is as follows:

Python correctly calls the functions in C ++.

Related Article

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.