title: How to compile simple Qt 5.2 project in C4Droid
body: <p>I'm trying to figure how to build and compile a simple Qt project in C4Droid, an C++ compiler for Android.</p>
  
  <p>The program comes with 2 simple examples: An "Hello world" Label (one file), and a example Notepad. The second one, seems interesting, but it deals with forms creating them entirely from code.</p>
  
  <p>I'm investigating if doing this with forms generated from .ui files is possible. I've created a simple project in Qt Creator for Qt 4.8, compiled the .ui form, and adapted the code from Qt 4.8 to Qt 5.2. The project itself is very simple: A form with a QButton and a QLineEdit. When QButton is pressed, "Hello world" appears in QLineEdit.</p>
  
  <p>AS far as I've seen, C4Droid doesn't parse .pro files. To compile project, I must open main.cpp and long-tap "compile", select "Compile multiple source code files (simple)", and then run. C4droid uses it's own "makefile" file, in a .c4droid text file.</p>
  
  <p>These are the files I've posted in my C4droid test folder:</p>
  
  <p>ButtonHelloWorld.pro:</p>
  
  <pre><code>QT       += core gui
  
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  
  TARGET = ButtonHelloWorld
  TEMPLATE = app
  
  SOURCES += main.cpp\
          mainwindow.cpp
  
  HEADERS  += mainwindow.h
  
  FORMS    += mainwindow.ui
  </code></pre>
  
  <p>main.cpp:</p>
  
  <pre><code>#include &lt;QApplication&gt;
  #include "mainwindow.h"
  
  int main(int argc, char *argv[])
  {
      QApplication a(argc, argv);
      MainWindow w;
      w.show();
  
      return a.exec();
  }
  </code></pre>
  
  <p>mainwindow.h:</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  private slots:
      void on_ButtonSayHello_clicked();
  
  private:
      Ui::MainWindow *ui;
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>mainwindow.cpp:</p>
  
  <pre><code>#include "mainwindow.h"
  #include "ui_mainwindow.h"
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  
  void MainWindow::on_ButtonSayHello_clicked()
  {
      ui-&gt;LeditSayHello-&gt;setText(tr("Hello world"));
  }
  </code></pre>
  
  <p>ui_mainwindow.h:</p>
  
  <pre><code>#ifndef UI_MAINWINDOW_H
  #define UI_MAINWINDOW_H
  
  #include &lt;QtCore/QVariant&gt;
  #include &lt;QAction&gt;
  #include &lt;QApplication&gt;
  #include &lt;QButtonGroup&gt;
  #include &lt;QHeaderView&gt;
  #include &lt;QLineEdit&gt;
  #include &lt;QMainWindow&gt;
  #include &lt;QPushButton&gt;
  #include &lt;QVBoxLayout&gt;
  #include &lt;QWidget&gt;
  
  QT_BEGIN_NAMESPACE
  
  class Ui_MainWindow
  {
  public:
      QWidget *centralWidget;
      QVBoxLayout *verticalLayout;
      QPushButton *ButtonSayHello;
      QLineEdit *LeditSayHello;
  
      void setupUi(QMainWindow *MainWindow)
      {
          if (MainWindow-&gt;objectName().isEmpty())
              MainWindow-&gt;setObjectName(QString::fromUtf8("MainWindow"));
          MainWindow-&gt;resize(198, 103);
          centralWidget = new QWidget(MainWindow);
          centralWidget-&gt;setObjectName(QString::fromUtf8("centralWidget"));
          verticalLayout = new QVBoxLayout(centralWidget);
          verticalLayout-&gt;setSpacing(6);
          verticalLayout-&gt;setContentsMargins(11, 11, 11, 11);
          verticalLayout-&gt;setObjectName(QString::fromUtf8("verticalLayout"));
          ButtonSayHello = new QPushButton(centralWidget);
          ButtonSayHello-&gt;setObjectName(QString::fromUtf8("ButtonSayHello"));
  
          verticalLayout-&gt;addWidget(ButtonSayHello);
  
          LeditSayHello = new QLineEdit(centralWidget);
          LeditSayHello-&gt;setObjectName(QString::fromUtf8("LeditSayHello"));
          LeditSayHello-&gt;setAlignment(Qt::AlignCenter);
  
          verticalLayout-&gt;addWidget(LeditSayHello);
  
          MainWindow-&gt;setCentralWidget(centralWidget);
  
          retranslateUi(MainWindow);
  
          QMetaObject::connectSlotsByName(MainWindow);
      } // setupUi
  
      void retranslateUi(QMainWindow *MainWindow)
      {
          MainWindow-&gt;setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
          ButtonSayHello-&gt;setText(QApplication::translate("MainWindow", "Say Hello", 0));
      } // retranslateUi
  
  };
  
  namespace Ui {
      class MainWindow: public Ui_MainWindow {};
  } // namespace Ui
  
  QT_END_NAMESPACE
  
  #endif // UI_MAINWINDOW_H
  </code></pre>
  
  <p>I put here the original mainwindow.ui for reference (c4droid does not use it):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ui version="4.0"&gt;
   &lt;class&gt;MainWindow&lt;/class&gt;
   &lt;widget class="QMainWindow" name="MainWindow"&gt;
    &lt;property name="geometry"&gt;
     &lt;rect&gt;
      &lt;x&gt;0&lt;/x&gt;
      &lt;y&gt;0&lt;/y&gt;
      &lt;width&gt;198&lt;/width&gt;
      &lt;height&gt;103&lt;/height&gt;
     &lt;/rect&gt;
    &lt;/property&gt;
    &lt;property name="windowTitle"&gt;
     &lt;string&gt;MainWindow&lt;/string&gt;
    &lt;/property&gt;
    &lt;widget class="QWidget" name="centralWidget"&gt;
     &lt;layout class="QVBoxLayout" name="verticalLayout"&gt;
      &lt;item&gt;
       &lt;widget class="QPushButton" name="ButtonSayHello"&gt;
        &lt;property name="text"&gt;
         &lt;string&gt;Say Hello&lt;/string&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
      &lt;item&gt;
       &lt;widget class="QLineEdit" name="LeditSayHello"&gt;
        &lt;property name="alignment"&gt;
         &lt;set&gt;Qt::AlignCenter&lt;/set&gt;
        &lt;/property&gt;
       &lt;/widget&gt;
      &lt;/item&gt;
     &lt;/layout&gt;
    &lt;/widget&gt;
   &lt;/widget&gt;
   &lt;layoutdefault spacing="6" margin="11"/&gt;
   &lt;resources/&gt;
   &lt;connections/&gt;
  &lt;/ui&gt;
  </code></pre>
  
  <p>When I try to compile this project from main.cpp in C4droid, the output is:</p>
  
  <blockquote>
    <p>/storage/emulated/0/Bull/C4Droid/ButtonHelloWorld/main.cpp:0: Note: No
    relevant classes found. No output generated.
    /data/data/com.n0n3m4.droidc/files/gcc/tmpdir/ccyCC2RV.o: In function
    <code>MainWindow::MainWindow(QWidget*)': mainwindow.cpp:(.text+0xc4):
    undefined reference to</code>vtable for MainWindow'
    /data/data/com.n0n3m4.droidc/files/gcc/tmpdir/ccyCC2RV.o: In function
    <code>MainWindow::~MainWindow()': mainwindow.cpp:(.text+0x14c): undefined
    reference to</code>vtable for MainWindow'
    /data/data/com.n0n3m4.droidc/files/gcc/tmpdir/ccyCC2RV.o: In function
    <code>MainWindow::tr(char const*, char const*, int)':
    mainwindow.cpp:(.text._ZN10MainWindow2trEPKcS1_i[_ZN10MainWindow2trEPKcS1_i]+0x5c):
    undefined reference to</code>MainWindow::staticMetaObject' collect2: error:
    ld returned 1 exit status</p>
  </blockquote>
  
  <p>What can I do to compile successfully this project?</p>
  
  <p>Of course, I've googled a lot, and tried to include .moc files everywere with no luck.</p>
  
  <p>(By the way, I just tried now to create the tag "C4Droid", but I haven't enough reputation to do that :-/)</p>
  
  <p><strong>Update 1</strong>
  --- Looking carefully the multiple files Qt example shipped with C4droid, I've noticed that it uses Makefile. Reading Makefile, I've noticed that this file was generated using the command:</p>
  
  <blockquote>
    <p>/data/data/com.n0n3m4.droidc/files/gcc/qt/bin/qmake -spec android-g++
    -o Makefile application.pro</p>
  </blockquote>
  
  <p>(This also made me to know that moc, qmake, rcc and uic executables are in /data/data/com.n0n3m4.droidc/files/gcc/qt/bin/ folder in my android)</p>
  
  <p>This should create a Makefile according to file .pro, that automatically tells the system to use uic, moc, etc before compiling, and doing the proper linkings after that. So I entered in Terminal in my android and typed:</p>
  
  <blockquote>
    <p>/data/data/com.n0n3m4.droidc/files/gcc/qt/bin/qmake -spec android-g++
    -o Makefile ButtonHelloWorld.pro</p>
  </blockquote>
  
  <p>All I get is:</p>
  
  <blockquote>
    <p>sh: arm-linux-androideabi-gcc: not found</p>
  </blockquote>
  
  <p><strong>Update 2</strong></p>
  
  <p>The C4droid developer responsed me:</p>
  
  <blockquote>
    <p>"C4droid can parse .pro files, look at the examples carefully. The description on Google Play is the only tutorial available, but it is enough."</p>
  </blockquote>
  
  <p>So I decided to go to my .pro file in C4droid. After looking the example compile options, I decided to long-press compile, selected "Makefile" compilation mode and "Qt application" run mode, and "ButtonHelloWorld.qexe" as executable file.</p>
  
  <p>I now compiles and all appears to go much better, but at end this message pops:</p>
  
  <blockquote>
    <p>Failed to copy file</p>
  </blockquote>
  
  <p>Any hints?</p>
  
  <p><strong>Update 3</strong></p>
  
  <p>OK, I tested and played (a lot) with the example Qt appl bundled I emailed again the developer, and it now has been absolutly handy.</p>
  
  <p>Me:</p>
  
  <blockquote>
    <p>I've decided to do a test with your example. I've copied to a new folder (/storage/emulated/0/Bull/C4Droid/application)</p>
  
  <pre><code>images folder
  application.pro
  application.qrc
  main.cpp
  mainwindow.cpp
  mainwindow.h
  </code></pre>
    
    <p>Then, in C4droid, I open application.pro, long-tap "Compile". Select
    "Makefile" compilation mode and "Qt application" run mode, and put an
    executable file "pplication.so". It compiles OK, but fails with
    "Failed to copy file".</p>
    
    <p>But if I put "libapplication.so" as executable file, it compiles and
    finishes OK.</p>
    
    <p>¿Why?</p>
  </blockquote>
  
  <p>He:</p>
  
  <blockquote>
    <p>Because on Android Qt executables are always named libXXX.so</p>
  </blockquote>
  
  <p>Me:</p>
  
  <blockquote>
    <p>Well, I tried "libappli.so" and got same error.</p>
    
    <p>But, Investigating this, I've seen that executable name must be
    lib[NameOf.proFileWithoutExtension].so, and it's folder-name
    independant. Now your copied example and mine work!!</p>
  </blockquote>
  
  <p>And also, he told me that once compiled, export it to an .APK is so easy as Menu->Export. I've tried it and... it's amazing!!</p>
  
  <p><strong>Conclusion</strong></p>
  
  <p>Solved. Answering my own Question in order to help others in my same situation.</p>