[CMake] PIE and PIC in mixed projects

Emil Langrock emil.langrock at gmx.de
Thu Sep 9 07:58:41 EDT 2010


Hi,

I have a project which builds shared objects and executables on linux. The 
goal is to have the library as PIC and the executable as PIE. PIC seems not a 
problem as cmake already does it.

There exist a blacklist for architectures which doesn't support PIE well yet 
(hppa, m68k, mips, mipsel, avr32). This list is managed by the distribution 
and thus the distribution gives the pie information via C(XX|PP)FLAGS to 
cmake.

In case of a platform with pie support, we would have following flags set:

CFLAGS  := -fPIE                                                                                                                                                                        
LDFLAGS := -fPIE -pie


The problem is now that the library cannot have the LDFLAGS -fPIE -pie and 
CFLAGS -fPIE set as it is a PIC and not a PIE.

As result cmake does something like (a little bit simplified):
$ echo 'void test(void) {} void bla(void) { test(); }' > test.c
$ gcc -fPIE -pie -fPIC test.c -shared -o test.so  
/usr/bin/ld.bfd.real: /tmp/cc7oYMnf.o: relocation R_X86_64_PC32 against symbol 
`test' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld.bfd.real: final link failed: Bad value
collect2: ld returned 1 exit status

My first tests with libtool (via autotools project) worked fine. The reason 
seems to be that libtool is intelligent enough detect that pie is not for 
shared objects and strips it for the compilation and linking process for them.

So my question: How is a similar functionality with cmake possible?

Example project is amarok.
-- 
Emil Langrock


More information about the CMake mailing list