Recommend compiling with PIC consistently.

While shared objects with non-PIC code work on some architectures
(notably x86), unless code is always PIC on that arch (not true for x86)
doing so requires runtime relocations, which prevents the object
actually being shared, and means such segments can't be marked as
read-only.
This commit is contained in:
Olly Betts 2014-02-21 08:09:58 +13:00
commit de7ed84f77
7 changed files with 29 additions and 27 deletions

View file

@ -332,8 +332,8 @@ Assuming you have code you need to link to in a file called <tt>example.c</tt>,
<div class="code"><pre> <div class="code"><pre>
$ swig -java example.i $ swig -java example.i
$ gcc -c example_wrap.c -I/usr/java/include -I/usr/java/include/solaris $ gcc -fPIC -c example_wrap.c -I/usr/java/include -I/usr/java/include/solaris
$ gcc -c example.c $ gcc -fPIC -c example.c
$ ld -G example_wrap.o example.o -o libexample.so $ ld -G example_wrap.o example.o -o libexample.so
</pre></div> </pre></div>
@ -493,8 +493,7 @@ compiler. For example:
<div class="code"><pre> <div class="code"><pre>
% swig -c++ -java example.i % swig -c++ -java example.i
% g++ -c -fpic example.cxx % g++ -c -fpic example.cxx
% g++ -c -fpic example_wrap.cxx -I/usr/java/j2sdk1.4.1/include -I/usr/java/ % g++ -c -fpic example_wrap.cxx -I/usr/java/j2sdk1.4.1/include -I/usr/java/j2sdk1.4.1/include/linux
j2sdk1.4.1/include/linux
% g++ -shared example.o example_wrap.o -o libexample.so % g++ -shared example.o example_wrap.o -o libexample.so
</pre></div> </pre></div>

View file

