pi3g / bme68x-python-library

Python 3 Library for BME688 and BME680 (Bosch Sensortec sensors), supports Bosch BSEC
MIT License
46 stars 18 forks source link

Issue with shared lib in setup.py when using Pi4 with PiFour_Armv8 BSEC 2.4.4 Library #21

Open ahmadnav opened 1 year ago

ahmadnav commented 1 year ago

Error:

/usr/bin/ld: BSEC2.4.0.0_Generic_Release_23012023/algo/normal_version/bin/RaspberryPi/PiFour_Armv8/libalgobsec.a(ChannelHub.o): relocation R_AARCH64_ADR_PREL_PG_HI21 against symbol `uv2' which may bind externally can not be used when making a shared object; recompile with -fPIC ChannelHub.c:(.text+0xe88): dangerous relocation: unsupported relocation

We cannot use the PiThree v6 and v8 libs in pi4 aarch64.

Recreate by setting lib_dirs to lib_dirs = ['/usr/local/lib', 'BSEC2.4.0.0_Generic_Release_23012023/algo/normal_version/bin/RaspberryPi/PiFour_Armv8']

and running sudo python3 setup.py install

The main issue seems to be that libalgobsec.a is not compiled with the -fpic flag a compiler directive to output position independent code, a characteristic required by shared libraries.

mcalisterkm commented 1 year ago

The main issue is that the Bosch-Sensortec BSEC library is 32bit, and I think you are using a 64bit OS.

mcalisterkm commented 1 year ago

The main issue is that the Bosch-Sensortec BSEC library is 32bit, and I think you are using a 64bit OS. Oooops

I see that BSEC2.4.0.0_Generic_Release_23012023/algo/normal_version/bin/RaspberryPi/PiFour_Armv8 is indeed 64 bit

# readelf -h libalgobsec.a

File: libalgobsec.a(Bsec.o)
ELF Header:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  Class:                             ELF64
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              REL (Relocatable file)
  Machine:                           AArch64
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          0 (bytes into file)
  Start of section headers:          10416 (bytes into file)
  Flags:                             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0

So I have tried to compile the version we made to support BSEC 2.4.0 (https://github.com/mcalisterkm/bme68x-python-library-bsec2.4.0.0) by changing only setup.py to point to the new library and if fails I got this linker error (and a stack of warnings)

/usr/bin/ld: BSEC2.4.0.0_Generic_Release_23012023/algo/normal_version/bin/RaspberryPi/PiFour_Armv8/libalgobsec.a: error adding symbols: file format not recognized
collect2: error: ld returned 1 exit status

I will do some more investigation but I suspect there are lots of type mismatch problems as well as the linker error.

Keith

mcalisterkm commented 1 year ago

OK I have rebuilt the OS with 64bit raspbian lite (bullseye) and now I get the same errors as you

The PiFour_Armv8 (libalgobsec.a) is a 64bit relocatable file $ readelf -h libalgobsec.a

File: libalgobsec.a(Bsec.o) ELF Header: Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 Class: ELF64 Data: 2's complement, little endian Version: 1 (current) OS/ABI: UNIX - System V ABI Version: 0 Type: REL (Relocatable file) Machine: AArch64 Version: 0x1 Entry point address: 0x0 Start of program headers: 0 (bytes into file) Start of section headers: 10416 (bytes into file) Flags: 0x0 Size of this header: 64 (bytes) Size of program headers: 0 (bytes) Number of program headers: 0 Size of section headers: 64 (bytes) Number of section headers: 13 Section header string table index: 12

This is a relocatable format as is the PiThree_ArmV6 32bit file, but for 64 bit the architecture is AArch64.

What gcc-10 is failing to do is produce its equivalent of the 32 bit ./build/lib.linux-armv7l/ bme68x.cpython-39-arm-linux-gnueabihf.so. In the 64 bit raspian the .c files from the module are compiled to .o but the linker fails in this last step of producing the bme library.

I have played around with the order of linking but not made any progress on resolving the issue. I understand that the error messages suggest recompiling algobsec with -Fpic but it may well be a red herring, and much more likely to be issues with the linker.

I also looked at the gcc/Linux/m64 library but this is for X86/AMD not arm64

mcalisterkm commented 1 year ago

I raised a query on the Bosch-Sensortech forum to reopen the 64 support question, and noticed they had posted an updated PiFour_Arm8.zip in the forum as an attachment, here

And it works … well it compiles and I have run the forced_mode.py and air quality.py examples.

I still think (know) there will be issues and this will need some work.

If you want to try it you will need to edit setup.py and change "libs_dirs = [……" to include the path to the new library and remove the previous path. I created a new folder PiFour-Aug2023 a put the new library in it and my libs_dirs looks like this: lib_dirs = ['/usr/lib/aarch64-linux-gnu','/usr/lib','/lib/aarch64-linux-gnu', 'PiFour-Aug2023']

Regards Keith

fabioCarfi95 commented 1 year ago

I raised a query on the Bosch-Sensortech forum to reopen the 64 support question, and noticed they had posted an updated PiFour_Arm8.zip in the forum as an attachment, here

And it works … well it compiles and I have run the forced_mode.py and air quality.py examples.

I still think (know) there will be issues and this will need some work.

If you want to try it you will need to edit setup.py and change "libs_dirs = [……" to include the path to the new library and remove the previous path. I created a new folder PiFour-Aug2023 a put the new library in it and my libs_dirs looks like this: lib_dirs = ['/usr/lib/aarch64-linux-gnu','/usr/lib','/lib/aarch64-linux-gnu', 'PiFour-Aug2023']

Regards Keith

Hello, I used the zip provided by Bosch too and the library successfully installed. Anyway, when I launch the examples I always get a traceback error that say (for example in airquality.py)

Traceback (most recent call last): File "airquality.py", line 40, in <module> bme=BME68X(cnst.BME68X_I2C_ADDR_HIGH, 0) SystemError: <class 'bme68x.BME68X'> returned NULL without setting an error

Any idea why?

mcalisterkm commented 1 year ago

forced_mode and airquality work for me, but parallel_mode gets null returns (looks like it hangs but it's looping). I changed parallel_mode to use get_bsec_data(… instead of the digital_nose data(… and that worked.

I have made changes to bme68xmodule.c to clean up the compiler warnings about data type mismatches, but they were fairly minor. Also I have used the updated BME68x-Sensor-API-Master v4.4.8 - again this has a minor bug fix. I can't see these being the cause of your issue.

I usually have the wrong address (0x77 in your code, alt is typically 0x76) when I get your error, please double check with i2cdetect -y 1 to make sure the sensor is detected and the ID matches the code.

mcalisterkm commented 1 year ago

BOSCH-Sensortech are going to add the 64 bit library to the next release of BSEC. In the mean time you can download from the Bosch-Sensortec Community pages in the link above.