title: SPI_IOC_MESSAGE(N) macro giving me fits
body: <p>I'm having trouble getting a SPI program I'm working on to behave correctly and it seems to be some issue with the SPI_IOC_MESSAGE(N) macro.</p>
  
  <p>Here's sample code that DOESN'T work (ioctl returns EINVAL (22) ):</p>
  
  <pre><code>std::vector&lt;spi_ioc_transfer&gt; tr;
  &lt;code that fills tr with 1+ transfers&gt;
  // Hand the transmission(s) off to the SPI driver
  if (tr.size() &gt; 0)
  {
      int ret = ioctl(fd, SPI_IOC_MESSAGE(tr.size()), tr.data());
      if (ret &lt; 1)
      {
           int err = errno;
      }
  }
  </code></pre>
  
  <p>My test code right now is creating a vector of length 1.
  If I explicitly change the code to:</p>
  
  <pre><code>int ret = ioctl(fd, SPI_IOC_MESSAGE( 1 ), tr.data());
  </code></pre>
  
  <p>...then ioctl(...) succeeds and my bits go down the pipe.
  Looking at the expansion of the SPI_IOC_MESSAGE macro in Eclipse, I don't see why this isn't happy.</p>
  
  <p>Suggestions?</p>
  
  <p>I'm cross-compiling for Linux/ARM (Beaglebone Black) from a 64-bit Linux VM, but that I can't see that affecting the macro.</p>
  
  <p>EDIT:
  Here are the two macro expansions out of the C pre-processor</p>
  
  <pre><code>int ret = ioctl(fd, (((1U) &lt;&lt; (((0 +8)+8)+14)) | ((('k')) &lt;&lt; (0 +8)) | (((0)) &lt;&lt; 0) | ((((sizeof(char[((((tr.size())*(sizeof (struct spi_ioc_transfer))) &lt; (1 &lt;&lt; 14)) ? ((tr.size())*(sizeof (struct spi_ioc_transfer))) : 0)])))) &lt;&lt; ((0 +8)+8))), tr.data());
  </code></pre>
  
  <p>and the literal:</p>
  
  <pre><code>int ret = ioctl(fd, (((1U) &lt;&lt; (((0 +8)+8)+14)) | ((('k')) &lt;&lt; (0 +8)) | (((0)) &lt;&lt; 0) | ((((sizeof(char[((((1)*(sizeof (struct spi_ioc_transfer))) &lt; (1 &lt;&lt; 14)) ? ((1)*(sizeof (struct spi_ioc_transfer))) : 0)])))) &lt;&lt; ((0 +8)+8))), tr.data());
  </code></pre>
  
  <p>Absolutely hideous, but I don't see anything surprising in how <code>tr.size()</code> would be getting used there.</p>
  
  <p><strong>Edit to include what seems to be the answer</strong></p>
  
  <pre><code>#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
  extern "C" {
  #endif
  #include &lt;linux/spi/spidev.h&gt;
  #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
  }
  #endif
  </code></pre>
  
  <p>Wrapping the linux SPI include file in an "extern C" instructs the system to treat that section as plain old C, and that seems to let me call <code>SPI_IOC_MESSAGE( tr.size() )</code> or <code>SPI_IOC_MESSAGE( an_int )</code> and have the correct thing happen (verified with GDB stepthrough and a signal analyzer).</p>