理解Swig教程examp中的重复

2024-03-28 14:03:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我是学游泳的新手。我对在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.

但它并没有解决例子中重复的问题。我错过了什么?你知道吗


Tags: or文件orghttpexamplemyhtmlwww
1条回答
网友
1楼 · 发布于 2024-03-28 14:03:47

%{%}之间的代码被逐字插入到生成的SWIG包装器中,并用于让包装器代码访问列出的头或声明。你知道吗

这些标记之外的代码指示SWIG为列出的每个声明(或整个头文件)制作一个包装器。你知道吗

如果在第一部分中省略了extern int fact(int n);,那么在编译并链接到包含函数的源或库时,包装器将无法访问函数,因为extern声明将丢失。如果不考虑第二部分,就不会生成包装器来从脚本语言访问它。你知道吗

有一个快捷方式:

%inline %{
...
%}

它指示SWIG插入和包装声明。你知道吗

相关问题 更多 >