@ -240,8 +240,8 @@ Most, but not all platforms support the dynamic loading of modules (Windows &amp
</p> </p>
<div class="shell"><pre> <div class="shell"><pre>
$ swig -lua example.i -o example_wrap.c $ swig -lua example.i -o example_wrap.c
$ gcc -I/usr/include/lua -c example_wrap.c -o example_wrap.o $ gcc -fPIC -I/usr/include/lua -c example_wrap.c -o example_wrap.o
$ gcc -c example.c -o example.o $ gcc -fPIC -c example.c -o example.o
$ gcc -shared -I/usr/include/lua -L/usr/lib/lua example_wrap.o example.o -o example.so $ gcc -shared -I/usr/include/lua -L/usr/lib/lua example_wrap.o example.o -o example.so
</pre></div> </pre></div>
<p> <p>

View file

@ -493,8 +493,8 @@ Solaris, you often need to add an extra library <tt>-lCrun</tt> like this:
<div class="code"><pre> <div class="code"><pre>
$ swig -c++ -perl example.i $ swig -c++ -perl example.i
$ CC -c example.cxx $ CC -Kpic -c example.cxx
$ CC -c example_wrap.cxx -I/usr/lib/perl/5.14/i386-linux/CORE $ CC -Kpic -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> $ CC -shared example.o example_wrap.o -o example.so <b>-lCrun</b>
</pre></div> </pre></div>

View file

@ -259,14 +259,22 @@ operating system would look something like this: </p>
<div class="code shell"> <div class="code shell">
<pre>$ swig -ruby example.i <pre>$ swig -ruby example.i
$ gcc -c example.c $ gcc -O2 -fPIC -c example.c
$ gcc -c example_wrap.c -I/usr/local/lib/ruby/1.6/i686-linux $ gcc -O2 -fPIC -c example_wrap.c -I/usr/local/lib/ruby/1.6/i686-linux
$ gcc -shared example.o example_wrap.o -o example.so $ gcc -shared example.o example_wrap.o -o example.so
</pre> </pre>
</div> </div>
<p> For other platforms it may be necessary to compile with the <tt>-fPIC</tt> <p>
option to generate position-independent code. If in doubt, consult the The -fPIC option tells GCC to generate position-independent code (PIC)
which is required for most architectures (it's not vital on x86, but
still a good idea as it allows code pages from the library to be shared between
processes). Other compilers may need a different option specified instead of
-fPIC.
</p>
<p>
If in doubt, consult the
manual pages for your compiler and linker to determine the correct set manual pages for your compiler and linker to determine the correct set
of options. You might also check the <a href="http://www.dabeaz.com/cgi-bin/wiki.pl">SWIG Wiki</a> of options. You might also check the <a href="http://www.dabeaz.com/cgi-bin/wiki.pl">SWIG Wiki</a>
for additional information. </p> for additional information. </p>
@ -325,8 +333,8 @@ using the C++ compiler. For example: </p>
<div class="code shell"> <div class="code shell">
<pre> <pre>
$ swig -c++ -ruby example.i $ swig -c++ -ruby example.i
$ g++ -c example.cxx $ g++ -fPIC -c example.cxx
$ g++ -c example_wrap.cxx -I/usr/local/lib/ruby/1.6/i686-linux $ g++ -fPIC -c example_wrap.cxx -I/usr/local/lib/ruby/1.6/i686-linux
$ g++ -shared example.o example_wrap.o -o example.so $ g++ -shared example.o example_wrap.o -o example.so
</pre> </pre>
</div> </div>

View file

@ -216,7 +216,7 @@ to use the C++ compiler. For example:
<div class="shell"> <div class="shell">
<pre> <pre>
$ swig -c++ -tcl example.i $ swig -c++ -tcl example.i
$ c++ -c example_wrap.cxx $ c++ -fPIC -c example_wrap.cxx
$ c++ example_wrap.o $(OBJS) -o example.so $ c++ example_wrap.o $(OBJS) -o example.so
</pre> </pre>
</div> </div>

View file

@ -368,17 +368,12 @@ for a few common platforms is shown below:</p>
<div class="shell"><pre> <div class="shell"><pre>
# Build a shared library for Solaris # Build a shared library for Solaris
gcc -c example.c example_wrap.c -I/usr/local/include gcc -fpic -c example.c example_wrap.c -I/usr/local/include
ld -G example.o example_wrap.o -o example.so ld -G example.o example_wrap.o -o example.so
# Build a shared library for Linux # Build a shared library for Linux
gcc -fpic -c example.c example_wrap.c -I/usr/local/include gcc -fpic -c example.c example_wrap.c -I/usr/local/include
gcc -shared example.o example_wrap.o -o example.so gcc -shared example.o example_wrap.o -o example.so
# Build a shared library for Irix
gcc -c example.c example_wrap.c -I/usr/local/include
ld -shared example.o example_wrap.o -o example.so
</pre></div> </pre></div>
<p> <p>

View file

@ -139,8 +139,8 @@ using commands like this (shown for Linux):
<div class="code"><pre> <div class="code"><pre>
$ swig -tcl example.i $ swig -tcl example.i
$ gcc -c example.c $ gcc -fPIC -c example.c
$ gcc -c example_wrap.c -I/usr/local/include $ gcc -fPIC -c example_wrap.c -I/usr/local/include
$ gcc -shared example.o example_wrap.o -o example.so $ gcc -shared example.o example_wrap.o -o example.so
</pre></div> </pre></div>
@ -374,8 +374,8 @@ compiler. For example:
<div class="code"><pre> <div class="code"><pre>
% swig -c++ -tcl example.i % swig -c++ -tcl example.i
% g++ -c example.cxx % g++ -fPIC -c example.cxx
% g++ -c example_wrap.cxx -I/usr/local/include % g++ -fPIC -c example_wrap.cxx -I/usr/local/include
% g++ -shared example.o example_wrap.o -o example.so % g++ -shared example.o example_wrap.o -o example.so
</pre></div> </pre></div>
@ -387,8 +387,8 @@ Solaris, you often need to add an extra library <tt>-lCrun</tt> like this:
<div class="code"><pre> <div class="code"><pre>
% swig -c++ -tcl example.i % swig -c++ -tcl example.i
% CC -c example.cxx % CC -KPIC -c example.cxx
% CC -c example_wrap.cxx -I/usr/local/include % CC -KPIC -c example_wrap.cxx -I/usr/local/include
% CC -G example.o example_wrap.o -L/opt/SUNWspro/lib -o example.so -lCrun % CC -G example.o example_wrap.o -L/opt/SUNWspro/lib -o example.so -lCrun
</pre></div> </pre></div>