了解 Swig 教程示例中的重复
Understanding repetitions in Swig tutorial example
我刚开始学习 swig。我有兴趣在 Ubuntu 机器上从 Python 调用 C++。
我刚开始看介绍教程http://www.swig.org/tutorial.html
考虑该页面上的界面文件 example.i
复制如下。
/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
为什么文件后半部分%{ %}
之间的内容重复了?如手册中所述,http://www.swig.org/Doc3.0/SWIGDocumentation.html#Introduction_nn5
The %{ %} block provides a location for inserting additional code,
such as C header files or additional C declarations, into the
generated C wrapper code.
但它没有解决示例中重复的问题。我错过了什么?
%{
和 %}
之间的代码被逐字插入到生成的 SWIG 包装器中,用于使包装器代码能够访问 header 或列出的声明。
这些标记外的代码指示 SWIG 为列出的每个声明(或整个 header 文件)创建一个包装器。
如果您在第一部分中遗漏了 extern int fact(int n);
,则包装器在编译并链接到包含该函数的源代码或库时将无法访问该函数,因为 extern 声明将是失踪。如果将第二部分排除在外,将不会生成包装器以从脚本语言访问它。
有个捷径:
%inline %{
...
%}
指示 SWIG 插入和包装声明。
我刚开始学习 swig。我有兴趣在 Ubuntu 机器上从 Python 调用 C++。
我刚开始看介绍教程http://www.swig.org/tutorial.html
考虑该页面上的界面文件 example.i
复制如下。
/* example.i */
%module example
%{
/* Put header files here or function declarations like below */
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
%}
extern double My_variable;
extern int fact(int n);
extern int my_mod(int x, int y);
extern char *get_time();
为什么文件后半部分%{ %}
之间的内容重复了?如手册中所述,http://www.swig.org/Doc3.0/SWIGDocumentation.html#Introduction_nn5
The %{ %} block provides a location for inserting additional code, such as C header files or additional C declarations, into the generated C wrapper code.
但它没有解决示例中重复的问题。我错过了什么?
%{
和 %}
之间的代码被逐字插入到生成的 SWIG 包装器中,用于使包装器代码能够访问 header 或列出的声明。
这些标记外的代码指示 SWIG 为列出的每个声明(或整个 header 文件)创建一个包装器。
如果您在第一部分中遗漏了 extern int fact(int n);
,则包装器在编译并链接到包含该函数的源代码或库时将无法访问该函数,因为 extern 声明将是失踪。如果将第二部分排除在外,将不会生成包装器以从脚本语言访问它。
有个捷径:
%inline %{
...
%}
指示 SWIG 插入和包装声明。