来呀
欢迎关注我的公众号「测试游记」
pip install https://github.com/nanoric/c2py/archive/master.zip
> c2py generate --helpUsage: 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 TEXTencoding of input files, default is utf-8
-e
:文件的编码,默认 utf-8
-I, --include-path TEXTadditional include paths
-I
:其他的包括路径-A, --additional-include TEXTadditional include files. These files will be included in output cxx file, but skipped by parser.
-A
:额外的包含文件。这些文件将包含在输出cxx文件中,但是解析器将跳过它们。-ew, --string-encoding-windows TEXTencoding 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 TEXTencoding 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 TEXTignore symbols matched
-i
:忽略符号匹配--no-callback-pattern TEXTdisable generation of callback for functions matched (for some virtual method used as undocumented API)
--no-callback-pattern
:禁用为匹配的函数生成回调(对于一些作为未文档化的API使用的虚拟方法)--no-transform-pattern TEXTdisable applying transforms(changing its signature) into functions matched (for some virtual method used as callback only)
--no-transform-pattern
:禁用将转换(更改其签名)应用到匹配的函数(对于某些仅用作回调的虚拟方法)--inout-arg-pattern TEXTmake symbol(arguments only) as input_output
--inout-arg-pattern
:将符号(仅限参数)作为input_output--output-arg-pattern TEXTmake symbol(arguments only) as output only
--output-arg-pattern
:将符号(仅限参数)作为输出--m2c / --no-m2ctreat const macros as global variable
--m2c/--no-m2c
:将const宏作为全局变量--ignore-underline-prefixed / --no-ignore-underline-prefixedignore global variables starts with underline
--ignore-underline-prefixed/--no-ignore-underline-prefixed
:忽略以下划线开始的全局变量--ignore-unsupported / --no-ignore-unsupportedignore functions that has unsupported argument
--ignore-unsupported/--no-ignore-unsupported
:忽略不支持参数的函数--inject-symbol-name / --no-inject-symbol-nameAdd comment to describe every generated symbol's name
--inject-symbol-name/--no-inject-symbol-name
:添加注释来描述每个生成的符号的名称-o, --output-dir PATHmodule source output directory
-o
:模块源输出目录-p, --pyi-output-dir PATHpyi files output directory
-p
:pyi文件输出目录--clear-output-dir / --no-clear-output-dir
--clear-pyi-output-dir / --no-clear-pyi-output-dir
--copy-c2py-includes TEXTcopy all c2py include files, excluding input files to specific dir.
--copy-c2py-includes
:复制所有c2py包含的文件,不包括输入文件到特定的目录。-m, --max-lines-per-file INTEGER RANGE
--generate-setup TEXTif 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 TEXTCheck 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版本生成代码。--helpShow this message and exit.
环境:
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.shc2py 0.5.4parsing ...parsing 100.00: 2697/2697parse finished.processing result ...process finished.# of unsupported functions: 0generating cxx code ...cxx code generated.generating pyi code ...pyi code generated.# of files generated : 36wrappers.hppmodule.cppgenerated_functions.hgenerated_functions_0.cppgenerated_functions_1.cppgenerated_functions_2.cppgenerated_functions_3.cppgenerated_functions_4.cppgenerated_functions_5.cppgenerated_functions_6.cppgenerated_functions_7.cppgenerated_functions_8.cppgenerated_functions_9.cppgenerated_functions_10.cppgenerated_functions_11.cppgenerated_functions_12.cppgenerated_functions_13.cppgenerated_functions_14.cppgenerated_functions_15.cppgenerated_functions_16.cppgenerated_functions_17.cppgenerated_functions_18.cppgenerated_functions_19.cppgenerated_functions_20.cppgenerated_functions_21.cppgenerated_functions_22.cppgenerated_functions_23.cppgenerated_functions_24.cppgenerated_functions_25.cppgenerated_functions_26.cppgenerated_functions_27.cppgenerated_functions_28.cppgenerated_functions_29.cppgenerated_functions_30.cppconfig.hmodule.hpp# of files generated : 1vnctp.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 listPackage Version---------- -------c2py 0.5.4Click 7.0pip 19.0.3setuptools 40.8.0vnctp 1.0.0
导入还存在问题,需要继续查看原因
$ python3Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import vnctpTraceback (most recent call last): File "<stdin>", line 1, in <module>ImportError: DLL load failed: 找不到指定的模块。>>>