c++hadoopmapreducelinkerfpic

mapreduce c programing failed to link with -fPIC, how to fix?


I've got a sample program as below, w.cpp on ubunt 18.04 with 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>());
}

Then I compile it:

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

It gives an error:

/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

No matter I add or remove "-fPIC" flag, same error.


Solution

  • The issue is that:

    1. Your compiler is configured to build position-independent executables by default, and
    2. Some of the libraries (in particular libhadooppipes.a) you are linking in were not compiled in a way that allows them to be linked into a PIE binary.

    Solution: add -nopie to your link command line.