Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) How to cross-compile a Qt Creator project from Ubuntu to a windows executable: example 9: any application, use of the moc variable


This is example 9, and perhaps a viable solutions of answering the Qt FAQ How to cross-compile a Qt Creator project from Ubuntu to a windows executable?, which follows [1].














Project information


Operating system: Ubuntu 10.04 LTS Lucid Lynx

IDE: Qt Creator 2.0.0

Project type: GUI application

Compiler: G++ 4.4.1

Libraries used:






Qt project file


# Project created by QtCreator 2010-09-25T09:43:28
QT       += core
QT       -= gui
LIBS += -L/usr/local/lib -lboost_filesystem
TARGET = CppQtCrosscompileToWindowsExample5
CONFIG   += console
CONFIG   -= app_bundle
SOURCES += main.cpp








///cross-compiling a console application with
///a library (that is: boost_filesystem).
#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
//From http://www.richelbilderbeek.nl/CppGetFilesInFolder.htm
const std::vector<std::string> GetFilesInFolder(const std::string& folder)
  std::vector<std::string> v;

  const boost::filesystem::path my_folder
    = boost::filesystem::system_complete(

  if (!boost::filesystem::is_directory(my_folder)) return v;

  const boost::filesystem::directory_iterator j;
  for ( boost::filesystem::directory_iterator i(my_folder);
        i != j;
    if ( boost::filesystem::is_regular_file( i->status() ) )
      const std::string filename = i->path().filename();
      //const std::string full_filename = folder + "/" + filename;
  return v;
//From http://www.richelbilderbeek.nl/CppGetPath.htm
const std::string GetPath(const std::string& filename)
  return boost::filesystem::path(filename).parent_path().string();
int main(int, char* argv[])
  const std::vector<std::string> v = GetFilesInFolder(GetPath(argv[0]));
  std::cout << "Number of files: " << v.size() << '\n';








For this example, I use a console project that calls the Boost libraries. If you only need the STL, example 1: console application, no libaries shows an easy and successfull approach.


Additional qmake arguments can be supplied in the 'Projects -> Build Settings -> Build Steps -> qmake -> Additional arguments' edit.














[1] http://labs.qt.nokia.com/2009/10/12/to-make-or-not-to-make-qmake-and-beyond


5) Cross-compiling

In large mixed cross-compiled (were you compile projects for both host and target systems at the same time) projects, most of the sub-projects normally compile for the target system, while only a few (supporting) projects are intended for the host. This means that projects should easily and automatically pick up the target platform, while a bit more attention should be required for host platform projects only.

The project shouldn?t have to worry too much about which platform, nor how to manipulate the project file to achieve the result. Ideally they would only have to mark that a project is intended for the host platform, like for example:

    var moc = {
        "target" : "moc",
        "platform": "Host",
        "defines" : { "all"           : ["QT_MOC"]},

As the configure process has already figured out which parameters are needed for the host and target platform, the build system would know which defaults to apply to each project type in the cross-compiling setup; so the project developer need not do anything else to the project, unless he/she needs to override the cross-compiling defaults.






Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict