正如您所看到的,失败的链接中所有未定义的引用
位于作为成员的链接对象文件中libpython3.5m.a
, 哪个
是链接中请求的 python3 库的静态版本
命令行通过pkg-config --libs --cflags python3
.
To link a fully static executable (-static
) when the linkage
includes libpython3.5m.a
, the linker must also find static (*.a
) versions of all the
libraries that libpython3.5m.a
depends upon1. The dynamic
(*.so
) versions of all those dependencies are installed on your system.
That is why:
gcc test.c -otest $(pkg-config --libs --cflags python3)
成功,无需-static
。这些依赖项的静态版本是
并非全部安装在您的系统上。因此所有未定义的参考
添加时出现链接错误-static
.
我自己的系统有python3.6,我看到:
$ pkg-config --libs python-3.6
-lpython3.6m
and:
$ locate libpython3.6m.so
/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6m.so
/usr/lib/x86_64-linux-gnu/libpython3.6m.so.1
/usr/lib/x86_64-linux-gnu/libpython3.6m.so.1.0
的动态依赖关系libpython3.6m.so
are:
$ ldd /usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/libpython3.6m.so
linux-vdso.so.1 => (0x00007ffc475af000)
libexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007fa87cf6e000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fa87cd51000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fa87cb32000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fa87c92e000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fa87c72a000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa87c3d4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa87bff4000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa87d85a000)
We can disregard the first and last ones, which don't look like regular libraries
and indeed aren't2. So, I'd conclude that
to satisfy the static dependencies of libpython3.6a
, I need to install the
static versions of:-
libexpat
libz
libpthread
libdl
libutil
libm
libc
这将由这些库的开发包提供。由于我的系统是 64 位 Ubuntu,因此我会过滤这些开发包
经过:
$ dpkg --search libexpat.a libz.a libpthread.a libdl.a libutil.a libm.a libc.a | grep amd64
libexpat1-dev:amd64: /usr/lib/x86_64-linux-gnu/libexpat.a
zlib1g-dev:amd64: /usr/lib/x86_64-linux-gnu/libz.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libpthread.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libdl.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libutil.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libm.a
libc6-dev:amd64: /usr/lib/x86_64-linux-gnu/libc.a
并安装它们:
sudo apt install libexpat1-dev zlib1g-dev libc6-dev
你还没有透露你的系统是什么,但毫无疑问你可以适应这个
思考发现静态依赖关系libpython3.5m.a
, 任何
你的系统是。
[1] Strictly, all
libraries that the members of
libpython3.5m.a
that you are linking depend upon,
but we won't be that pernickity.
[2] 第一个是图书馆的vDSO https://en.wikipedia.org/wiki/VDSO,
根本不是真正的文件。第二个是linux加载器。