我正在使用<bcm2835.h>
连接我的Raspberry Pi的GPIO。首先,我用C语言实现了一个程序,并且运行良好。现在,我正在将同一程序转换为C ++,并且在尝试启动<bcm2835.h>
时遇到以下错误。我的Eclipse IDE说/usr/local/lib/libbcm2835.a(bcm2835.o): relocation R_X86_64_32 against
。rodata.str1.8'不能在创建共享库时使用。用-fPIC only when I use the function
bcm2835_init()重新编译。我已经在使用-fPIC生成共享库。
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;
}
现在出现错误...
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