开发者

How to design my interface for SWIG?

开发者 https://www.devze.com 2023-02-20 20:57 出处:网络
I\'m using SWIG to make my C code work 开发者_如何学Con php...But I don\'t figure out how to write a good interface based on my C code to pass to SWIG.Can somebody help me? The link to see what my C c

I'm using SWIG to make my C code work 开发者_如何学Con php...But I don't figure out how to write a good interface based on my C code to pass to SWIG.Can somebody help me? The link to see what my C code looks like is this one : http://www.pastie.org/1739618 Thanks a lot for your help


You should move your function prototypes to a separate header file. SWIG can process that and generate the required stuff for you. You can follow the SWIG tutorial and specifically pay attention to the section SWIG for the truly lazy which shows how you can avoid having to maintain a separate SWIG interface file.

Combine this with the information from the SWIG and PHP page and you should be able to make things work. Note that SWIG does not support PHP4.

Maybe you can start by trying something like the following out and using it as the input to the swig command:

/* optim_wizard_5.h */

#ifdef SWIG
%module optim_wizard_5
%{
#include "optim_wizard_5.h"
%}
#endif

/* Function prototypes to define later */
void *getCpc(void *ptr);
float getCpc_max(float *arg);
char *do_web_request(char *url);
size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);
float request_cpc();


Some things that often don't work well across languages, with the use of SWIG:

  1. Don't throw exceptions outside of the interface you want to expose to SWIG
  2. Don't return or require as parameters, raw C-arrays in the interface you expose. (Wrap them in structures preferably)
  3. Write SWIG interface files that include the header files you want to expose in your interface to PHP. Don't embed SWIG #ifdefs in the C code itself...
0

精彩评论

暂无评论...
验证码 换一张
取 消