art vanderlay
art vanderlay

Reputation: 2463

how to pass -fPIC to GCC on linux

I am trying to compile libedit on linux using GCC 5.3 and am getting a cryptic error message.

/home/mybin/libgcc/x86_64-unknown-linux-gnu/5.3.0/../../../libcurses.a(lib_termcap.o): relocation R_X86_64_32 against `_nc_globals' can not be used when making a shared object; recompile with -fPIC
/home/mybin/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/../../../libcurses.a: could not read symbols: Bad value

To what does the recompile with -fPIC refer, ncurses or libedit? and then how do I pass the -fPIC flag. I have tried adding CFLAGS=-fPIC to the configure of ncurses & libedit but still did not work.

I have found may posts on SO about what -fPIC is, but none on how to set the flag.

thanks Art

Upvotes: 4

Views: 4309

Answers (3)

Thomas Dickey
Thomas Dickey

Reputation: 54563

Perhaps you ran afoul of the changes outlined in Fedora's Changes/Harden All Packages which use a linker spec that only works if you have compiled using either -fPIC or -fPIE. The linker message is almost useless; only the part about -fPIC has any usefulness.

To address this problem, you can add/modify the compiler flags in several ways. One of the simplest is to set it in the CFLAGS environment variable, e.g.,

export CFLAGS='-O -fPIC'

If you happen to be building ncurses, this means that you would have to also be configuring to build only shared libraries, e.g.,

configure --with-shared --without-normal --without-debug

Of course that all works best if you do not have a previous set of makefiles, etc.

Upvotes: 4

Baroudi Safwen
Baroudi Safwen

Reputation: 842

-fPIC is used to generate position independent code, it is used to create shared libraries. the make file has a problem, to fix it:
edit the Makefile, line 98 :

.c.o:
        ${CC} ${CFLAGS} -c $<

after CC add -fpic after CC like this :

.c.o:
        ${CC} -fpic ${CFLAGS} -c $<

also in line 103:

libedit.so: ${OOBJS}
        ${CC} --shared -o $@ ${OOBJS}

add -fpic after --shared:

libedit.so: ${OOBJS}
        ${CC} --shared -fpic -o $@ ${OOBJS}

if you are wondering what is the difference between -fPIC and -fpic note that they both do the same thing but -fpic is more efficient, check this for more informations What is the difference between `-fpic` and `-fPIC` gcc parameters?.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799150

You're looking at the wrong part of the error message. The "relocation R_X86_64_32" means that you're trying to build 32-bit code against a 64-bit library or vice versa. Make sure you have selected the same architecture for both.

Upvotes: 2

Related Questions