How to configure CMake to build a library with -fPIC? [duplicate]

折月煮酒 提交于 2019-12-10 17:22:25

问题


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 do however, 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 target foo 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?


回答1:


You seem to have forgotten the ON:

set_property(TARGET cpp-netlib_pic PROPERTY POSITION_INDEPENDENT_CODE ON)


来源:https://stackoverflow.com/questions/39195955/how-to-configure-cmake-to-build-a-library-with-fpic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!