[CMake] How to add -fPIC to a static library?

Nicholas Braden nicholas11braden at gmail.com
Sun Aug 28 18:35:45 EDT 2016


Have you tried set_property(TARGET cpp-netlib_pic PROPERTY
POSITION_INDEPENDENT_CODE ON)? I think you must specify the value ON
for it to work.

On Sun, Aug 28, 2016 at 4:28 PM, Steve Lorimer <steve.lorimer at gmail.com> wrote:
> I'm trying to create a static library with -fPIC specified.
>
> add_library(cpp-netlib_pic
>     STATIC
>     ${SRCS})
> set_property(TARGET cpp-netlib_pic PROPERTY POSITION_INDEPENDENT_CODE)
>
> This doesn't work.
>
> The library (cpp-netlib_pic) is built without the -fPIC flags.
>
> Other targets which link against cpp-netlib_pic have -fPIC added to their
> compiler flags, but the linking fails because cpp-netlib_pic didn't.
>
> Here foo will have -fPIC added:
>
> add_library(foo
>     SHARED
>     ${SRCS})
> target_link_libraries(foo cpp-netlib_pic)
>
>
> I've proved this to myself with make VERBOSE=1
>
> [ 87%] Building CXX object
> third_party/cpp-netlib/CMakeFiles/cpp-netlib_pic.dir/src/server_request_parsers_impl.cpp.o
>
> /usr/bin/c++   ... -std=c++14 -Werror -Wall -Wextra ... -o
> CMakeFiles/cpp-netlib_pic.dir/src/server_request_parsers_impl.cpp.o -c
> .../third_party/cpp-netlib/src/server_request_parsers_impl.cpp
>
> Note no -fPIC here.
>
> When building a target which uses cpp-netlib_pic -fPIC appears:
>
> [ 93%] Building CXX object foo.cc.o
> /usr/bin/c++  ... -std=c++14 -Werror -Wall -Wextra ... -fPIC ... -o
> CMakeFiles/foo_shared_lib.dir/foo.cc.o -c .../foo/foo.cc
>
> How can I configure CMake to build the 1st library (cpp-netlib_pic) with
> -fPIC?
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake


More information about the CMake mailing list