snappy静态库linux编译

snappy是一款基于C++语言开发的压缩算法,旨在提供较高的压缩解压速率和相对合理的压缩比。

github上的仓库地址 https://github.com/google/snappy

背景

linux下开发一个动态库,编译了snappy的静态库,显示需要 recompile with -fPIC。下载的源码未开启pic。

:-1: error: /home/include/..//lib/libsnappy.a(snappy.cc.o): relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC

:-1: error: /home/include/..//lib/libsnappy.a(snappy-sinksource.cc.o): relocation R_X86_64_32 against symbol `_ZTVN6snappy6SourceE' can not be used when making a shared object; recompile with -fPIC

:-1: error: final link failed: nonrepresentable section on output

:-1: error: collect2: error: ld returned 1 exit status

开启PIC 

set(CMAKE_POSITION_INDEPENDENT_CODE ON)    #开启PIC

# 大约在88行

# BUILD_SHARED_LIBS is a standard CMake variable, but we declare it here to make
# it prominent in the GUI.
option(BUILD_SHARED_LIBS "Build shared libraries(DLLs)." ON)

option(SNAPPY_BUILD_TESTS "Build Snappy's own tests." OFF)

编译过程

1、下载源码

2、


git clone [email protected]:google/snappy #下载源码
git submodule update --init
mkdir build
cd build && cmake ../ && make

3、结果出错。 可能没有这个问题,使用的是2021.6.3的源码,后续可能会修正。

/home/y/Desktop/opensrc/snappy/snappy.cc:1017:9: error: inlining failed in call to always_inline ‘size_t snappy::AdvanceToNextTag(const uint8_t**, size_t*)’: function body can be overwritten at link time
/home/eastmoney/Desktop/opensrc/snappy/snappy.cc:1097:43: note: called from here
         size_t tag_type = AdvanceToNextTag(&ip, &tag);
                           ~~~~~~~~~~~~~~~~^~~~~~~~~~~
make[2]: *** [CMakeFiles/snappy.dir/build.make:102:CMakeFiles/snappy.dir/snappy.cc.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:110:CMakeFiles/snappy.dir/all] 错误 2
make: *** [Makefile:141:all] 错误 2

4、解决方法

修正的pull request 128.

Add inline with SNAPPY_ATTRIBUTE_ALWAYS_INLINE 

参考

a、What is the idiomatic way in CMAKE to add the -fPIC compiler option?

b、relocation R_X86_64_32 against `.rodata' can not be used

猜你喜欢

转载自blog.csdn.net/mybachelor123/article/details/117519710
今日推荐