[CMake] CMake - overriding shared linker flags

Hendrik Sattler post at hendrik-sattler.de
Wed Jun 23 06:29:27 EDT 2010


Zitat von K Lakshman <maruthilakshman-cmake at yahoo.com>:
> I'm trying to build a shared library (gcc on Linux CentOS) with -pie  
>  flag. If I
> simply add the flag -pie to CMAKE_SHARED_LINKER_FLAGS "-Wl,-pie", the command
> that gets invoked looks like this:
> gcc -shared -Wl,-pie <rest of the command> -o libmylibrary.so
>
> A shared library built like this is not runnable stand-alone (I get an error
> /usr/lib/libc.so: bad ELF interpreter: No such file or directory).   
> Where as if I
> can get the shared library to be built using this command:
> gcc -Wl,-shared -Wl,-pie <rest of the command> -o libmylibrary.so
>
> then that shared library seems to run fine stand alone. (The only difference
> betweent the two commands is that the -shared flags is passed to gcc in the
> first version and directly to the linker in the second version thus bypassing
> collect2).
>
> How do I change the shared library build command to use -Wl,-shared   
> instead of
> -shared? Do I have to write a complete custom command or can I use   
> some tricks
> to simply change this part alone?


Does that actually make sense? To cite from gcc manpage:
-pie
     Produce a position independent executable on targets which  
support it. For predictable results, you must also specify the same  
set of options that were used to generate code (-fpie, -fPIE, or model  
suboptions) when you specify this option.

-shared
     Produce a shared object which can then be linked with other  
objects to form an executable. Not all systems support this option.  
For predictable results, you must also specify the same set of options  
that were used to generate code (-fpic, -fPIC, or model suboptions)  
when you specify this option.[1]

So, you should NOT use -pie for shared libraries (which are _already_  
position independent) but only for static libraries and executables,  
if you really want that.
Additionally, you then use "gcc -pie", not "gcc -Wl,-pie".

HS




More information about the CMake mailing list