欢迎关注我的公众号「测试游记」
pip install https://github.com/nanoric/c2py/archive/master.zip
> c2py generate --help
Usage: c2py generate [OPTIONS] MODULE_NAME [FILES]...
Converts C/C++ .h files into python module source files. All matching is
based on c++ qualified name, using regex.
Options:
-e, --encoding TEXT encoding of input files, default is utf-8
-I, --include-path TEXT additional include paths
-A, --additional-include TEXT additional include files. These files will
be included in output cxx file, but skipped
by parser.
-ew, --string-encoding-windows TEXT
encoding used to get & set string. This
value is used to construct std::locale. use
`locale -a` to show all the locates
supported. default is utf-8, which is the
internal encoding used by pybind11.
-el, --string-encoding-linux TEXT
encoding used to get & set string. This
value is used to construct std::locale. use
`locale -a` to show all the locates
supported. default is utf-8, which is the
internal encoding used by pybind11.
-i, --ignore-pattern TEXT ignore symbols matched
--no-callback-pattern TEXT disable generation of callback for functions
matched (for some virtual method used as
undocumented API)
--no-transform-pattern TEXT disable applying transforms(changing its
signature) into functions matched (for some
virtual method used as callback only)
--inout-arg-pattern TEXT make symbol(arguments only) as input_output
--output-arg-pattern TEXT make symbol(arguments only) as output only
--m2c / --no-m2c treat const macros as global variable
--ignore-underline-prefixed / --no-ignore-underline-prefixed
ignore global variables starts with
underline
--ignore-unsupported / --no-ignore-unsupported
ignore functions that has unsupported
argument
--inject-symbol-name / --no-inject-symbol-name
Add comment to describe every generated
symbol's name
-o, --output-dir PATH module source output directory
-p, --pyi-output-dir PATH pyi files output directory
--clear-output-dir / --no-clear-output-dir
--clear-pyi-output-dir / --no-clear-pyi-output-dir
--copy-c2py-includes TEXT copy all c2py include files, excluding input
files to specific dir.
-m, --max-lines-per-file INTEGER RANGE
--generate-setup TEXT if set, generate setup.py into this location
--setup-lib-dir TEXT
--setup-lib TEXT
--setup-use-patches / --setup-no-use-patches
--enforce-version TEXT Check if c2py version matches. If not match,
print error and exit. Use this to prevent
generating code from incompatible version of
c2py.
--help Show this message and exit.
Converts C/C++ .h files into python module source files. All matching is based on c++ qualified name, using regex.
转变 C/C++的 .h
文件为python模块源文件,所有匹配都基于c++限定名,使用正则表达式。
-e, --encoding TEXT
encoding of input files, default is utf-8
-e
:文件的编码,默认 utf-8
-I, --include-path TEXT
additional include paths
-I
:其他的包括路径
-A, --additional-include TEXT
additional include files. These files will be included in output cxx file, but skipped by parser.
-A
:额外的包含文件。这些文件将包含在输出cxx文件中,但是解析器将跳过它们。
-ew, --string-encoding-windows TEXT
encoding used to get & set string. This value is used to construct std::locale. use `locale -a` to show all the locates supported. default is utf-8, which is the internal encoding used by pybind11.
-ew
:用于获取和设置windows的字符串的编码
-el, --string-encoding-linux TEXT
encoding used to get & set string. This value is used to construct std::locale. use `locale -a` to show all the locates supported. default is utf-8, which is the internal encoding used by pybind11.
-el
:用于获取和设置linux的字符串的编码
-i, --ignore-pattern TEXT
ignore symbols matched
-i
:忽略符号匹配
--no-callback-pattern TEXT
disable generation of callback for functions matched (for some virtual method used as undocumented API)
--no-callback-pattern
:禁用为匹配的函数生成回调(对于一些作为未文档化的API使用的虚拟方法)
--no-transform-pattern TEXT
disable applying transforms(changing its signature) into functions matched (for some virtual method used as callback only)
--no-transform-pattern
:禁用将转换(更改其签名)应用到匹配的函数(对于某些仅用作回调的虚拟方法)
--inout-arg-pattern TEXT
make symbol(arguments only) as input_output
--inout-arg-pattern
:将符号(仅限参数)作为input_output
--output-arg-pattern TEXT
make symbol(arguments only) as output only
--output-arg-pattern
:将符号(仅限参数)作为输出
--m2c / --no-m2c
treat const macros as global variable
--m2c/--no-m2c
:将const宏作为全局变量
--ignore-underline-prefixed / --no-ignore-underline-prefixed
ignore global variables starts with underline
--ignore-underline-prefixed/--no-ignore-underline-prefixed
:忽略以下划线开始的全局变量
--ignore-unsupported / --no-ignore-unsupported
ignore functions that has unsupported argument
--ignore-unsupported/--no-ignore-unsupported
:忽略不支持参数的函数
--inject-symbol-name / --no-inject-symbol-name
Add comment to describe every generated symbol's name
--inject-symbol-name/--no-inject-symbol-name
:添加注释来描述每个生成的符号的名称
-o, --output-dir PATH
module source output directory
-o
:模块源输出目录
-p, --pyi-output-dir PATH
pyi files output directory
-p
:pyi文件输出目录
--clear-output-dir / --no-clear-output-dir
--clear-pyi-output-dir / --no-clear-pyi-output-dir
--copy-c2py-includes TEXT
copy all c2py include files, excluding input files to specific dir.
--copy-c2py-includes
:复制所有c2py包含的文件,不包括输入文件到特定的目录。
-m, --max-lines-per-file INTEGER RANGE
--generate-setup TEXT
if set, generate setup.py into this location
--generate-setup
:如果设置,则在此位置生成setup.py
--setup-lib-dir TEXT
--setup-lib TEXT
--setup-use-patches / --setup-no-use-patches
--enforce-version TEXT
Check if c2py version matches. If not match, print error and exit. Use this to prevent generating code from incompatible version of c2py.
--enforce-version
:检查c2py版本是否匹配。如果不匹配,则打印错误并退出。使用它可以防止从不兼容的c2py版本生成代码。
--help
Show this message and exit.
环境:
Python3.7.4
Windows 64
Visual Studio 2019
https://visualstudio.microsoft.com/zh-hans/downloads
测试内容&语句
c2py/tests/generate/ctp/vnctp
$ c2py generate vnctp ThostFtdcMdApi.h ThostFtdcTraderApi.h ThostFtdcUserApiDataType.h ThostFtdcUserApiStruct.h -I vnctp/include/ --no-callback-pattern ".*Api::.*" --string-encoding-windows .936 --string-encoding-linux zh_CN.GB18030 --copy-c2py-includes vnctp/include/ --output-dir vnctp/generated_files/ --clear-output-dir --generate-setup . --setup-lib-dir vnctp/libs/ --setup-lib thostmduserapi --setup-lib thosttraderapi
期间会有各种找不到文件的报错,将 c2py
中的文件拷贝到 python/Lib/site-packages
对应路径下即可
$ /e/c2py/tests/generate/ctp/generate_ctp.sh
c2py 0.5.4
parsing ...
parsing 100.00: 2697/2697
parse finished.
processing result ...
process finished.
# of unsupported functions: 0
generating cxx code ...
cxx code generated.
generating pyi code ...
pyi code generated.
# of files generated : 36
wrappers.hpp
module.cpp
generated_functions.h
generated_functions_0.cpp
generated_functions_1.cpp
generated_functions_2.cpp
generated_functions_3.cpp
generated_functions_4.cpp
generated_functions_5.cpp
generated_functions_6.cpp
generated_functions_7.cpp
generated_functions_8.cpp
generated_functions_9.cpp
generated_functions_10.cpp
generated_functions_11.cpp
generated_functions_12.cpp
generated_functions_13.cpp
generated_functions_14.cpp
generated_functions_15.cpp
generated_functions_16.cpp
generated_functions_17.cpp
generated_functions_18.cpp
generated_functions_19.cpp
generated_functions_20.cpp
generated_functions_21.cpp
generated_functions_22.cpp
generated_functions_23.cpp
generated_functions_24.cpp
generated_functions_25.cpp
generated_functions_26.cpp
generated_functions_27.cpp
generated_functions_28.cpp
generated_functions_29.cpp
generated_functions_30.cpp
config.h
module.hpp
# of files generated : 1
vnctp.pyi
完成之后,会在 vnctp
文件下生成一个 generated_files
文件夹
进入 c2py/tests/generate/ctp
下有一个 setup.py
执行
$ python setup.py build
$ python setup.py install
这个时候没安装 VisualStudio
会报错,安装一下
C:\Users\Administrator>C:\Python37\Scripts\pip3.exe list
Package Version
---------- -------
c2py 0.5.4
Click 7.0
pip 19.0.3
setuptools 40.8.0
vnctp 1.0.0
导入还存在问题,需要继续查看原因
$ python3
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import vnctp
Traceback (most recent call last):
File "", line 1, in
ImportError: DLL load failed: 找不到指定的模块。
>>>