1. 程式人生 > 其它 >boost python編譯教程

boost python編譯教程

技術標籤:pythonC++boostc++

官網下載boost:https://www.boost.org/

  • 開啟VS2015 Developer Command Promp(VS2015開發人員命令提示)t命令列
  • 定位到Boost解壓後的資料夾:cd /d J:\Library\boost\boost_1_75_0
  • 在命令列執行booststrap.bat,將在資料夾下生成b2.exe和project-config.jam等檔案
  • 新建user-config.jam檔案
# Configure specific Python version.---user for boost.python---2018.10.12---
# using python : 3.6.1
 : D:/Anaconda3/envs/lanenet/python.exe
 : D:/Anaconda3/envs/lanenet/include #directory that contains pyconfig.h
 : D:/Anaconda3/envs/lanenet/libs    #directory that contains python361.lib
 : <toolset>msvc ;
  • b2 toolset=msvc-14.0 --with-python threading=multi link=static address-model=64 variant=debug --user-config=user-config.jam
  • b2 toolset=msvc-14.0 --with-python threading=multi link=static address-model=64 variant=release --user-config=user-config.jam

  • 得到如下(其中將xxx-gd-xxx的複製一份並把前面lib去掉)

  • vs專案配置引數
  • boost_debug.props
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup>
    <IncludePath>J:\Library\boost\boost_1_75_0;D:\Anaconda3\envs\lanenet\include;$(IncludePath)</IncludePath>
    <LibraryPath>J:\Library\boost\boost_1_75_0\stage\lib;D:\Anaconda3\envs\lanenet\libs;$(LibraryPath)</LibraryPath>
  </PropertyGroup>
  <ItemDefinitionGroup>
    <Link>
      <AdditionalDependencies>python3.lib;python36.lib;libboost_numpy36-vc140-mt-gd-x64-1_75.lib;libboost_python36-vc140-mt-gd-x64-1_75.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
    <ClCompile />
    <ClCompile />
  </ItemDefinitionGroup>
  <ItemGroup />
</Project>
  • boost_release.props
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros" />
  <PropertyGroup>
    <IncludePath>J:\Library\boost\boost_1_75_0;D:\Anaconda3\envs\lanenet\include;$(IncludePath)</IncludePath>
    <LibraryPath>J:\Library\boost\boost_1_75_0\stage\lib;D:\Anaconda3\envs\lanenet\libs;$(LibraryPath)</LibraryPath>
  </PropertyGroup>
  <ItemDefinitionGroup>
    <Link>
      <AdditionalDependencies>python3.lib;python36.lib;libboost_numpy36-vc140-mt-x64-1_75.lib;libboost_python36-vc140-mt-x64-1_75.lib;%(AdditionalDependencies)</AdditionalDependencies>
    </Link>
    <ClCompile />
    <ClCompile />
    <ClCompile>
      <PreprocessorDefinitions>BOOST_PYTHON_STATIC_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
    </ClCompile>
  </ItemDefinitionGroup>
  <ItemGroup />
</Project>
  • 測試
    //#include<iostream>
    //#include<stdio.h>
    //
    //#include<boost\version.hpp> //包含boost標頭檔案
    //#include<boost\config.hpp>
    //
    //int main()
    //{
    //	using namespace std;
    //	cout << BOOST_VERSION << endl;
    //	cout << BOOST_LIB_VERSION << endl;
    //	cout << BOOST_PLATFORM << endl;
    //	cout << BOOST_COMPILER << endl;
    //	cout << BOOST_STDLIB << endl;
    //
    //	system("pause");
    //	return 0;
    //}
    
    //#define BOOST_PYTHON_STATIC_LIB
    #include <boost/python.hpp>
    #include <iostream>
    #include <stdio.h>
    using namespace boost::python;
    
    char const* greet()
    {
    	return "hello, world";
    }
    
    
    BOOST_PYTHON_MODULE(boostpy03)			// Python 模組開始
    {
    	using namespace boost::python;		// 開啟名稱空間
    	def("greet", greet);
    }

    生成boostpy03.dll

  • 修改為boostpy03.pyd

  • 進入python環境,執行python

    import boostpy03
    print(boostpy03.greet())

    輸出:hello, world

  • 成功