benchmark icon indicating copy to clipboard operation
benchmark copied to clipboard

Position-independent code: Recompile with -fPIC

Open Wheest opened this issue 7 years ago • 2 comments

Awright?

I installed Google Bench globally on a Linux based system using:

cmake .. -DCMAKE_BUILD_TYPE=RELEASE -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON
sudo make install

However, when I attempted to build an example benchmark, i.e. the Basic usage one, my build failed. The relevant cmake script looks like this:

add_executable(basic_bench basic_bench.cpp)
target_link_libraries(basic_bench
  pthread
  benchmark)
/usr/bin/ld: //usr/local/lib/libbenchmark.a(benchmark.cc.o): relocation R_X86_64_32 against `.bss' can not be used when making a shared object; recompile with -fPIC

Being one to put a lot of stock by the everyday heros that are of compiler error messages, I inelegantly papped the following line at the top of the benchmark library CMake script, and rebuilt:

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

And lo and behold, the example then worked!

I haven't actually encountered PIC before on my travels, but some Wikipedia level research suggests that it is maybe what one would want to have when dealing with an externally linked library. Perhaps someone more well versed in the area would know what's going on?

Making this issue as information for folk that have the same problem (since I couldn't find a previous example).

Should I make a pull request to add this to the build script, or perhaps in troubleshooting?

Wheest avatar Sep 21 '18 15:09 Wheest

i have not encountered this issue and it doesn't seem widespread so i don't think introducing that variable for everyone is appropriate. i'm open to other options.

dmah42 avatar Apr 27 '21 15:04 dmah42

Maybe specify this in the install document is appropriate? In case some users should compile benchmark with -fPIC, like this.

bshang17 avatar Aug 26 '21 04:08 bshang17