Skip to content

use Qt in a premake project

Alexandre Marcireau edited this page Jun 3, 2018 · 8 revisions

Each Qt class (including Chameleon displays) requires an extra file to be generated before compilation. The program handling this step is called moc.

The options required by moc, as well as the include and link directories required during compilation, depend on the platform. Chameleon provides the file "third_party/chameleon/source/qt.lua" to simplify the integration of Qt in a premake 4 project.

Assuming the following project structure:

solution
├── premake4.lua
├── source
|   ├── project.hpp
|   └── project.cpp
└── third_party
    └── chameleon

the file premake4.lua can use qt.lua as follows to properly use chameleon::background_cleaner and chameleon::dvs_display:

local qt = require 'third_party/chameleon/qt'

solution 'solution'
    project 'project'
        kind 'ConsoleApp'
        language 'C++'
        location 'build'
        files {'source/project.hpp', 'source/project.cpp'}
        files(qt.moc({
            'third_party/chameleon/source/background_cleaner.hpp',
            'third_party/chameleon/source/dvs_display.hpp'},
            'build/moc'))
        includedirs(qt.includedirs())
        libdirs(qt.libdirs())
        links(qt.links())
        buildoptions(qt.buildoptions())
        linkoptions(qt.linkoptions())

The functions provided by qt.lua have the following signatures:

-- moc calls Qt's moc on each file, and writes the result to target_directory.
-- os_to_configuration can be used to override os-specific configurations.
-- Only parameters different from the default need to be specified.
function qt.moc(files, target_directory, os_to_configuration)

-- includedirs returns a list to be passed to premake's includedirs function.
-- os_to_configuration can be used to override os-specific configurations.
-- Only parameters different from the default need to be specified.
function qt.includedirs(os_to_configuration)

-- libdirs returns a list to be passed to premake's libdirs function.
-- os_to_configuration can be used to override os-specific configurations.
-- Only parameters different from the default need to be specified.
function qt.libdirs(os_to_configuration)

-- links returns a list to be passed to premake's links function.
-- os_to_configuration can be used to override os-specific configurations.
-- Only parameters different from the default need to be specified.
function qt.links(os_to_configuration)

-- buildoptions returns a list to be passed to premake's buildoptions function.
-- os_to_configuration can be used to override os-specific configurations.
-- Only parameters different from the default need to be specified.
function qt.buildoptions(os_to_configuration)

-- linkoptions returns a list to be passed to premake's linkoptions function.
-- os_to_configuration can be used to override os-specific configurations.
-- Only parameters different from the default need to be specified.
function qt.linkoptions(os_to_configuration)
  • files is an array of absolute paths (or relative to the current working directory) to Chameleon headers.
  • target_directory is the absolute path (or relative to the current working directory) to the output directory for files generated by moc. The directory will be created if it does not exist (recursively if needed).
  • os_to_configuration (optional) is a table associating each operating system supported by premake (bsd, linux, macosx, solaris and windows) with a configuration table. Unless overwritten, the defaults provided by qt.luaare used. A configuration table can have the keys:
    • moc: a string to the moc executable.
    • moc_includedirs: an array of strings, corresponding to the directories to include when calling moc.
    • includedirs: an array of strings, meant to be given to premake's includedirs function.
    • libdirs: an array of strings, meant to be given to premake's libdirs function.
    • links: an array of strings, meant to be given to premake's links function.
    • buildoptions: an array of strings, meant to be given to premake's buildoptions function.
    • linkoptions: an array of strings, meant to be given to premake's linkoptions function.

qt.lua uses the following default configurations:

local os_to_default_configuration = {
    bsd = {

    },
    linux = {
        moc = '/usr/lib/x86_64-linux-gnu/qt5/bin/moc',
        moc_includedirs = {
            '/usr/include/qt5/QtQml',
            '/usr/include/x86_64-linux-gnu/qt5/QtQml'},
        includedirs = {
            '/usr/include/qt5/',
            '/usr/include/qt5/QtQml',
            '/usr/include/x86_64-linux-gnu/qt5',
            '/usr/include/x86_64-linux-gnu/qt5/QtQml'},
        libdirs = {'/usr/lib/x86_64-linux-gnu'},
        links = {'Qt5Core', 'Qt5Gui', 'Qt5Qml', 'Qt5Quick'},
        buildoptions = {'-fPIC'},
        linkoptions = {},
    },
    macosx = {
        moc = '/usr/local/opt/qt/bin/moc',
        moc_includedirs = {'/usr/local/opt/qt/include/QtQml'},
        includedirs = {
            '/usr/local/opt/qt/include',
            '/usr/local/opt/qt/include/QtQml'},
        libdirs = {},
        links = {},
        buildoptions = {'-Wno-comma'},
        linkoptions = {
            '-F /usr/local/opt/qt/lib',
            '-framework QtCore',
            '-framework QtGui',
            '-framework QtQml',
            '-framework QtQuick',
            '-framework QtQuickControls2'},
    },
    solaris = {

    },
    windows = {

    },
}

As an example, the following premake4.lua script:

local qt = require 'third_party/chameleon/qt'

local os_to_configuration = {
    linux = {
        moc = '/custom/path/to/moc',
    },
}

solution 'solution'
    project 'project'
        kind 'ConsoleApp'
        language 'C++'
        location 'build'
        files {'source/project.hpp', 'source/project.cpp'}
        files(qt.moc({
            'third_party/chameleon/source/background_cleaner.hpp',
            'third_party/chameleon/source/dvs_display.hpp'},
            'build/moc',
            os_to_configuration))
        includedirs(qt.includedirs(os_to_configuration))
        libdirs(qt.libdirs(os_to_configuration))
        links(qt.links(os_to_configuration))
        buildoptions(qt.buildoptions(os_to_configuration))
        linkoptions(qt.linkoptions(os_to_configuration))

would used the default values provided by qt.lua, but would use the executable located at /custom/path/to/moc as moc when run on linux.

Clone this wiki locally