I have a RPI 3 model B, and I want to compile a small program for interacting with the device's SPI. I am writing the program on my Linux desktop so I want to cross compile the program.
For this I have installed the cross compiler from here: https://github.com/raspberrypi/tools
I am using witingPi library so I followed these instructions for installaion: http://wiringpi.com/download-and-install/
The program I am writing is one of the examples from the web (just to make sure I can compile and run it):
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <wiringPiSPI.h>
#define CHANNEL 1
void send3(uint8_t a, uint8_t b, uint8_t c) {
uint8_t buf[3];
buf[0] = a;
buf[1] = b;
buf[2] = c;
wiringPiSPIDataRW(CHANNEL, buf, 3);
}
int main(int argc, char** argv) {
if (wiringPiSPISetup(CHANNEL, 4000000) < 0) {
fprintf (stderr, "SPI Setup failed: %s\n", strerror (errno));
exit(errno);
}
printf("start\n");
send3(0x40, 0x0A, 0x0B);
send3(0x41, 0x0A, 0x00);
send3(0x40, 0x00, 0xFF);
send3(0x40, 0x0C, 0xFF);
send3(0x40, 0x13, 0x00);
send3(0x40, 0x01, 0x00);
send3(0x40, 0x04, 0xFF);
printf("done\n");
return 0;
}
The problem is that I can't get the linkage part with the wiringPi library to work:
The compilation part works well:
./arm-linux-gnueabihf-gcc -c /home/guy/main.c -o /home/guy/main.o -I /home/guy/toolchain/wiringPi/wiringPi
Where /home/guy/toolchain/wiringPi/wiringPi
is the path to the wiringPi libraies and headers. I am sure this worked well since the compilation was successful (meaning the access to the wiringPi headers succeeded).
Next I have tried the following command to link the code with the wirinPi library and create the executable:
./arm-linux-gnueabihf-gcc -L/home/guy/toolchain/wiringPi/wiringPi -o "AngleCalculator" /home/guy/main.o -lwiringPi
This failed and I got the following message: /home/guy/toolchain/wiringPi/wiringPi/libwiringPi.so: file not recognized: File format not recognized
Some posts suggested using the -static
flag during the linkage but it still didn't work and I got the following message: /home/guy/toolchain/rpi2/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/../lib/gcc/arm-linux-gnueabihf/4.8.3/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lwiringPi
. I guess this is because there is no static version of wiringPi.
Running the same linkage command on the RPI device itself works successfully. I have no idea what causes this error. Is there another version of gcc cross compiler I should use? Maybe I should use another version of wiringPi?
Thanks