1

I am working on a project with the Raspberry Pi Zero and since I don't have a monitor to connect the Pi and don't want to use vi through ssh I thouhgt that cross compiling would be a decent solution. I am trying to do it in a Windows machine with WSL since almost all the guides I found use Linux.

For my project I need to use the bcm2835 library for managing the GPIOs of the Pi.

I found this guide which tells me how to build a library for cross compilation: https://medium.com/@au42/the-useful-raspberrypi-cross-compile-guide-ea56054de187 When I run cmake to build the shared library I have this out put.

    mariano@MSI:~/raspberrypi/bcm2835-1.68/src$ cmake . - DCMAKE_TOOLCHAIN_FILE=/home/mariano/raspberrypi/pi.cmake
CMake Warning (dev) in CMakeLists.txt:
  No project() command is present.  The top-level CMakeLists.txt file must
  contain a literal, direct call to the project() command.  Add a line of
  code such as

    project(ProjectName)

  near the top of the file, but after cmake_minimum_required().

  CMake is pretending there is a "project(Project)" command on the first
  line.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The C compiler identification is GNU 4.9.3
-- The CXX compiler identification is GNU 4.9.3
-- Check for working C compiler: /home/mariano/raspberrypi/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
-- Check for working C compiler: /home/mariano/raspberrypi/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /home/mariano/raspberrypi/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
-- Check for working CXX compiler: /home/mariano/raspberrypi/tools/arm-bcm2708/arm-rpi-4.9.3-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/mariano/raspberrypi/bcm2835-1.68/src

After that I run make an got this output:

mariano@MSI:~/raspberrypi/bcm2835-1.68/src$ make
Scanning dependencies of target bcm2528
[ 33%] Building C object CMakeFiles/bcm2528.dir/bcm2835.c.o
/home/mariano/raspberrypi/bcm2835-1.68/src/bcm2835.c: In function 'bcm2835_delay':
/home/mariano/raspberrypi/bcm2835-1.68/src/bcm2835.c:531:5: warning: implicit declaration of function 'nanosleep' [-Wimplicit-function-declaration]
     nanosleep(&sleeper, NULL);
     ^
/home/mariano/raspberrypi/bcm2835-1.68/src/bcm2835.c: In function 'malloc_aligned':
/home/mariano/raspberrypi/bcm2835-1.68/src/bcm2835.c:1754:5: warning: implicit declaration of function 'posix_memalign' [-Wimplicit-function-declaration]
     errno = posix_memalign(&mem, BCM2835_PAGE_SIZE, size);
     ^
[ 66%] Building C object CMakeFiles/bcm2528.dir/test.c.o
In file included from /home/mariano/raspberrypi/bcm2835-1.68/src/test.c:10:0:
/home/mariano/raspberrypi/bcm2835-1.68/src/bcm2835.h:692:8: error: unknown type name 'off_t'
 extern off_t bcm2835_peripherals_base;
        ^
make[2]: *** [CMakeFiles/bcm2528.dir/build.make:76: CMakeFiles/bcm2528.dir/test.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/bcm2528.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

Checking the output of the cmake command found these lines

-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found

I googled it and tried a few solutions but they didn't work.

Is there something I am doing wrong? Also, if you have another method to cross compile tell me, I choose this one because it shows how to use other libraries other that ones that come in Raspbian.

Any help would be appreciated.

1
  • I'm not sure, but one thing you might want to test is that you're using a 32 bit compiler. When I was last trying to cross-compile for the Pi4, I had a nightmare getting that to work for Raspbian 32 bit, as it was always trying to build for 64 bit arm architecture.
    – PMF
    Dec 10, 2020 at 7:07

2 Answers 2

1

I remember doing a cross-compilation for ARM using Debian Cross Toolchains (right now I just compile directly on a Pi 4). Raspberry Pi OS is very close to Debian-armhf, so doing everything the Debian way typically has a higher chance of success than the alternatives.

Your immediate problem appears to be wrong version of the C library, libc. How did you install it? On Debian, you would get it with apt-get install crossbuild-essential-armhf, along with other essential packages you need for cross-compilation. If you somehow installed the WSL version, it's not at all surprising that POSIX threads are not working, since Windows is using a different threading model.

If you want to be pedantic, you could pull the actual Raspbian libraries and build against these: this would avoid the rare incompatibilities between RPi and Debian, should you ever run into these.

0

I modify "extern off_t bcm2835_peripherals_base;" to "extern uint32_t bcm2835_peripherals_base;" then it works for my Pi4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.