8

I try to reference a static *.a library for my c++ project in CDT. I included it in

C/C++ Build -> Cross C++ Linker -> Libraries

But I get the error:

relocation R_X86_64_32S against symbol ... can not be used when making a shared object.

Now from what I have read so far I think this means the compiler thinks I am referencing a shared library, when it is a static library instead - Is that the case and if so what can do now? Is there any way to just use the *.a libraries or do I have to recompile them as .so - objects?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
don-joe
  • 600
  • 1
  • 4
  • 12
  • Just wondering, what is this Cross C++ linker? Is it for cross-compilation? – kiner_shah Dec 08 '21 at 06:16
  • @kiner_shah I'm not really sure what it is exactly. I was referring to a ledger in the CDT IDE, which is the C++ flavor of Eclipse – don-joe Mar 06 '22 at 12:36

3 Answers3

2

The problem is most probably coming from the fact that the static library is compiled without supporting position independent code. I don't know a way to fix it without recompiling the static library.

If you can recompile the static library, then assuming you are using gcc or clang, you have to add the -fPIC flag to your compiler flags.

Without more information it is hard to give more advice. If you can provide a minimal, reproducible example, then I can help you with that.

  • Could you add some reference to the flags to enable PIC eg in CMAKE and/or other common kind of makefiles? Thanks – arivero Dec 10 '21 at 10:07
  • For CMake [it is easy](https://stackoverflow.com/a/38297422/6639989). For other build systems I am not 100% sure, but basically you have to extend the `CFLAGS` and/or `CXXFLAGS` with the `-fPIC` option. For unix makefiles, it looks like something similar to [this](https://stackoverflow.com/a/22803578/6639989). – János Benjamin Antal Dec 10 '21 at 19:24
1

In my case, I had just compiled it as *.a, so I was pretty sure a recompile again as *.a wouldn't work, there was no chance gcc was updated in the meantime. So I tried what you mentioned in the question already: removed the .a libs and recompiled&installed as .so and it worked. Thanks :D

N4ppeL
  • 1,771
  • 18
  • 18
0

I had to recompile, but it sufficed to recompile as *.a library again. I'm pretty sure the problem originated from an update of my gcc compiler.

don-joe
  • 600
  • 1
  • 4
  • 12