简体   繁体   English

直接链接 libfdisk.a 和通过 -lfdisk 链接的区别

[英]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.

相关问题 链接和绑定有什么区别? - What is the difference between linking and binding? `using`和直接插入之间的区别 - difference between `using` and plugging in directly 直接将浮点变量分配为十六进制整数与通过指针转换分配之间的区别 - Difference between directly assigning a float variable a hexadecimal integer and assigning through pointer conversion 链接OpenMP与-fopenmp和-lgomp之间的区别 - Difference between linking OpenMP with -fopenmp and -lgomp 返回引用和直接修改类的区别? - Difference between returning a reference and modifying a class directly? 来自新重载的调用构造函数和直接调用的构造函数之间有什么区别? - What's the difference between call constructor from new overload and directly? 中间变量和直接返回函数调用之间有区别吗? - Is there a difference between an intermediate variable and return'ing a function call directly? “编译和链接”和“编译”(使用g ++)之间有什么区别? - What is the difference between 'compiling and linking' and just 'compiling' (with g++)? C++:链接库和添加包含目录的区别 - C++ : Difference between linking library and adding include directories Natty Narwhal和Oneiric Ocelot之间共享对象链接的区别 - Difference in shared object linking between Natty Narwhal and Oneiric Ocelot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM