-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPerl5.html
2999 lines (2446 loc) · 75.5 KB
/
Perl5.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SWIG and Perl5</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="#ffffff">
<H1><a name="Perl5"></a>31 SWIG and Perl5</H1>
<!-- INDEX -->
<div class="sectiontoc">
<ul>
<li><a href="#Perl5_nn2">Overview</a>
<li><a href="#Perl5_nn3">Preliminaries</a>
<ul>
<li><a href="#Perl5_nn4">Getting the right header files</a>
<li><a href="#Perl5_nn5">Compiling a dynamic module</a>
<li><a href="#Perl5_nn6">Building a dynamic module with MakeMaker</a>
<li><a href="#Perl5_nn7">Building a static version of Perl</a>
<li><a href="#Perl5_nn8">Using the module</a>
<li><a href="#Perl5_nn9">Compilation problems and compiling with C++</a>
<li><a href="#Perl5_nn10">Compiling for 64-bit platforms</a>
</ul>
<li><a href="#Perl5_nn11">Building Perl Extensions under Windows</a>
<ul>
<li><a href="#Perl5_nn12">Running SWIG from Developer Studio</a>
<li><a href="#Perl5_nn13">Using other compilers</a>
</ul>
<li><a href="#Perl5_nn14">The low-level interface</a>
<ul>
<li><a href="#Perl5_nn15">Functions</a>
<li><a href="#Perl5_nn16">Global variables</a>
<li><a href="#Perl5_nn17">Constants</a>
<li><a href="#Perl5_nn18">Pointers</a>
<li><a href="#Perl5_nn19">Structures</a>
<li><a href="#Perl5_nn20">C++ classes</a>
<li><a href="#Perl5_nn21">C++ classes and type-checking</a>
<li><a href="#Perl5_nn22">C++ overloaded functions</a>
<li><a href="#Perl5_nn23">Operators</a>
<li><a href="#Perl5_nn24">Modules and packages</a>
</ul>
<li><a href="#Perl5_nn25">Input and output parameters</a>
<li><a href="#Perl5_nn26">Exception handling</a>
<li><a href="#Perl5_nn27">Remapping datatypes with typemaps</a>
<ul>
<li><a href="#Perl5_nn28">A simple typemap example</a>
<li><a href="#Perl5_nn29">Perl5 typemaps</a>
<li><a href="#Perl5_nn30">Typemap variables</a>
<li><a href="#Perl5_nn31">Useful functions</a>
</ul>
<li><a href="#Perl5_nn32">Typemap Examples</a>
<ul>
<li><a href="#Perl5_nn33">Converting a Perl5 array to a char **</a>
<li><a href="#Perl5_nn34">Return values</a>
<li><a href="#Perl5_nn35">Returning values from arguments</a>
<li><a href="#Perl5_nn36">Accessing array structure members</a>
<li><a href="#Perl5_nn37">Turning Perl references into C pointers</a>
<li><a href="#Perl5_nn38">Pointer handling</a>
</ul>
<li><a href="#Perl5_nn39">Proxy classes</a>
<ul>
<li><a href="#Perl5_nn40">Preliminaries</a>
<li><a href="#Perl5_nn41">Structure and class wrappers</a>
<li><a href="#Perl5_nn42">Object Ownership</a>
<li><a href="#Perl5_nn43">Nested Objects</a>
<li><a href="#Perl5_nn44">Proxy Functions</a>
<li><a href="#Perl5_nn45">Inheritance</a>
<li><a href="#Perl5_nn46">Modifying the proxy methods</a>
</ul>
<li><a href="#Perl5_nn47">Adding additional Perl code</a>
</ul>
</div>
<!-- INDEX -->
<p>
<b>Caution: This chapter is under repair!</b>
</p>
<p>
This chapter describes SWIG's support of Perl5. Although the Perl5
module is one of the earliest SWIG modules, it has continued to evolve
and has been improved greatly with the help of SWIG users. For the
best results, it is recommended that SWIG be used with Perl 5.8 or
later. We're no longer testing regularly with older versions, but
Perl 5.6 seems to mostly work, while older versions don't.
</p>
<H2><a name="Perl5_nn2"></a>31.1 Overview</H2>
<p>
To build Perl extension modules, SWIG uses a layered approach. At
the lowest level, simple procedural wrappers are generated for
functions, classes, methods, and other declarations in the input file.
Then, for structures and classes, an optional collection of Perl
proxy classes can be generated in order to provide a more natural object oriented Perl
interface. These proxy classes simply build upon the low-level interface.
</p>
<p>
In describing the Perl interface, this chapter begins by covering the
essentials. First, the problem of configuration, compiling,
and installing Perl modules is discussed. Next, the low-level
procedural interface is presented. Finally, proxy classes are
described. Advanced customization features, typemaps, and other
options are found near the end of the chapter.
</p>
<H2><a name="Perl5_nn3"></a>31.2 Preliminaries</H2>
<p>
To build a Perl5 module, run SWIG using the <tt>-perl</tt> option as
follows:
</p>
<div class="code"><pre>
swig -perl example.i
</pre></div>
<p>
This produces two files. The first file, <tt>example_wrap.c</tt>
contains all of the C code needed to build a Perl5 module. The second
file, <tt>example.pm</tt> contains supporting Perl code needed to
properly load the module.
</p>
<p>
To build the module, you will need to compile the file
<tt>example_wrap.c</tt> and link it with the rest of your program.
</p>
<H3><a name="Perl5_nn4"></a>31.2.1 Getting the right header files</H3>
<p>
In order to compile, SWIG extensions need the following Perl5 header files:</p>
<div class="code"><pre>
#include "Extern.h"
#include "perl.h"
#include "XSUB.h"
</pre></div>
<p>
These are typically located in a directory like this</p>
<div class="code"><pre>
/usr/lib/perl/5.14/CORE
</pre></div>
<p>
The SWIG configuration script automatically tries to locate this directory so
that it can compile examples. However, if you need to find out where the directory is
located, an easy way to find out is to ask Perl itself:
</p>
<div class="code">
<pre>
$ perl -e 'use Config; print "$Config{archlib}\n";'
/usr/lib/perl/5.14
</pre>
</div>
<H3><a name="Perl5_nn5"></a>31.2.2 Compiling a dynamic module</H3>
<p>
The preferred approach to building an extension module is to compile it into
a shared object file or DLL. Assuming you have code you need to link to in a file called <tt>example.c</tt>,
you will need to compile your program using commands like this (shown for Linux):
</p>
<div class="code"><pre>
$ swig -perl example.i
$ gcc -fPIC example.c
$ gcc -fPIC -c example_wrap.c -I/usr/lib/perl/5.14/CORE -Dbool=char
$ gcc -shared example.o example_wrap.o -o example.so
</pre></div>
<p>
The exact compiler options vary from platform to platform.
SWIG tries to guess the right options when it is installed. Therefore,
you may want to start with one of the examples in the <tt>SWIG/Examples/perl5</tt>
directory. If that doesn't work, you will need to read the man-pages for
your compiler and linker to get the right set of options. You might also
check the <a href="http://www.dabeaz.com/cgi-bin/wiki.pl">SWIG Wiki</a> for
additional information.
</p>
<p>
When linking the module, the name of the shared object file must match the module name used in
the SWIG interface file. If you used `<tt>%module example</tt>', then
the target should be named `<tt>example.so</tt>',
`<tt>example.sl</tt>', or the appropriate dynamic module name on your system.
</p>
<H3><a name="Perl5_nn6"></a>31.2.3 Building a dynamic module with MakeMaker</H3>
<p>
It is also possible to use Perl to build dynamically loadable modules
for you using the MakeMaker utility. To do this, write a Perl
script such as the following:</p>
<div class="targetlang"><pre>
# File : Makefile.PL
use ExtUtils::MakeMaker;
WriteMakefile(
`NAME' => `example', # Name of package
`LIBS' => [`-lm'], # Name of custom libraries
`OBJECT' => `example.o example_wrap.o' # Object files
);
</pre></div>
<p>
Now, to build a module, simply follow these steps:</p>
<div class="code"><pre>
$ perl Makefile.PL
$ make
$ make install
</pre></div>
<p>
If you are planning to distribute a SWIG-generated module, this is
the preferred approach to compilation. More information about MakeMaker can be
found in "Programming Perl, 2nd ed." by Larry Wall, Tom Christiansen,
and Randal Schwartz.</p>
<H3><a name="Perl5_nn7"></a>31.2.4 Building a static version of Perl</H3>
<p>
If you machine does not support dynamic loading or if you've tried to
use it without success, you can build a new version of the Perl
interpreter with your SWIG extensions added to it. To build a static
extension, you first need to invoke SWIG as follows:</p>
<div class="code"><pre>
$ swig -perl -static example.i
</pre></div>
<p>
By default SWIG includes code for dynamic loading, but the
<tt>-static</tt> option takes it out.</p>
<p>
Next, you will need to supply a <tt>main()</tt> function that
initializes your extension and starts the Perl interpreter. While,
this may sound daunting, SWIG can do this for you automatically as
follows:</p>
<div class="targetlang"><pre>
%module example
%inline %{
extern double My_variable;
extern int fact(int);
%}
// Include code for rebuilding Perl
%include <perlmain.i>
</pre></div>
<p>
The same thing can be accomplished by running SWIG as follows:</p>
<div class="code"><pre>
$ swig -perl -static -lperlmain.i example.i
</pre></div>
<p>
The <tt>perlmain.i</tt> file inserts Perl's <tt>main()</tt> function
into the wrapper code and automatically initializes the SWIG generated
module. If you just want to make a quick a dirty module, this may be
the easiest way. By default, the <tt>perlmain.i</tt> code does not
initialize any other Perl extensions. If you need to use other
packages, you will need to modify it appropriately. You can do this by
just copying <tt>perlmain.i</tt> out of the SWIG library, placing it
in your own directory, and modifying it to suit your purposes.</p>
<p>
To build your new Perl executable, follow the exact same procedure as
for a dynamic module, but change the link line to something like this:
</p>
<div class="code"><pre>
$ gcc example.o example_wrap.o -L/usr/lib/perl/5.14/CORE \
-lperl -lsocket -lnsl -lm -o myperl
</pre></div>
<p>
This will produce a new version of Perl called <tt>myperl</tt>. It
should be functionality identical to Perl with your C/C++ extension
added to it. Depending on your machine, you may need to link with
additional libraries such as <tt>-lsocket, -lnsl, -ldl</tt>, etc.
</p>
<H3><a name="Perl5_nn8"></a>31.2.5 Using the module</H3>
<p>
To use the module, simply use the Perl <tt>use</tt> statement. If
all goes well, you will be able to do this:
</p>
<div class="targetlang"><pre>
$ perl
use example;
print example::fact(4),"\n";
24
</pre></div>
<p>
A common error received by first-time users is the following:
</p>
<div class="targetlang">
<pre>
use example;
Can't locate example.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at - line 1.
BEGIN failed--compilation aborted at - line 1.
</pre>
</div>
<p>
This error is almost caused when the name of the shared object file you created doesn't match the module name
you specified with the <tt>%module</tt> directive.
</p>
<p>
A somewhat related, but slightly different error is this:
</p>
<div class="targetlang">
<pre>
use example;
Can't find 'boot_example' symbol in ./example.so
at - line 1
BEGIN failed--compilation aborted at - line 1.
</pre>
</div>
<p>
This error is generated because Perl can't locate the module bootstrap function in the
SWIG extension module. This could be caused by a mismatch between the module name and the shared library name.
However, another possible cause is forgetting to link the SWIG-generated wrapper code with the rest
of your application when you linked the extension module.
</p>
<p>
Another common error is the following:
</p>
<div class="targetlang">
<pre>
use example;
Can't load './example.so' for module example: ./example.so:
undefined symbol: Foo at /usr/lib/perl/5.14/i386-linux/DynaLoader.pm line 169.
at - line 1
BEGIN failed--compilation aborted at - line 1.
</pre>
</div>
<p>
This error usually indicates that you forgot to include some object
files or libraries in the linking of the shared library file. Make
sure you compile both the SWIG wrapper file and your original program
into a shared library file. Make sure you pass all of the required libraries
to the linker.
</p>
<p>
Sometimes unresolved symbols occur because a wrapper has been created
for a function that doesn't actually exist in a library. This usually
occurs when a header file includes a declaration for a function that
was never actually implemented or it was removed from a library
without updating the header file. To fix this, you can either edit
the SWIG input file to remove the offending declaration or you can use
the <tt>%ignore</tt> directive to ignore the declaration. Better yet,
update the header file so that it doesn't have an undefined declaration.
</p>
<p>
Finally, suppose that your extension module is linked with another library like this:
</p>
<div class="code">
<pre>
$ gcc -shared example.o example_wrap.o -L/home/beazley/projects/lib -lfoo \
-o example.so
</pre>
</div>
<p>
If the <tt>foo</tt> library is compiled as a shared library, you might get the following
error when you try to use your module:
</p>
<div class="targetlang">
<pre>
use example;
Can't load './example.so' for module example: libfoo.so: cannot open shared object file:
No such file or directory at /usr/lib/perl/5.14/i386-linux/DynaLoader.pm line 169.
at - line 1
BEGIN failed--compilation aborted at - line 1.
>>>
</pre>
</div>
<p>
This error is generated because the dynamic linker can't locate the
<tt>libfoo.so</tt> library. When shared libraries are loaded, the
system normally only checks a few standard locations such as
<tt>/usr/lib</tt> and <tt>/usr/local/lib</tt>. To get the loader to look in other
locations, there are several things you can do. First, you can recompile your extension
module with extra path information. For example, on Linux you can do this:
</p>
<div class="code">
<pre>
$ gcc -shared example.o example_wrap.o -L/home/beazley/projects/lib -lfoo \
<b>-Xlinker -rpath /home/beazley/projects/lib \</b>
-o example.so
</pre>
</div>
<p>
Alternatively, you can set the <tt>LD_LIBRARY_PATH</tt> environment
variable to include the directory with your shared libraries. If
setting <tt>LD_LIBRARY_PATH</tt>, be aware that setting this variable
can introduce a noticeable performance impact on all other
applications that you run. To set it only for Perl, you might want
to do this instead:
</p>
<div class="code">
<pre>
$ env LD_LIBRARY_PATH=/home/beazley/projects/lib perl
</pre>
</div>
<p>
Finally, you can use a command such as <tt>ldconfig</tt> (Linux) or
<tt>crle</tt> (Solaris) to add additional search paths to the default
system configuration (this requires root access and you will need to
read the man pages).
</p>
<H3><a name="Perl5_nn9"></a>31.2.6 Compilation problems and compiling with C++</H3>
<p>
Compilation of C++ extensions has traditionally been a tricky problem.
Since the Perl interpreter is written in C, you need to take steps to
make sure C++ is properly initialized and that modules are compiled
correctly.
</p>
<p>
On most machines, C++ extension modules should be linked using the C++
compiler. For example:
</p>
<div class="code"><pre>
$ swig -c++ -perl example.i
$ g++ -fPIC -c example.cxx
$ g++ -fPIC -c example_wrap.cxx -I/usr/lib/perl/5.14/i386-linux/CORE
$ <b>g++ -shared example.o example_wrap.o -o example.so</b>
</pre></div>
<p>
In addition to this, you may need to include additional library
files to make it work. For example, if you are using the Sun C++ compiler on
Solaris, you often need to add an extra library <tt>-lCrun</tt> like this:
</p>
<div class="code"><pre>
$ swig -c++ -perl example.i
$ CC -c example.cxx
$ CC -c example_wrap.cxx -I/usr/lib/perl/5.14/i386-linux/CORE
$ CC -shared example.o example_wrap.o -o example.so <b>-lCrun</b>
</pre></div>
<p>
Of course, the names of the extra libraries are completely non-portable---you will
probably need to do some experimentation.
</p>
<p>
Another possible compile problem comes from recent versions of Perl (5.8.0) and the GNU tools.
If you see errors having to do with _crypt_struct, that means _GNU_SOURCE is not defined and
it needs to be. So you should compile the wrapper like:
</p>
<div class="code"><pre>
$ g++ -fPIC -c example_wrap.cxx -I/usr/lib/perl/5.8.0/CORE -D_GNU_SOURCE
</pre></div>
<p>
-D_GNU_SOURCE is also included in the Perl ccflags, which can be found by running
</p>
<div class="code"><pre>
$ perl -e 'use Config; print "$Config{ccflags}\n";'
</pre></div>
<p>
So you could also compile the wrapper like
</p>
<div class="code"><pre>
$ g++ -fPIC -c example_wrap.cxx -I/usr/lib/perl/5.8.0/CORE \
`perl -MConfig -e 'print $Config{ccflags}'`
</pre></div>
<p>
Sometimes people have suggested that it is necessary to relink the
Perl interpreter using the C++ compiler to make C++ extension modules work.
In the experience of this author, this has never actually appeared to be
necessary on most platforms. Relinking the interpreter with C++ really only includes the
special run-time libraries described above---as long as you link your extension
modules with these libraries, it should not be necessary to rebuild Perl.
</p>
<p>
If you aren't entirely sure about the linking of a C++ extension, you
might look at an existing C++ program. On many Unix machines, the
<tt>ldd</tt> command will list library dependencies. This should give
you some clues about what you might have to include when you link your
extension module. For example, notice the first line of output here:
</p>
<div class="code">
<pre>
$ ldd swig
<b>libstdc++-libc6.1-1.so.2 => /usr/lib/libstdc++-libc6.1-1.so.2 (0x40019000)</b>
libm.so.6 => /lib/libm.so.6 (0x4005b000)
libc.so.6 => /lib/libc.so.6 (0x40077000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
$
</pre>
</div>
<p>
If linking wasn't enough of a problem, another major complication of C++ is that it does not
define any sort of standard for binary linking of libraries. This
means that C++ code compiled by different compilers will not link
together properly as libraries nor is the memory layout of classes and
data structures implemented in any kind of portable manner. In a
monolithic C++ program, this problem may be unnoticed. However, in Perl, it
is possible for different extension modules to be compiled with
different C++ compilers. As long as these modules are self-contained,
this probably won't matter. However, if these modules start sharing data,
you will need to take steps to avoid segmentation faults and other
erratic program behavior. Also, be aware that certain C++ features, especially RTTI,
can behave strangely when working with multiple modules.
</p>
<p>
It should be noted that you may get a lot of error messages
about the '<tt>bool</tt>' datatype when compiling a C++ Perl module. If
you experience this problem, you can try the following:</p>
<ul>
<li>Use <tt>-DHAS_BOOL</tt> when compiling the SWIG wrapper code
<li>Or use <tt>-Dbool=char</tt> when compiling.
</ul>
<p>
Finally, recent versions of Perl (5.8.0) have namespace conflict problems. Perl defines a bunch
of short macros to make the Perl API function names shorter. For example, in
/usr/lib/perl/5.8.0/CORE/embed.h there is a line:
</p>
<div class="code"><pre>
#define do_open Perl_do_open
</pre></div>
<p>
The problem is, in the <iostream> header from GNU libstdc++v3 there is a private
function named do_open. If <iostream> is included after the perl headers, then
the Perl macro causes the iostream do_open to be renamed, which causes compile errors.
Hopefully in the future Perl will support a PERL_NO_SHORT_NAMES flag, but for now the
only solution is to undef the macros that conflict. Lib/perl5/noembed.h in the SWIG
source has a list of macros that are known to conflict with either standard headers or
other headers. But if you get macro type conflicts from other macros not included
in Lib/perl5/noembed.h while compiling the wrapper, you will
have to find the macro that conflicts and add an #undef into the .i file. Please report
any conflicting macros you find to <a href="http://www.swig.org/mail.html">swig-user mailing list</a>.
</p>
<H3><a name="Perl5_nn10"></a>31.2.7 Compiling for 64-bit platforms</H3>
<p>
On platforms that support 64-bit applications (Solaris, Irix, etc.),
special care is required when building extension modules. On these
machines, 64-bit applications are compiled and linked using a different
set of compiler/linker options. In addition, it is not generally possible to mix
32-bit and 64-bit code together in the same application.
</p>
<p>
To utilize 64-bits, the Perl executable will need to be recompiled
as a 64-bit application. In addition, all libraries, wrapper code,
and every other part of your application will need to be compiled for
64-bits. If you plan to use other third-party extension modules, they
will also have to be recompiled as 64-bit extensions.
</p>
<p>
If you are wrapping commercial software for which you have no source
code, you will be forced to use the same linking standard as used by
that software. This may prevent the use of 64-bit extensions. It may
also introduce problems on platforms that support more than one
linking standard (e.g., -o32 and -n32 on Irix).
</p>
<H2><a name="Perl5_nn11"></a>31.3 Building Perl Extensions under Windows</H2>
<p>
Building a SWIG extension to Perl under Windows is roughly
similar to the process used with Unix. Normally, you will want to
produce a DLL that can be loaded into the Perl interpreter. This
section assumes you are using SWIG with Microsoft Visual C++
although the procedure may be similar with other compilers.
</p>
<H3><a name="Perl5_nn12"></a>31.3.1 Running SWIG from Developer Studio</H3>
<p>
If you are developing your application within Microsoft developer
studio, SWIG can be invoked as a custom build option. The process
roughly requires these steps:</p>
<ul>
<li>Open up a new workspace and use the AppWizard to select a DLL
project.
<li>Add both the SWIG interface file (the .i file), any supporting C
files, and the name of the wrapper file that will be created by SWIG
(ie. <tt>example_wrap.c</tt>). Note: If using C++, choose a
different suffix for the wrapper file such as
<tt>example_wrap.cxx</tt>. Don't worry if the wrapper file doesn't
exist yet--Developer studio will keep a reference to it around.
<li>Select the SWIG interface file and go to the settings menu. Under
settings, select the "Custom Build" option.
<li>Enter "SWIG" in the description field.
<li>Enter "<tt>swig -perl5 -o $(ProjDir)\$(InputName)_wrap.cxx
$(InputPath)</tt>" in the "Build command(s) field"
<li>Enter "<tt>$(ProjDir)\$(InputName)_wrap.c</tt>xx" in the "Output
files(s) field".
<li>Next, select the settings for the entire project and go to
"C++:Preprocessor". Add the include directories for your Perl 5
installation under "Additional include directories".
<li>Define the symbols WIN32 and MSWIN32 under preprocessor options.
If using the ActiveWare port, also define the symbol PERL_OBJECT.
Note that all extensions to the ActiveWare port must be compiled with
the C++ compiler since Perl has been encapsulated in a C++ class.
<li>Finally, select the settings for the entire project and go to
"Link Options". Add the Perl library file to your link libraries.
For example "perl.lib". Also, set the name of the output file to
match the name of your Perl module (ie. example.dll).
<li>Build your project.
</ul>
<p>
Now, assuming you made it this far, SWIG will be automatically invoked when
you build your project. Any changes made to the interface file will
result in SWIG being automatically invoked to produce a new version of
the wrapper file. To run your new Perl extension, simply run Perl and
use the use command as normal. For example:
</p>
<div class="targetlang"><pre>
DOS > perl
use example;
$a = example::fact(4);
print "$a\n";
</pre></div>
<H3><a name="Perl5_nn13"></a>31.3.2 Using other compilers</H3>
<p>
SWIG is known to work with Cygwin and may work with other compilers on Windows.
For general hints and suggestions refer to the <a href="Windows.html#Windows">Windows</a> chapter.
</p>
<H2><a name="Perl5_nn14"></a>31.4 The low-level interface</H2>
<p>
At its core, the Perl module uses a simple low-level interface
to C function, variables, constants, and classes. This low-level interface
can be used to control your application. However, it is also used to
construct more user-friendly proxy classes as described in the next section.
</p>
<H3><a name="Perl5_nn15"></a>31.4.1 Functions</H3>
<p>
C functions are converted into new Perl built-in commands (or
subroutines). For example:
</p>
<div class="targetlang"><pre>
%module example
int fact(int a);
...
</pre></div>
<p>
Now, in Perl:
</p>
<div class="targetlang"><pre>
use example;
$a = &example::fact(2);
</pre></div>
<H3><a name="Perl5_nn16"></a>31.4.2 Global variables</H3>
<p>
Global variables are handled using Perl's magic
variable mechanism. SWIG generates a pair of functions
that intercept read/write operations and attaches them to a Perl variable with
the same name as the C global variable. Thus, an interface like this </p>
<div class="targetlang"><pre>
%module example;
...
double Spam;
...
</pre></div>
<p>
is accessed as follows:</p>
<div class="targetlang"><pre>
use example;
print $example::Spam,"\n";
$example::Spam = $example::Spam + 4
# ... etc ...
</pre></div>
<p>
If a variable is declared as <tt>const</tt>, it is wrapped as a
read-only variable. Attempts to modify its value will result in an
error.
</p>
<p>
To make ordinary variables read-only, you can also use the <tt>%immutable</tt> directive. For example:
</p>
<div class="code">
<pre>
%{
extern char *path;
%}
%immutable;
extern char *path;
%mutable;
</pre>
</div>
<p>
The <tt>%immutable</tt> directive stays in effect until it is explicitly disabled or cleared using
<tt>%mutable</tt>.
See the <a href="SWIG.html#SWIG_readonly_variables">Creating read-only variables</a> section for further details.
</p>
<p>
It is also possible to tag a specific variable as read-only like this:
</p>
<div class="code">
<pre>
%{
extern char *path;
%}
%immutable path;
...
...
extern char *path; // Declared later in the input
</pre>
</div>
<H3><a name="Perl5_nn17"></a>31.4.3 Constants</H3>
<p>
By default, constants are wrapped as read-only Perl variables. For example:
</p>
<div class="code">
<pre>
%module example
#define FOO 42
</pre>
</div>
<p>
In Perl:
</p>
<div class="targetlang">
<pre>
use example;
print $example::FOO,"\n"; # OK
$example::FOO = 2; # Error
</pre>
</div>
<p>
Alternatively, if you use swig's <tt>-const</tt> option, constants are wrapped
such that the leading $ isn't required (by using a constant subroutine), which
usually gives a more natural Perl interface, for example:
</p>
<div class="targetlang">
<pre>
use example;
print example::FOO,"\n";
</pre>
</div>
<H3><a name="Perl5_nn18"></a>31.4.4 Pointers</H3>
<p>
SWIG represents pointers as blessed references. A blessed reference
is the same as a Perl reference except that it has additional
information attached to it indicating what kind of reference it
is. That is, if you have a C declaration like this:</p>
<div class="code"><pre>
Matrix *new_Matrix(int n, int m);
</pre></div>
<p>
The module returns a value generated as follows:
</p>
<div class="targetlang"><pre>
$ptr = new_Matrix(int n, int m); # Save pointer return result
bless $ptr, "p_Matrix"; # Bless it as a pointer to Matrix
</pre></div>
<p>
SWIG uses the "blessing" to check the datatype of various pointers.
In the event of a mismatch, an error or warning message is
generated.</p>
<p>
To check to see if a value is the NULL pointer, use the
<tt>defined()</tt> command:</p>
<div class="targetlang"><pre>
if (defined($ptr)) {
print "Not a NULL pointer.";
} else {
print "Is a NULL pointer.";
}
</pre></div>
<p>
To create a NULL pointer, you should pass the <tt>undef</tt> value to
a function.
</p>
<p>
The "value" of a Perl reference is not the same as the underlying C
pointer that SWIG wrapper functions return. Suppose that <tt>$a</tt>
and <tt>$b</tt> are two references that point to the same C object.
In general, <tt>$a</tt> and <tt>$b</tt> will be different--since they
are different references. Thus, it is a mistake to check the equality
of <tt>$a</tt> and <tt>$b</tt> to check the equality of two C
pointers. The correct method to check equality of C pointers is to
dereference them as follows:
</p>
<div class="targetlang"><pre>
if ($$a == $$b) {
print "a and b point to the same thing in C";
} else {
print "a and b point to different objects.";
}
</pre></div>
<p>
As much as you might be inclined to modify a pointer value directly
from Perl, don't. Manipulating pointer values is architecture dependent and
could cause your program to crash. Similarly, don't try to manually cast
a pointer to a new type by reblessing a pointer. This
may not work like you expect and it is particularly dangerous when
casting C++ objects. If you need to cast a pointer or
change its value, consider writing some helper functions instead. For
example:
</p>
<div class="code">
<pre>
%inline %{
/* C-style cast */
Bar *FooToBar(Foo *f) {
return (Bar *) f;
}
/* C++-style cast */
Foo *BarToFoo(Bar *b) {
return dynamic_cast<Foo*>(b);
}
Foo *IncrFoo(Foo *f, int i) {
return f+i;
}
%}
</pre>
</div>
<p>
Also, if working with C++, you should always try
to use the new C++ style casts. For example, in the above code, the
C-style cast may return a bogus result whereas as the C++-style cast will return
<tt>NULL</tt> if the conversion can't be performed.
</p>
<p>
<b>Compatibility Note:</b> In earlier versions, SWIG tried to preserve the same pointer naming conventions
as XS and <tt>xsubpp</tt>. Given the advancement of the SWIG typesystem and the growing differences between
SWIG and XS, this is no longer supported.
</p>
<H3><a name="Perl5_nn19"></a>31.4.5 Structures</H3>
<p>
Access to the contents of a structure are provided through a set of low-level
accessor functions as described in the "SWIG Basics" chapter. For example,
</p>
<div class="code"><pre>
struct Vector {
double x,y,z;
};
</pre></div>
<p>
gets mapped into the following collection of accessor functions:
</p>
<div class="code"><pre>
struct Vector *new_Vector();
void delete_Vector(Vector *v);
double Vector_x_get(Vector *obj)
void Vector_x_set(Vector *obj, double x)
double Vector_y_get(Vector *obj)
void Vector_y_set(Vector *obj, double y)
double Vector_z_get(Vector *obj)
void Vector_z_set(Vector *obj, double z)
</pre></div>
<p>
These functions are then used to access structure data from Perl as follows:
</p>
<div class="targetlang"><pre>
$v = example::new_Vector();
print example::Vector_x_get($v),"\n"; # Get x component
example::Vector_x_set($v,7.8); # Change x component
</pre></div>