mapreduce c编程无法与-fPIC链接,如何修复?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mapreduce c编程无法与-fPIC链接,如何修复?相关的知识,希望对你有一定的参考价值。

我有一个示例程序,如下所示,w.cpp在ubuntu 18.04上使用g ++ 7.3.0

#include<algorithm>
#include<limits>
#include<string>
#include"stdint.h"
#include"Pipes.hh"
#include"TemplateFactory.hh"
#include"StringUtils.hh"
using namespace std;
using namespace HadoopPipes;
using namespace HadoopUtils;
class wMapper:public Mapper{
public:
    wMapper(TaskContext&){}
    void map(MapContext& context){
        string line = context.getInputValue();
        vector<string> words = splitString(line, " ");
        for(size_t i=0;i<words.size();++i){
            context.emit(words[i], toString(i));
        }
    }
};
class wReducer:public Reducer{
public:
    wReducer(TaskContext&){}
    void reduce(ReduceContext& context){
        int count = 0;
        while(context.nextValue()){
            count += toInt(context.getInputValue());
        }
        context.emit(context.getInputKey(), toString(count));
    }
};
int main(){
    return HadoopPipes::runTask(TemplateFactory<wMapper, wReducer>());
}

然后我编译它:

g++ w.cpp -I$HADOOP_LIB/include -I$HADOOP_HOME/include  
-I$JAVA_HOME/include -L$HADOOP_LIB/native -lhadooppipes -lhadooputils 
-lpthread -lcrypto -o w -fPIC

它给出了一个错误:

/usr/bin/x86_64-linux-gnu-ld: /opt/hadoop-2.8.4/lib/native/libhadooppipes.a(HadoopPipes.cc.o): relocation R_X86_64_32S against symbol `_ZNSs4_Rep20_S_empty_rep_storageE@@GLIBCXX_3.4' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /opt/hadoop-2.8.4/lib/native/libhadooputils.a(StringUtils.cc.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a PIE object; recompile with -fPIC
/usr/bin/x86_64-linux-gnu-ld: /opt/hadoop-2.8.4/lib/native/libhadooputils.a(SerialUtils.cc.o): relocation R_X86_64_32S against symbol `_ZTVN11HadoopUtils12FileInStreamE' can not be used when making a PIE object; recompile with -fPIC

无论我添加或删除“-fPIC”标志,同样的错误。

答案

问题是:

  1. 您的编译器配置为默认构建position-independent executables,和
  2. 您链接的某些库(特别是libhadooppipes.a)的编译方式不允许将它们链接到PIE二进制文件中。

解决方案:将-nopie添加到链接命令行。

以上是关于mapreduce c编程无法与-fPIC链接,如何修复?的主要内容,如果未能解决你的问题,请参考以下文章

C/C++链接静态库报错:dangerous relocation: unsupported relocation(-fPIC)

C/C++链接静态库报错:dangerous relocation: unsupported relocation(-fPIC)

Linux共享对象之编译参数fPIC(转)

makefile 和 编译条件 的简略总结

MapReduce (MRV1)设计理念与基本架构

编译共享库时出现链接错误