3

I'm compiling C++ static library with g++ and using the -fPIC option. I must use the -fPIC option because eventually this library will be linked with other static libraries to form a dynamic library.

When I test the static library locally, it works completely fine when I don't include the -fPIC option. But as soon as I compile the library with -fPIC, I receive a segmentation fault error at run-time when calling one of the functions.

What reasons could including -fPIC to compile a static library cause a segementation fault at run-time?

Connor Flood
  • 67
  • 1
  • 6
  • 1
    This is very generic, I know, but if your code contains Undefined Behaviour then anything may happen and changing a compiler flag may cause the compiler to exploit that UB in a new way - in any case, the bug is in your code if UB is involved. Just a long-shot guess... – Jesper Juhl Jun 15 '16 at 17:47
  • That's along the lines I was thinking too. I did a fair bit of reading on -fPIC and couldn't find anyone else reporting run-time errors in their code by including it (in most cases, it was including -fPIC that was the solution to their problems) – Connor Flood Jun 15 '16 at 18:03
  • Maybe you could provide a minimal, reproducible, compilable test case that we could try. That would help diagnosing the problem. See http://sscce.org – Jesper Juhl Jun 15 '16 at 18:06
  • I agree, I'll look into creating one. The problem is the static library is compiled from a very large codebase, and is not something that can be made public. – Connor Flood Jun 15 '16 at 18:41

1 Answers1

5

A dynamic library is supposed to be loaded at run-time and can therefore not have position-dependent code.

A static library, on the other hand, is just an archive of object files.

When linking with a dynamic library, the linker adds the name of the library in the executable file, so the loader can load it when it loads the program. When the linker links with a static library, it basically extracts the object files and links with them like any other object file.

So unless you create an executable where all other object files are position-independent (you use -fPIC for your the programs code) then you can't link with a static library which uses position-independent code, the generated executable is simply not set up for it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • So let's say I compile a static library (with the -fPIC option). Now, I want to compile that static library with Test.cpp (which has a main method). When I run the generated executable, only ONE of my functions callled by main causes a segmentation fault, while the rest of the code works fine. But when the static library is compiled WITHOUT -fPIC, then all of the code in the generated executable works g. So why does the inclusion of -fPIC cause only some of the code to fail? – Connor Flood Jun 15 '16 at 17:58
  • @ConnorFlood It's really impossible to say without more information, and depends on what all your functions are doing. Are the functions that work calling other functions in the libraries? Do the function that crashes do that? – Some programmer dude Jun 16 '16 at 06:36