C 擴充套件python ,簡單例子,實現加法

2021-09-28 22:42:32 字數 4216 閱讀 8290

首先編寫lgadd_model.cpp

#define py_ssize_t_clean

#include "python.h"

/*由於 python 可能會定義一些能在某些系統上影響標準標頭檔案的預處理器定義,

因此在包含任何標準標頭檔案之前,你 必須 先包含 python.h。

*/// c++ 實現加法,傳入倆個int型,輸出和.

int add(int a, int b);

/*模組中每個可供python呼叫的函式都需要乙個對應的包裹函式*/

static pyobject *lg_add(pyobject * self, pyobject *args)

return (pyobject*)py_buildvalue("i",add(a,b));

/* 這裡返回的值,必須這麼寫,返回的是乙個python物件,而不能直接是c物件.*/

};// 定義乙個方法表,方法表中可以包含多個方法

static pymethoddef lgmethods = ,

/* sentinel */

};// 定義模組結構 引用方法表

static struct pymoduledef lgmodule = ;

/*模組初始化函式必須命名為pyinit_name(),其中name是模組的名字,並應該定義為非static.

模組結構必須傳遞給直譯器的模組初始化函式*/

pymodinit_func

pyinit_lg(void)

/*需要特別注意 檔案各方法名稱的對應關係,必須完全對應*/

編寫 setup.py檔案

from distutils.core import setup,extension

mod = 'lg' # 模組名

setup(name=mod,ext_modules=[extension(mod,sources=['lgadd_model.cpp'])])

執行進行編譯

python setup.py build
輸出:

running build

running build_ext

building 'lg' extension

gcc -pthread -b /home/***/anaconda3/compiler_compat -wl,--sysroot=/ -wsign-compare -dndebug -g -fwrapv -o3 -wall -wstrict-prototypes -fpic -i/home/***/anaconda3/include/python3.7m -c cal_ids.cpp -o build/temp.linux-x86_64-3.7/cal_ids.o

cc1plus: warning: command line option 『-wstrict-prototypes』 is valid for c/objc but not for c++

creating build/lib.linux-x86_64-3.7

g++ -pthread -shared -b /home/***/anaconda3/compiler_compat -l/home/***/anaconda3/lib -wl,-rpath=/home/***/anaconda3/lib -wl,--no-as-needed -wl,--sysroot=/ build/temp.linux-x86_64-3.7/cal_ids.o -o build/lib.linux-x86_64-3.7/lg.cpython-37m-x86_64-linux-gnu.so

執行進行安裝

python setup.py install
輸出:

running install

running bdist_egg

running egg_info

creating lg.egg-info

writing lg.egg-info/pkg-info

writing dependency_links to lg.egg-info/dependency_links.txt

writing top-level names to lg.egg-info/top_level.txt

writing manifest file 'lg.egg-info/sources.txt'

reading manifest file 'lg.egg-info/sources.txt'

writing manifest file 'lg.egg-info/sources.txt'

installing library code to build/bdist.linux-x86_64/egg

running install_lib

running build_ext

creating build/bdist.linux-x86_64

creating build/bdist.linux-x86_64/egg

copying build/lib.linux-x86_64-3.7/lg.cpython-37m-x86_64-linux-gnu.so -> build/bdist.linux-x86_64/egg

creating stub loader for lg.cpython-37m-x86_64-linux-gnu.so

byte-compiling build/bdist.linux-x86_64/egg/lg.py to lg.cpython-37.pyc

creating build/bdist.linux-x86_64/egg/egg-info

copying lg.egg-info/pkg-info -> build/bdist.linux-x86_64/egg/egg-info

copying lg.egg-info/sources.txt -> build/bdist.linux-x86_64/egg/egg-info

copying lg.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/egg-info

copying lg.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/egg-info

writing build/bdist.linux-x86_64/egg/egg-info/native_libs.txt

zip_safe flag not set; analyzing archive contents...

__pycache__.lg.cpython-37: module references __file__

creating dist

creating 'dist/lg-0.0.0-py3.7-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it

removing 'build/bdist.linux-x86_64/egg' (and everything under it)

processing lg-0.0.0-py3.7-linux-x86_64.egg

creating /home/***/anaconda3/lib/python3.7/site-packages/lg-0.0.0-py3.7-linux-x86_64.egg

extracting lg-0.0.0-py3.7-linux-x86_64.egg to /home/***/anaconda3/lib/python3.7/site-packages

adding lg 0.0.0 to easy-install.pth file

installed /home/***/anaconda3/lib/python3.7/site-packages/lg-0.0.0-py3.7-linux-x86_64.egg

processing dependencies for lg==0.0.0

finished processing dependencies for lg==0.0.0

最終在python檔案中就可以呼叫

import lg

a = 1

b =2

print(lg.add(a,b))

在呼叫時, 雖然lg模組 顯示no module named lg

但是可以正常使用

簡單登入註冊例子實現redux功能

以登入存貯賬號密碼為例 actions部分 actions login.js export function updateaccount value export function updatepsw value reducers部分 reducers login.js const statedat...

SElinux 學習筆記 例子實現

測試環境介紹 作業系統 fedora 22 mate fedora 21 環境搭建 root yum install y policycoreutils python policycoreutils devel selinux policy devel setools hddtemp git vim...

通過KNN例子,實現Sklearn入門

pip 安裝 安裝 scikit learn sklearn 最簡單的方法就是使用 pip 安裝它.首先確認自己電腦中有安裝 python 2.6 或 3.3 版本 numpy 1.6.1 scipy 0.9 然後開啟cmd視窗,使用如下命令安裝 pip3 install u scikit lear...