我有一个 Qt C++ 项目。
系统
Windows XP
QtCreator 和 QtSDK 最新
C++
MinGW/gcc/g++
出于性能原因,我需要在汇编中完成一些功能。我的项目主要是 Qt 和 c++,但我需要将我的 C++ 代码链接到我编写的一些程序集。我有一个程序集 *.o 文件,但无法从我的 C++ 代码访问我的程序集函数。我用nasm编译了汇编calc.o文件。
简化示例。
专业文件
QT += core
QT -= gui
TARGET = Test_asm
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS += C:/Users/David/Test_asm/calc.o
SOURCES += main.cpp
MAIN.CPP
#include <QtCore/QCoreApplication>
#include <QTextStream>
extern "C" int five();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream qout(stdout);
int f = five();
qout << "five: " << f << endl;
return a.exec();
}
错误
main.cpp:11: undefined reference to `five'
collect2: ld returned 1 exit status
尝试 #1 - Karlphillip 的方式(不适用于 Windows)
Directory of C:\x
16/03/2012 03:09 PM <DIR> .
16/03/2012 03:09 PM <DIR> ..
16/03/2012 03:07 PM 82 calc.pro
16/03/2012 03:10 PM 178 calc.S
16/03/2012 03:10 PM 164 main.c
3 File(s) 424 bytes
2 Dir(s) 78,905,851,904 bytes free
C:\x>qmake
C:\x>make
'make' is not recognized as an internal or external command,
operable program or batch file.
C:\x>mingw32-make
mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/x'
gcc -c -g -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -I"..\QtSDK\Desktop\Qt\4.7.3\mi
ngw\mkspecs\default" -o debug\main.o main.c
gcc -c -g -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -I"..\QtSDK\Desktop\Qt\4.7.3\mi
ngw\mkspecs\default" -o debug\calc.o calc.S
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-rel
oc -Wl,-subsystem,console -mthreads -Wl -o debug\calc.exe debug/main.o debug/cal
c.o
debug/main.o: In function `main':
C:\x/main.c:9: undefined reference to `helloasm'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\calc.exe] Error 1
mingw32-make[1]: Leaving directory `C:/x'
mingw32-make: *** [debug] Error 2
最佳答案
我发现在 QT Creator 中使用程序集的最佳方法是配置 .pro 文件。我将 nasm 编译器添加到 .pro 文件和一些标志中。如果需要,您可以更改这些标志。但这种配置对我来说效果很好
示例.pro
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
完成后,您必须将所有 ASM 文件分配给 NASM_SOURCES 变量,在我的例子中,它只有一个,名为 arregloAsm.asm
NASM_SOURCES += arregloAsm.asm
我希望这一切也对您有用!
关于c++ - 将程序集 *.o 文件包含到 Qt C++ 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9742608/