Elsewhere there are things that we all miss, yet it takes just one to notice...

Qt and NASM assembler

After getting the NASM assembler working with Eclipse it didn’t take that long to finally get around to getting it working with Qt.

The main credit goes to this answer on stackoverflow, NASM .pro file in Qt.

So to quote the stackoverflow answer, to add NASM assembler (.asm files), add these lines to your .pro file in your Qt project:

QMAKE_EXTRA_COMPILERS += nasm
NASMEXTRAFLAGS = -f elf64 -g -F dwarf
OTHER_FILES += $$NASM_SOURCES
nasm.output = ${QMAKE_FILE_BASE}.o
nasm.commands = nasm $$NASMEXTRAFLAGS -o ${QMAKE_FILE_BASE}.o ${QMAKE_FILE_NAME}
nasm.input = NASM_SOURCES

And then to get your own sources to compile and be linked along with your project add them to the .pro file as simply as this:

NASM_SOURCES = test.asm

More sources can be added using the ‘NASM_SOURCES +=’. That simple.

Because Qt is almost certainly 99.9% a C++ project, your assembler functions will have to be added by externalising them as C functions like:

extern "C" {
void _testmain();
}

An example of a .pro file for Qt 5.9 is:

#-------------------------------------------------
#
# Project created by QtCreator 2017-11-25T17:39:58
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = WidgetsAsmTest
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

QMAKE_EXTRA_COMPILERS += nasm
NASMEXTRAFLAGS = -f elf64 -g -F dwarf
OTHER_FILES += $$NASM_SOURCES
nasm.output = ${QMAKE_FILE_BASE}.o
nasm.commands = nasm $$NASMEXTRAFLAGS -o ${QMAKE_FILE_BASE}.o ${QMAKE_FILE_NAME}
nasm.input = NASM_SOURCES

NASM_SOURCES = test.asm

SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h

FORMS += \
        mainwindow.ui

Have fun with using assembler with Qt. Next up will be passing function parameters to assembler functions.

Scroll to top