gpt4 book ai didi

java - Swig - 为什么我们需要声明函数两次?

转载 作者:太空宇宙 更新时间:2023-11-04 01:57:28 24 4
gpt4 key购买 nike

我想使用 swig 从 Java 调用 C 函数。我读: SWIG Tutorial

Web 包含接口(interface)文件示例:

/* 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();

为什么函数声明有重复?(例如“extern int fact(int n);”在 {% %} 和文件底部声明?

最佳答案

%{ %} block 中的内容被精确复制到生成的 c 文件 (example_wrap.c) 中,用作正向原型(prototype)。 block 外的东西用于生成生成的 .c 文件内的函数。

一个更好的例子假设你已经有一个像 example.h 这样的头文件:

extern void functionIwantToCallFromJava(int);
extern crazyType *functionIDoNotCareAbout(anotherCrazyType *);

所以在 example.i 里面你有

%{ 
#include "example.h"
%}
extern void functionIwantToCallFromJava(int);

现在生成的文件将在开头附近有 #include "example.h",它需要这样 functionIwantToCallFromJava 才能正确原型(prototype)化,但是 SWIG 不必生成大量代码来实现 functionIDoNotCareAbout,即使它是在同一个头文件中定义的。

关于java - Swig - 为什么我们需要声明函数两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32208350/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com