“relocation R_X86_64_32 against xxx can not be use

2019-09-02 02:45发布

问题:

I am using the <bcm2835.h> to connect on the GPIO of my Raspberry Pi. First I implemented a program in C and work perfectly. Now I am translating the same program to C++ and I am getting the error below when I try to start the <bcm2835.h>. My Eclipse IDE says /usr/local/lib/libbcm2835.a(bcm2835.o): relocation R_X86_64_32 against.rodata.str1.8' can not be used when making a shared object; recompile with -fPIConly when I use the functionbcm2835_init()`. I am already using -fPIC to generate the shared library.

class HCSR04r {
private:
    int trigger;
    int echo;

public:
    HCSR04();
    HCSR04(int trigger, int echo);
    ~HCSR04();
    uint64_t cyclePulse(int trigger, int echo);
    float distanceCentimeters();
};
#endif 

#include <iostream>
#include <bcm2835.h>
#include "sensor/HCSR04.h"

HCSR04::HCSR04() {
    this->echo = RPI_V2_GPIO_P1_13;
    this->trigger = RPI_V2_GPIO_P1_15;
}

HCSR04::HCSR04(int trigger, int echo) {
    this->echo = echo;
    this->trigger = trigger;
}

HCSR04::~HCSR04() {
}

float HCSR04::distanceCentimeters() {
    return (float) cyclePulse(trigger, echo) / 55.5;
}

uint64_t HCSR04::cyclePulse(int trigger, int echo) {
    if (!bcm2835_init()) // THE ERROR IS BECAUSE THIS LINE
        return 1;

    uint64_t width;

    //Close the bcm2835 bridge
    bcm2835_close();

    return width;
}

Now the error...

Building target: ../../../lib/libCommunicationLib.so
Invoking: GCC C++ Linker
g++ -shared -o "../../../lib/libCommunicationLib.so"  ./code/utils/Metric.o  ./code/sensor/GPS.o ./code/sensor/HCSR04.o ./code/sensor/ISensor.o  ./code/CommunicationLib.o   -lpthread -lbcm2835 -lgps
/usr/bin/x86_64-linux-gnu-ld: //usr/local/lib/libbcm2835.a(bcm2835.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
//usr/local/lib/libbcm2835.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [../../../lib/libCommunicationLib.so] Error 1
makefile:47: recipe for target '../../../lib/libCommunicationLib.so' failed