12.06.2017       Выпуск 182 (12.06.2017 - 18.06.2017)       Статьи

Python и C ++ с использованием Boost.Python

Читать>>



Экспериментальная функция:

Ниже вы видите текст статьи по ссылке. По нему можно быстро понять ссылка достойна прочтения или нет

Просим обратить внимание, что текст по ссылке и здесь может не совпадать.

Python and C++ Interoperability using Boost.Pythonwith Python 3.6.0,Visual Studio 2015, and Boost 1.63.0

Boost.Python is a part library of Boost which is used for integrating between Python and C++, using your C++  Compiler and IDE.

There are two methods to integrate between Python and C++ with Boost.Python:

 

  • Extend: meaning, you can take a C++ library (DLL/LIB), wrap it using Boost.Python framework (without changing the original code) and use it from Python as a regular import of extension module.
  • Embed: meaning, you can load and run a Python script (py file) to your C++ program and also use it’s output values inside your C++ code. Also you can evaluate inline python expressions/statements (strings) and receive the output to your C++ code as well.

 

In this tutorial you will learn all the technical steps that you need to make, in order to setup your Visual Studio environment for build and run Python code that uses your C++ dll library and a C++ code which uses a python code.

 

Step 1 – Install Python 3.6

Check the Environment PATH variable.
Just try to run python from a command prompt window, you should see :

 

If not, make sure that the Python root folder exists on your system PATH environment variable:

C:\Users\{Your User}\AppData\Local\Programs\Python\Python36-32

and it’s not conflicts with other installed version of python

* If you already have boost installed and working with your visual studio environment, you can jump to step 4

 

Step 2 – Installing Boost 1.63

  1. Download the Boost library from : http://www.boost.org/doc/libs/1_63_0/more/getting_started/windows.html
  2. Extract the downloaded zip file to C:\Program Files\boost folder (or somewhere else that you prefer)
  3. Build the library: (Actually, most of the boost library not have to be built, because it is based on header files, but the Boost.Python library must be build, so let’s build them all)
    1. Open a Command Prompt window. Note: Run cmd as Administrator
    2. cd to the your boost folder and run :
      1. bootstrap.bat
      2. b2 (takes a while…)

after : bootstrap                                                                after: b2

  

Step 3 – Test the Boost library from Visual Studio 2015

 

  1. Run Visual Studio and select New->Project from the file menu.
  2. Select Visual C++ -> Win32 -> Win32 Console Application
  3. Enter “TestBoost” in the name field.
  4. In the Application Setting dialog box, choose empty project and hit finish
  5. In the Solution Explorer pane right click the source files folder and add new item -> C++ File -> cpp
  6. First Test “Header file only library” Paste the following code:
    #include <string>
    #include <iostream>
    #include <boost/foreach.hpp>
    int main()
    {
      std::string str("Hello Boost!\n");
      BOOST_FOREACH(char ch, str)
      {
        std::cout << ch;
      }
      return 0;
    }
    
  7. This code should fail at compilation time , you must add the boost library include folder to the project settings, by:
    Right click the TestBoost project in the solution explorer pane -> properties -> Configuration Properties -> C/C++ -> General -> Additional Include Directories . Enter the path of your boost root folder e.g.: C:\Program Files\boost\boost_1_63_0
  8. Build and run, you should get “Hello Boost!” on the screen.
  9. Add some code that uses the boost built library, paste the following code just before the main function:
    #include <boost/regex.hpp>
    bool ValidateCreditCard(const std::string& str)
    {
      static const boost::regex creditRegEx("(\\d{4}[- ]){3}\\d{4}");
      return regex_match(str, creditRegEx);
    }
    
  10. Change the main function body code with the following one:
std::cout << (ValidateCreditCard("1234-1234-1234-1234")?"Valid, ":"Not valid, ")
          << (ValidateCreditCard("1234-123-1234-1234")?"Valid":"Not valid") << std::endl;

 

This code should fail on link time, you must add the boost binaries folders to the project settings, by:Right click the TestBoost project in the solution explorer pane -> properties -> Configuration Properties -> Linker -> General -> Additional Library Directories. Add the following paths: C:\Program Files\boost\boost_1_63_0\stage\lib and C:\Program Files\boost\boost_1_63_0

