0

Dear gtest/cmake experts: Is there a way to use the gtest archive libgtest.a from a location that is different from the location where it was created?

Here's my working case:

libgtest location: /usr/lib/x86_64-linux-gnu/libgtest.a and also in /usr/src/googletest/googletest/lib/libgtest.a

(I'm not sure why/how it is present in two locations. Its possible that I copy/pasted it long ago). The source code and CMakeLists etc for gtest are under /usr/src/googletest/

CMakeLists.txt:

< .. link other libraries, dependencies etc .. >
target_link_libraries(Application PRIVATE libgtest.a)

Note: Application is being built as a "shared object". libgtest.a is static. But this works fine.

Non-working case:

I copied libgtest.a to another location:

 /my/project/dir/depends/pkgs/libgtest.a

CMakeLists.txt:

< .. link other libraries, dependencies etc .. >
target_link_libraries(Application PRIVATE /my/project/dir/depends/pkgget/libgtest.a)

This throws a LOT of "relocation/ fPIC" related errors. For example:

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol testing::FLAGS_gtest_output[abi:cxx11]; recompile with -fPIC
>>> defined in ../../depends/lib/libgtest.a(gtest-all.cc.o)
>>> referenced by gtest-all.cc
>>>               gtest-all.cc.o:(testing::internal::UnitTestOptions::GetOutputFormat[abi:cxx11]()) in archive ../../depends/lib/libgtest.a

ld.lld: error: relocation R_X86_64_PC32 cannot be used against symbol testing::FLAGS_gtest_output[abi:cxx11]; recompile with -fPIC
>>> defined in ../../depends/lib/libgtest.a(gtest-all.cc.o)
>>> referenced by gtest-all.cc
>>>               gtest-all.cc.o:(testing::internal::UnitTestOptions::GetAbsolutePathToOutputFile[abi:cxx11]()) in archive ../../depends/lib/libgtest.a

Questions:

  1. How come there is no problem with relocation/ fPIC in the former case but there is a problem in the latter case? What gives?
  2. What's the correct way to use the pre-compiled archive from a different location?

I'm looking forward to your inputs.

Best regards,

--Venk

venk
  • 1,085
  • 1
  • 7
  • 18
  • 1
    The problem is **unrelated** to the **location** of `libgtest.a`. It seems that in the first case you use `libgtest.a` compiled with `-fPIC`, but in the second case you use **another** library (not a copy!), which is compiled without `-fPIC`. – Tsyvarev Oct 01 '21 at 19:45
  • @Tsyvarev, thank you for looking into this. However, I doubt that is the case for two reasons: (a) I installed gtest in the simple recommended way from the web only once and I had no idea that something like fPIC even existed until yesterday. (b) But more importantly, I tried the readelf command as mentioned here: https://stackoverflow.com/a/1351771/232955 and the output showed a ton of lines related to R_X86_64_PLT32 and a few related to R_X86_64_REX_GOTP (I must confess that I dont yet know what these truly refer to but the above link says that the output should NOT be empty). – venk Oct 02 '21 at 11:03
  • 1
    So, which `libgtest.a` is used in your "working case", `/usr/lib/x86_64-linux-gnu/libgtest.a` or `/usr/src/googletest/googletest/lib/libgtest.a`? The line `target_link_libraries(Application PRIVATE libgtest.a)` could use any of them. If you want to make sure that specific `libgtest.a` is used, then pass **absolute path** to that file in `target_link_libraries` call, like you do in your second, "non-working case". – Tsyvarev Oct 02 '21 at 12:50
  • Thanks @Tsyvarev, this truly helped. I dont have the faintest idea as to how I came to have two libgtest.a files on my laptop or why they are different. But they indeed are different somehow and when I used the .../x86_64-linux-gnu/libgtest.a, things worked fine (including when I copied and moved it elsewhere). This is of significant help. Thank you. – venk Oct 07 '21 at 14:41

0 Answers0