如何配置CMake使用-fPIC构建库? [英] How to configure CMake to build a library with -fPIC?
问题描述
我试图创建一个指定 -fPIC
的静态库。
add_library(cpp-netlib_pic
STATIC
$ {SRCS})
set_property(TARGET cpp-netlib_pic PROPERTY POSITION_INDEPENDENT_CODE)
这不起作用。
库( cpp-netlib_pic
)不包含
-fPIC
标志。
链接到 cpp-netlib_pic
do 但是,将 -fPIC
添加到它们的编译器标记,因为 cpp-netlib_pic
没有连接失败。
这里 foo
将添加 -fPIC
:
add_library
SHARED
$ {SRCS})
target_link_libraries(foo cpp-netlib_pic)
$ b b
我用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
此处注意 -fPIC
。
当创建使用 cpp-netlib_pic
, -fPIC的目标
出现: foo
[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
如何配置CMake使用 -fPIC
?
构建第一个库(
cpp-netlib_pic
=h2_lin>解决方案您似乎忘记了 ON
:
set_property(TARGET cpp-netlib_pic PROPERTY POSITION_INDEPENDENT_CODE ON)
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
?
You seem to have forgotten the ON
:
set_property(TARGET cpp-netlib_pic PROPERTY POSITION_INDEPENDENT_CODE ON)
这篇关于如何配置CMake使用-fPIC构建库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!