title: Qt Creator proper setup for cross-compilation and remote deployment for the Raspberry Pi 2 - producing binaries for wrong architecture
body: <p>I just went through hell to setup my Qt Creator, Raspberry Pi and Debian 8 (for my notebook) and finally managed to configure everything. Or so I thought. What follows here is a description of what I have done so far so that some wise people here on Stackoverflow might see something stupid I've done (or not done :D).</p>
  
  <p>I am using the following platforms:</p>
  
  <ul>
  <li>Raspberry Pi 2 running the latest Raspbian - the target platform where I want to run my code</li>
  <li>64bit Debian 8 Jessie - the development platform where I want to develop my code</li>
  </ul>
  
  <p>After getting my SSH up and running between my notebook (Debian) and RPi2 (Raspbian) I started looking around for tutorials how to setup my favourite C/C++ IDE <strong>Qt Creator</strong> for development of applications for the RPi2. Project tool is <code>cmake</code> (I'm mentioning this below since the other option - using <code>qmake</code> - is also available and is easier to work with but for my project <code>cmake</code> is mandatory).</p>
  
  <p>Keywords here are of <strong>course cross-compilation</strong> and <strong>remote deployment</strong>.</p>
  
  <ol>
  <li><p><a href="https://wiki.debian.org/CrossToolchains" rel="noreferrer">Getting the toolchain for the cross-compilation</a> - mostly useful for the arm-linux-gnueabihf-g++, which is required for the cross-compilation (I don't want to use <strong>CodeSourcery g++</strong>). The article states that <strong>armhf</strong> is for RPi2 and that is why I picked this compiler in particular</p></li>
  <li><p><a href="https://www.olimex.com/forum/index.php?topic=3826.0" rel="noreferrer">Configure Qt Creator Part 1 - Adding a Linux device, a development kit and compiler</a> - basically here you tell Qt Creator your target platform which in this case being an RPi2 is considered to be a generic Linux device and adding the ARM compiler plus generating a development kit, which I called <strong>RPi2 Kit</strong>:</p></li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/IO5ar.png" rel="noreferrer"><img src="https://i.stack.imgur.com/IO5ar.png" alt="Adding the ARM compiler"></a></p>
  
  <p><a href="https://i.stack.imgur.com/Mps0r.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Mps0r.png" alt="Creating a development kit"></a></p>
  
  <ol start="3">
  <li><p><a href="http://doc.qt.io/qtcreator/creator-project-cmake.html#deploying-cmake-projects-to-embedded-linux-devices" rel="noreferrer">Configure Qt Creator Part 2 - Changing build, run and deployment configuration for the CMake project</a>:</p>
  
  <ul>
  <li>The build configuration is adding the newly created kit (see
  2)):</li>
  </ul></li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/U6wY3.png" rel="noreferrer"><img src="https://i.stack.imgur.com/U6wY3.png" alt="enter image description here"></a></p>
  
  <p>The confirmation that the project will be build using the RPi2 Kit:</p>
  
  <p><a href="https://i.stack.imgur.com/2l6lE.png" rel="noreferrer"><img src="https://i.stack.imgur.com/2l6lE.png" alt="enter image description here"></a></p>
  
  <ul>
  <li><p>The deployment configuration is simply adding a <strong>QtCreatorDeployment.txt</strong> in the root folder of the CMake project:</p>
  
  <pre><code>    sftp://192.168.1.102/
      build/RPiCrossCompileRemoteTest:home/pi/Projects
  </code></pre>
  
  <p>The first line represents the deployment prefix and the second line describes the relative local (on my notebook) path of the executable file (on the left) and the relative remote (on my RPi2) path. Qt Creator's documentation also adds a step for automating this process withing CMakeLists.txt but as a first try I think it's better to do it by hand. Also you can add multiple paths if you need to (which I don't in my case since I can't get a single path working -_-).</p>
  
  <p>Additional steps involve changing a couple of the things in the deployment section of the project (found in the Run configuration) - checking for free space (I decided to do that in case I start building large applications and the space runs out quicker then I manage to keep track of :3), upload the files via SFTP and finally I added running a custom remote command, which simply writes to a text file (on my RPi2!) some silly message as log:</p></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/TpwuT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/TpwuT.png" alt="enter image description here"></a></p>
  
  <ul>
  <li>The run configuration of the project should have also been a breeze. The only thing I changed was setting the working directory of my project to the remote location on my RPi2:</li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/Xu8EP.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Xu8EP.png" alt="enter image description here"></a></p>
  
  <p>Finally here is the code I use:</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  int main()
  {
    std::cout &lt;&lt; "Writing to remote system...";
  
    try
    {
      std::string input = "This is a cross-compilation and remote deployment test for Raspberry Pi using Qt Creator";
      std::ofstream out("remote_log.txt");
      out &lt;&lt; input;
      out.close();
    }
    catch(std::exception e)
    {
      std::cout &lt;&lt; "Failed" &lt;&lt; std::endl;
      return 1;
    }
  
    std::cout &lt;&lt; "Successful" &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>along with the <strong>CMakeLists.txt</strong>:</p>
  
  <pre><code>project(RPiCrossCompileRemoteTest)
  cmake_minimum_required(VERSION 2.8)
  aux_source_directory(. SRC_LIST)
  add_executable(${PROJECT_NAME} ${SRC_LIST})
  </code></pre>
  
  <p>All good right? WRONG! Here is where I'm having trouble figuring out what I am missing or doing wrong - when I build my project the produced binaries are for my notebook's architecture. Here is the output of the command <code>file RPiCrossCompileRemoteTest</code>:</p>
  
  <pre><code>RPiCrossCompileRemoteTest: ELF 64-bit LSB executable, x86-64, version 1
  (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for
  GNU/Linux 2.6.32, BuildID[sha1]=73a819def72e496aab31127add9fb52df2ac2c47,
  not stripped
  </code></pre>
  
  <p>Yeap, it says it loud and clear - x86-64. Deployment doesn't cause any errors (SSH connection successful and <strong>text</strong> files written to RPi2 wihout any issues) and neither does running the binaries (via Qt Creator or terminal) on my notebook. However one more thing I have noticed is that for some reason the SFTP isn't uploading my binary. Yes, the binary is with the incorrect architecture and I won't be able to execute it on my RPi2 but still it should be uploaded right?</p>
  
  <p>I have also checked if for some reason I have picked the wrong compiler by manually compiling my main and producing a binary:</p>
  
  <pre><code>:~$ arm-linux-gnueabihf-g++ main.cpp -o RPiCrossCompileRemoteTest_Manual
  </code></pre>
  
  <p>I got a nice ARM binary, which I was able to copy and run on my RPi2. Additional confirmation from the output of <code>file RPiCrossCompileRemoteTest_Manual</code> was also shown to me:</p>
  
  <pre><code>RPiCrossCompileRemoteTest_Manual: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV),
  dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 
  2.6.32, BuildID[sha1]=8aca4ed570c44a51a820c6aa56e6257129e0c949, not stripped
  </code></pre>
  
  <p>I have no idea what I'm doing wrong. I have the feeling that I'm very, very close to getting this thing work but alas! my efforts for several hours were all in vain.</p>
  
  <p>Help is much appreciated!</p>
  
  <p>PS: Note that I haven't setup my debugger. This is something I plan to do next after getting the basics up and running and reading more about remote debugging.</p>