问题
I've been studying C++ and x64 Assembly the last weeks and would like to get into GUI programming with the Qt IDE. The problem is that I can't find a way to link and compile a file containing assembly code. I've been browsing the internet and this site without finding anything helpful.
Here is what I have tried so far: .pro
QMAKE_CXXFLAGS += -save-temps
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test---
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
QMAKE_EXTRA_COMPILERS += nasm
SOURCES += main.cpp\
mainwindow.cpp\
asm.s
HEADERS += mainwindow.h
FORMS += mainwindow.ui
mainwindow.cpp
//code
extern "C" __int64 assembly();
//code
void MainWindow::on_pushButton_clicked()
{
int zahl = assembly(); // place what the assembly function return into "zahl"
}
asm.s
.code
.global
assembly proc
mov rax, 1
ret
xor rax, rax
assembly endp
end
Build setup img
Attempting to compile the project leads to following error message:
mainwindow.obj:-1: Fehler: LNK2019: unresolved external symbol assembly referenced in function "private: void __cdecl MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked@MainWindow@@AEAAXXZ)
I have also found a setup guide on this for console applications. But it doesn't seem to be suitable for GUI applications: http ://dvisagie.blogspot.de/2011/09/setting-up-qt-creator-for-assemblylinux.html
Here's how I've been compiling assembly files in Visual Studio: .cpp:
#include "stdafx.h"
#include<iostream>
using namespace std;
extern "C" __int64 _stdcall assembly();
int main()
{
cout << assembly() << endl;
system("pause");
return 0;
}
.asm: .code assembly proc
mov rax, 1234
ret
xor rax, rax
assembly endp
end
EDIT: appended build
回答1:
The code should look like this (in your pro file). The first compiler (nasm) generates the lst file from the asm file. The second compiler (nasm_ld) then runs ld to generate an object file.
For the nasm compiler, ASM_FILES
is the input variable that it reads from, and writes the name of the generated lst to LST_FILES
, which is the processed by nasm_ld and the objects added to OBJECTS
, which should make the available for the linker.
nasm.name = nasm ${QMAKE_FILE_IN}
nasm.input = ASM_FILES
nasm.variable_out = OBJECTS
nasm.commands = nasm -f win64 ${QMAKE_FILE_IN} -o ${QMAKE_FILE_OUT}
nasm.output = ${QMAKE_VAR_OBJECTS_DIR}${QMAKE_FILE_IN_BASE}$${first(QMAKE_EXT_OBJ)}
nasm.CONFIG += target_predeps
QMAKE_EXTRA_COMPILERS += nasm
ASM_FILES += asm.s
Edit: I have updated the code based on the comments. Try if this works. It should compile the asm file and add the object file to the linker. Please remove your custom build step first.
回答2:
I was able to compile 64 bit MSVC 2017 with asm files using the following for my pro file. Note: I was building a static library using MDd & MD cxx flags so that I could static link my library to another project but the ASM section at the bottom of the pro file should still work.
TEMPLATE = lib
TARGET = mystaticlib
INCLUDEPATH += .
CONFIG -= qt
CONFIG += staticlib
CONFIG -= debug_and_release debug_and_release_target
LIBS += -lws2_32
QMAKE_CXXFLAGS_DEBUG += /MDd
QMAKE_CXXFLAGS_RELEASE += /MD
win32: DESTDIR = ./
OBJECTS_DIR = ./.obj
HEADERS += myheader.h
SOURCES += mysource.cpp
#add additional compiler for asm source using MS 2017 macro assembler 64 bit
ml64.name = ML64 ${QMAKE_FILE_IN}
ml64.input = ASM_FILES
ml64.variable_out = OBJECTS
ml64.commands = ML64 /c ${QMAKE_FILE_NAME} /Fo ${QMAKE_FILE_OUT}
ml64.output = ${QMAKE_VAR_OBJECTS_DIR}${QMAKE_FILE_IN_BASE}$${first(QMAKE_EXT_OBJ)}
ml64.CONFIG += target_predeps
QMAKE_EXTRA_COMPILERS += ml64
ASM_FILES += \
myx64.asm
来源:https://stackoverflow.com/questions/44943948/setting-up-qt-creator-for-linking-and-compiling-assembly-code