google/benchmark

Position-independent code: Recompile with -fPIC

Open

#687 创建于 2018年9月21日

在 GitHub 查看
 (2 评论) (3 反应) (0 负责人)C++ (1,539 fork)batch import
help wanted

仓库指标

Star
 (7,968 star)
PR 合并指标
 (平均合并 4天 2小时) (30 天内合并 19 个 PR)

描述

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?

贡献者指南