[CMake] Use of -fPIC when linking executables

Lars Nordlund lars.nordlund at hem.utfors.se
Mon Mar 13 17:56:42 EST 2006


Hello

This simple CMakeLists.txt will (on NetBSD at least) cause the binary
'bar' to be linked with -fPIC. Is this really necessary? I know that it
must be used when producing the shared object 'foo', but I did not know
it was needed for the executable also? I have checked the flags passed
from gcc to the linker during the linking of the executable but can not
see that -fPIC makes any difference.


CMakeLists.txt: (the foo.c and bar.c are very simple C files)
PROJECT(foobar)
ADD_LIBRARY(foo SHARED foo.c)
ADD_EXECUTABLE(bar bar.c)
TARGET_LINK_LIBRARIES(bar foo)


Output from verbose make:
/usr/pkg/bin/cmake -H/home/lars/tmp/hello -B/home/lars/tmp/hello/build --check-build-system CMakeFiles/Makefile.cmake 0
make -f CMakeFiles/Makefile2 all
make -f CMakeFiles/foo.dir/build.make CMakeFiles/foo.dir/depend
Scanning dependencies of target foo
/usr/pkg/bin/cmake -E cmake_depends  "Unix Makefiles" /home/lars/tmp/hello/build /home/lars/tmp/hello/build /home/lars/tmp/hello/build/CMakeFiles/foo.dir/DependInfo.cmake
make -f CMakeFiles/foo.dir/build.make CMakeFiles/foo.dir/build
Building C object CMakeFiles/foo.dir/foo.o
gcc -Dfoo_EXPORTS -fPIC -o CMakeFiles/foo.dir/foo.o   -c /home/lars/tmp/hello/foo.c
Linking C shared library libfoo.so
/usr/pkg/bin/cmake -E remove -f libfoo.a libfoo.so
gcc -fPIC  -shared  -o libfoo.so "CMakeFiles/foo.dir/foo.o" 
make -f CMakeFiles/bar.dir/build.make CMakeFiles/bar.dir/depend
Scanning dependencies of target bar
/usr/pkg/bin/cmake -E cmake_depends  "Unix Makefiles" /home/lars/tmp/hello/build /home/lars/tmp/hello/build /home/lars/tmp/hello/build/CMakeFiles/bar.dir/DependInfo.cmake
make -f CMakeFiles/bar.dir/build.make CMakeFiles/bar.dir/build
Building C object CMakeFiles/bar.dir/bar.o
gcc  -o CMakeFiles/bar.dir/bar.o   -c /home/lars/tmp/hello/bar.c
Linking C executable bar
/usr/pkg/bin/cmake -E remove -f bar
gcc   -fPIC "CMakeFiles/bar.dir/bar.o"   -o bar  -L/home/lars/tmp/hello/build -lfoo -Wl,-rpath,/home/lars/tmp/hello/build



Best regards
	Lars Nordlund


More information about the CMake mailing list