11.Build and run, you should get “Valid, Not valid” on the screen. That’s it, boost is      working!

Step 4 – Building a C++ DLL as a Python extension module

 

  1. In the Solution Explorer pane right click the source files folder and add new item -> C++ File -> cpp
  2. In the Application Setting dialog box, choose DLL as application type and check the empty project option , then hit finish.
  3. Enter “HelloExt” in the name field.
  4. Select Visual C++ -> Win32 -> Win32 Console Application
  5. Run Visual Studio and select New->Project from the file menu.
  6. Right click the HelloExt project in the solution explorer pane -> properties -> Configuration Properties -> C/C++ -> General -> Additional Include Directories . Add the path of your boost root folder e.g.: C:\Program Files\boost\boost_1_63_0 and also to your Python include folder e.g. C:\Users\{your user}\AppData\Local\Programs\Python\Python36-32\include
  7. Right click the HelloExt project in the solution explorer pane -> properties -> Configuration Properties -> Linker -> General -> Additional Library Directories. Add the following paths: C:\Program Files\boost\boost_1_63_0\stage\lib
    , C:\Program Files\boost\boost_1_63_0 and C:\Users\{your user}\AppData\Local\Programs\Python\Python36-32\libs
  8. Paste the following code to your Source.cpp file
    #define BOOST_PYTHON_STATIC_LIB
    #include <boost/python.hpp>
    
    char const* SayHello()
    {
    	return "Hello, from c++ dll!";
    }
    BOOST_PYTHON_MODULE(HelloExt)
    {
    	using namespace boost::python;
    	def("SayHello", SayHello);
    }
    
  9. Build the project, it should build with no warnings/errors.
  10. Open a File explorer window, and navigate to your {Project Folder}\Debug. Change the name of the file: HelloExt.dll to HelloExt.pyd
  11. Open a text editor and paste the following Python script:
    import HelloExt
    print(HelloExt.SayHello())
    
  12. Save this file in your {Project Folder}\Debug as TestHelloExt.py
  13. Open a command prompt window and cd to your {Project Folder}\Debug folder
  14. Run the script with the command : python TestHelloExt.py
  15. You should get the following output:
  16. That’s it, you’re done!

 

Step 5 – Integrate your C++ project with Python code.

 

  1. Run Visual Studio and select New->Project from the file menu.
  2. Select Visual C++ -> Win32 -> Win32 Console Application
  3. Enter “TestPythonInvoke” in the name field.
  4. In the Application Setting dialog box check the empty project option, then hit finish.
  5. In the Solution Explorer pane right click the source files folder and add new item -> C++ File -> cpp
  6. From Step 4 above repeat steps 6) and 7)
  7. Create a new text file name it hello.py , enter the following text:
print ('Hello from python py file')

      and save it in your project folder.

       8.Paste the following code inside your Source.cpp file.

#define BOOST_PYTHON_STATIC_LIB
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/python.hpp>
using namespace boost::python;
int main()
{
	try {
	Py_Initialize();
	object _mainModule = import("__main__");
	object _mainNamespace = _mainModule.attr("__dict__");
	// Testing python experssion evaluation
	object _evalResult = eval("2 ** 8");
	int _result = extract<int>(_evalResult);
	std::cout << "2**8 in Python is:" << _result << std::endl;
	//Testing executing python source code from str
	exec("print (\"Hello from python string code \")", _mainNamespace, _mainNamespace);
	//Testing executing a python script file
	exec_file("hello.py", _mainNamespace,_mainNamespace);
	}
	catch (error_already_set) {
		PyErr_Print();
	}
}

        9.Build and run your project , you should get:

       10.That’s it, you’re done!

 



Лучшая Python рассылка

Нас поддерживает


Python Software Foundation



Разместим вашу рекламу

Пиши: mail@pythondigest.ru

Нашли опечатку?

Выделите фрагмент и отправьте нажатием Ctrl+Enter.

Система Orphus