Difference between linking libfdisk.a directly and through -lfdisk

56 views Asked by At

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:

/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. And when -lfdisk is used instead of libfdisk.a, it compiles with no errors.

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. When I compile my program (not library) with -lfdisk, program can not create 2 partitions due to error 28 (returns from fdisk_add_partition(...)). I'll share the code if it needs.

1

There are 1 answers

0
user17732522 On BEST ANSWER

With -lfdisk the linker is asked to figure out which library file exactly to use.

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). If a .so is found, it will be preferred for linking if a static link wasn't requested.

Your other method will explicitly add in the file named libfdisk.a in the current directory. 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. If you are trying to build a non-PIE executable, then neither flag is required. GCC may be configured to build PIE by default (as a hardening measure).

So you are probably linking two completely different files.