Skip to content

Commit cee7238

Browse files
committed
Always use -fPIC, not -fpic, when building shared libraries with gcc.
On some platforms, -fpic fails for sufficiently large shared libraries. We've mostly not hit that boundary yet, but there are some extensions such as Citus and pglogical where it's becoming a problem. A bit of research suggests that the penalty for -fPIC is small, in the single-digit-percentage range --- and there's none at all on popular platforms such as x86_64. So let's just default to -fPIC everywhere and provide one less thing for extension developers to worry about. Per complaint from Christoph Berg. Back-patch to all supported branches. (I did not bother to touch the recently-removed Makefiles for sco and unixware in the back branches, though. We'd have no way to test that it doesn't break anything on those platforms.) Discussion: https://postgr.es/m/20170529155850.qojdfrwkkqnjb3ap@msg.df7cb.de
1 parent 4a3bb96 commit cee7238

File tree

4 files changed

+15
-26
lines changed

4 files changed

+15
-26
lines changed

doc/src/sgml/dfunc.sgml

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@
6161
<listitem>
6262
<para>
6363
The compiler flag to create <acronym>PIC</acronym> is
64-
<option>-fpic</option>. To create shared libraries the compiler
64+
<option>-fPIC</option>. To create shared libraries the compiler
6565
flag is <option>-shared</option>.
6666
<programlisting>
67-
gcc -fpic -c foo.c
67+
gcc -fPIC -c foo.c
6868
gcc -shared -o foo.so foo.o
6969
</programlisting>
7070
This is applicable as of version 3.0 of
@@ -80,14 +80,14 @@ gcc -shared -o foo.so foo.o
8080
<para>
8181
The compiler flag of the system compiler to create
8282
<acronym>PIC</acronym> is <option>+z</option>. When using
83-
<application>GCC</application> it's <option>-fpic</option>. The
83+
<application>GCC</application> it's <option>-fPIC</option>. The
8484
linker flag for shared libraries is <option>-b</option>. So:
8585
<programlisting>
8686
cc +z -c foo.c
8787
</programlisting>
8888
or:
8989
<programlisting>
90-
gcc -fpic -c foo.c
90+
gcc -fPIC -c foo.c
9191
</programlisting>
9292
and then:
9393
<programlisting>
@@ -122,13 +122,11 @@ ld -shared -o foo.so foo.o
122122
<listitem>
123123
<para>
124124
The compiler flag to create <acronym>PIC</acronym> is
125-
<option>-fpic</option>. On some platforms in some situations
126-
<option>-fPIC</option> must be used if <option>-fpic</option>
127-
does not work. Refer to the GCC manual for more information.
125+
<option>-fPIC</option>.
128126
The compiler flag to create a shared library is
129127
<option>-shared</option>. A complete example looks like this:
130128
<programlisting>
131-
cc -fpic -c foo.c
129+
cc -fPIC -c foo.c
132130
cc -shared -o foo.so foo.o
133131
</programlisting>
134132
</para>
@@ -155,12 +153,12 @@ cc -bundle -flat_namespace -undefined suppress -o foo.so foo.o
155153
<listitem>
156154
<para>
157155
The compiler flag to create <acronym>PIC</acronym> is
158-
<option>-fpic</option>. For <acronym>ELF</acronym> systems, the
156+
<option>-fPIC</option>. For <acronym>ELF</acronym> systems, the
159157
compiler with the flag <option>-shared</option> is used to link
160158
shared libraries. On the older non-ELF systems, <literal>ld
161159
-Bshareable</literal> is used.
162160
<programlisting>
163-
gcc -fpic -c foo.c
161+
gcc -fPIC -c foo.c
164162
gcc -shared -o foo.so foo.o
165163
</programlisting>
166164
</para>
@@ -173,10 +171,10 @@ gcc -shared -o foo.so foo.o
173171
<listitem>
174172
<para>
175173
The compiler flag to create <acronym>PIC</acronym> is
176-
<option>-fpic</option>. <literal>ld -Bshareable</literal> is
174+
<option>-fPIC</option>. <literal>ld -Bshareable</literal> is
177175
used to link shared libraries.
178176
<programlisting>
179-
gcc -fpic -c foo.c
177+
gcc -fPIC -c foo.c
180178
ld -Bshareable -o foo.so foo.o
181179
</programlisting>
182180
</para>
@@ -190,7 +188,7 @@ ld -Bshareable -o foo.so foo.o
190188
<para>
191189
The compiler flag to create <acronym>PIC</acronym> is
192190
<option>-KPIC</option> with the Sun compiler and
193-
<option>-fpic</option> with <application>GCC</>. To
191+
<option>-fPIC</option> with <application>GCC</>. To
194192
link shared libraries, the compiler option is
195193
<option>-G</option> with either compiler or alternatively
196194
<option>-shared</option> with <application>GCC</>.
@@ -200,7 +198,7 @@ cc -G -o foo.so foo.o
200198
</programlisting>
201199
or
202200
<programlisting>
203-
gcc -fpic -c foo.c
201+
gcc -fPIC -c foo.c
204202
gcc -G -o foo.so foo.o
205203
</programlisting>
206204
</para>

src/makefiles/Makefile.linux

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
AROPT = crs
2+
23
export_dynamic = -Wl,-E
34
# Use --enable-new-dtags to generate DT_RUNPATH instead of DT_RPATH.
45
# This allows LD_LIBRARY_PATH to still work when needed.
56
rpath = -Wl,-rpath,'$(rpathdir)',--enable-new-dtags
7+
68
DLSUFFIX = .so
79

8-
ifeq "$(findstring sparc,$(host_cpu))" "sparc"
910
CFLAGS_SL = -fPIC
10-
else
11-
CFLAGS_SL = -fpic
12-
endif
11+
1312

1413
# Rule for building a shared library from a single .o file
1514
%.so: %.o

src/makefiles/Makefile.netbsd

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ endif
99

1010
DLSUFFIX = .so
1111

12-
ifeq ($(findstring sparc,$(host_cpu)), sparc)
1312
CFLAGS_SL = -fPIC -DPIC
14-
else
15-
CFLAGS_SL = -fpic -DPIC
16-
endif
1713

1814

1915
# Rule for building a shared library from a single .o file

src/makefiles/Makefile.openbsd

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ endif
77

88
DLSUFFIX = .so
99

10-
ifeq ($(findstring sparc,$(host_cpu)), sparc)
1110
CFLAGS_SL = -fPIC -DPIC
12-
else
13-
CFLAGS_SL = -fpic -DPIC
14-
endif
1511

1612

1713
# Rule for building a shared library from a single .o file

0 commit comments

Comments
 (0)