title: Linking libwiringPi on Cross Compiler for RPI
body: <p>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. <br>
  For this I have installed the cross compiler from here: <a href="https://github.com/raspberrypi/tools" rel="nofollow">https://github.com/raspberrypi/tools</a> <br>
  I am using witingPi library so I followed these instructions for installaion: <a href="http://wiringpi.com/download-and-install/" rel="nofollow">http://wiringpi.com/download-and-install/</a> <br>
  The program I am writing is one of the examples from the web (just to make sure I can compile and run it):</p>
  
  <pre><code>#include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  
  #include &lt;wiringPiSPI.h&gt;
  
  #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) &lt; 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;
  }
  </code></pre>
  
  <p>The problem is that I can't get the linkage part with the wiringPi library to work:
  The compilation part works well:
  <code>./arm-linux-gnueabihf-gcc -c /home/guy/main.c -o /home/guy/main.o -I /home/guy/toolchain/wiringPi/wiringPi</code> <br>
  Where <code>/home/guy/toolchain/wiringPi/wiringPi</code> 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).</p>
  
  <p>Next I have tried the following command to link the code with the wirinPi library and create the executable: 
  <code>./arm-linux-gnueabihf-gcc -L/home/guy/toolchain/wiringPi/wiringPi -o "AngleCalculator"  /home/guy/main.o -lwiringPi</code></p>
  
  <p>This failed and I got the following message: <code>/home/guy/toolchain/wiringPi/wiringPi/libwiringPi.so: file not recognized: File format not recognized</code></p>
  
  <p>Some posts suggested using the <code>-static</code> flag during the linkage but it still didn't work and I got the following message: <code>/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</code>. I guess this is because there is no static version of wiringPi.</p>
  
  <p>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?</p>
  
  <p>Thanks</p>