[英]Difference between linking libfdisk.a directly and through -lfdisk
I've made program, which formats storage devices.我制作了程序,可以格式化存储设备。 However, when I've created library (for python GUI) based on this program it starts to show the error:
但是,当我基于此程序创建库(用于 python GUI)时,它开始显示错误:
/usr/bin/ld: fdisk/libfdisk.a(la-label.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC
libfdisk.a, which I use, has been built from source util-linux-2.35
.我使用的 libfdisk.a 是从源代码
util-linux-2.35
构建的。 And when -lfdisk
is used instead of libfdisk.a
, it compiles with no errors.当使用
-lfdisk
而不是libfdisk.a
时,它编译时没有错误。
Compiles with errors:编译错误:
g++ file1.cpp file2.cpp ... -o package.name ... libfdisk.a
Compiles correctly:正确编译:
g++ file1.cpp file2.cpp ... -o package.name ... -lfdisk
What the difference between these 2 ways?这两种方式有什么区别?
But there is another, optional, question about fdisk.但是还有另一个关于 fdisk 的可选问题。 When I compile my program (not library) with
-lfdisk
, program can not create 2 partitions due to error 28 (returns from fdisk_add_partition(...)).当我使用
-lfdisk
编译我的程序(不是库)时,由于错误 28(从 fdisk_add_partition(...) 返回),程序无法创建 2 个分区。 I'll share the code if it needs.如果需要,我会分享代码。
With -lfdisk
the linker is asked to figure out which library file exactly to use.使用
-lfdisk
时,要求 linker 确定要使用的库文件。
The usual linkers on Linux will prefix lib
and then search for files with .so
or .a
ending in the library path, which because you didn't specify any, will be the system library path (probably /usr/lib/
or similar). Linux 上的常用链接器将以
lib
为前缀,然后搜索以库路径结尾的.so
或.a
文件,因为您没有指定任何文件,这将是系统库路径(可能是/usr/lib/
或类似路径) . If a .so
is found, it will be preferred for linking if a static link wasn't requested.如果找到
.so
,如果未请求 static 链接,则首选链接。
Your other method will explicitly add in the file named libfdisk.a
in the current directory.您的其他方法将显式添加当前目录中名为
libfdisk.a
的文件。 That is a static library, not a shared one, and if you try to build a shared library from it, then you need to have compiled libfdisk.a
with -fPIC
or if you try to build a PIE executable at least with -fPIE
.那是一个 static 库,不是共享库,如果您尝试从它构建共享库,则需要使用
-fPIC
编译libfdisk.a
,或者如果您尝试至少使用-fPIE
构建 PIE 可执行文件。 If you are trying to build a non-PIE executable, then neither flag is required.如果您尝试构建非 PIE 可执行文件,则不需要任何标志。 GCC may be configured to build PIE by default (as a hardening measure).
GCC 可以配置为默认构建 PIE(作为加固措施)。
So you are probably linking two completely different files.所以你可能链接了两个完全不同的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.