C + + calls Python

Source: Internet
Author: User

1, download boost1.52,http://www.boost.org/, unzip the file to D:\boost\boost_1_52_0;

2, download python2.7.3,http://www.python.org/; (boost1.4 support to python2.5)

3, install Python, I installed in the D:\PYTHON25, set the environment variable path D:\PYTHON25;

4. Starting with the program->microsoft Visual Studio 2010->visual Studio tools->visual Studio Command Prompt (2010), open a console

5. In the console, enter the following 3 lines in turn:

CD D:\boost\boost_1_49_0
Bootstrap.bat
Bjam stage–toolset=msvc-9.0–with-python–stagedir= "D:\boost_1_49_0\bin\vc9" Link=static runtime-link=shared Runtime-link=static Threading=multi Debug Release

6, the project properties are configured as follows
Add additional Library directories to the linker, python/libs (in Python's installation directory), Boost/vs2010/lib (in the generated boost directory)

Add-on Add-on library directory for C/C + +, boost (boost download directory), python/include (Python installation directory)

1. Confirm that the project properties are configured as follows
Add additional Library directories to the linker, python/libs (in Python's installation directory), Boost/vs2010/lib (in the generated boost directory)
Add-on Add-on library directory for C/C + +, boost (boost download directory), python/include (Python installation directory)

2, if the use of Boost.python Static library: In the properties-----preprocessor-----preprocessor definition add boost_python_static_lib, otherwise compiled as dynamic, will be prompted to find python_boost*.lib or something

C + + Programs

//Python_test.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include <iostream>#include <boost/python.hpp>using namespace STD;using namespaceBoost::p Ython;int_tmain (intARGC, _tchar* argv[]) {py_initialize ();//InitializeObject main_module = Import ("__main__"); Object main_namespace = Main_module.attr ("__dict__");//Execute multiple Python statements: Create a hello.txt text filestr Lines ="Hello = File (' Hello.txt ', ' W ') \ n"  "Hello.write (' Hello World First python! ') \ n "  "Hello.close ()"; EXEC (Lines, main_namespace);//execute an expressionexec"result = 5 * * 2", main_namespace);//Extract and view the value of the variable result intfive_squared = extract<int> (main_namespace["Result"]);//View the value of the variable result cout<<"The five_squeared caculated by Python"<< five_squared << Endl;//Load sys module.Object sys = import ("SYS");//Extract version information for Python STD::stringVersion = extract<STD::string> (Sys.attr ("Version"));STD::cout<< version <<STD:: Endl;//Require simple.py with executable files under the same path! Run OKstr filename ="simple.py"; Object simple = exec_file (filename, main_namespace, main_namespace); Object foo = main_namespace["foo"];intval = extract<int> (foo (5));cout<<"Python has caculated foo as"<< Val << Endl; Py_finalize ();cout<<"My Python SUCCESS"<<endl; System"Pause");return 0;}

Create a simple.py python file in the current directory with the following contents:

def foo(i = 4):      return i**3  

(The sample program originates from the network)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + calls Python

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.