專案中需要將c++的程式暴露給網路使用,我也不想使用c++來用網路程式設計,就想到可以使用python來解決web端,然後將 c++ 的介面暴露給 python 。於是在尋求解決方案的時候找到了 boost.python 庫。
boost 中的 python 庫支援在 python 和 c++ 之間的自由轉換,包括 c++ 到 python 的型別轉換,預設引數,關鍵字引數,引用,指標等等。boost.python 庫可以讓 python 輕易地呼叫 c++ 編寫的模組,也可以很容易地在 c++ 中呼叫 python 。
c++ 檔案中需要包含標頭檔案
#include
using
namespace
boost
::python
;
還需要在鏈結的時候,加入-lboost_python
。
以下操作借助linux下g++完成,windows可能需要借助其他工具,匯出c++函式具體實現:
hello.cpp
char
const
*greet
()#include
boost_python_module
(hello
)// python 模組開始
makefile
python_version
=2.7
python_include
=/usr/
include
/python$
(python_version
)# location of the boost python include files and library
# boost_inc
=/usr/
local
/include
boost_lib
=/usr/
local
/lib
# compile mesh classes
target
=hello$(
target
).so:$
(target).o
g++-
shared $
(target
).o -l$
(boost_lib)-
lboost_python -l
/usr
/lib
/python$
(python_version
)/config
-lpython$
(python_version)-
o $(
target
).so$(
target).o
:$(target
).cpp
g++-
i$(python_include)-
i$(boost_inc)-
fpic
-c $
(target
).cpp
clean
:rm
*.so
*.o
hello.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
hello
print
hello
.greet
()
輸出: hello, world
注意將 python, boost 所在目錄 include 進去,在鏈結時加上-lboost_python -lpython2.7
並 include python 和 boost目錄。
demo 位址gitlab :
linux下安裝boost python詳解
boost.python的安裝 安裝boost boost.python的測試 boost.python 是boost庫的乙個子功能,可以實現python和c c 的無縫銜接。相比於cpython和cython來說boost.python在使用上會更加方便。關於boost.python的詳細內容可參...
ubuntu安裝postgresql和使用方法
安裝元件 客戶端安裝 sudo apt get install postgresql client 伺服器安裝 複製 如下 sudo apt get install postgresql postgresql contrib 獲取installing postgis,procedural langu...
PLSQL Developer安裝配置和使用
1 安裝 先將plsql developer 以及instantclient 11 2 複製到指定目錄解壓 2 在instantclient 11 2目錄下新建network admin tnsnames.ora檔案,並做以下配置 myoracle description address proto...