2012-03-16 102 views
2

我有一個Qt C++項目。將程序集* .o文件包含到Qt C++項目中

系統
的Windows XP
QtCreator和QTSDK最新
C++
的MinGW/GCC/G ++

因爲我需要在裝配做一些功能的性能的原因。我的項目主要是Qt和C++,但我需要將我的C++代碼鏈接到我編寫的某個程序集。我有一個程序集* .o文件,但我無法從我的C++代碼中訪問我的彙編函數。我使用nasm編譯了calc.o文件。

簡化示例。

PRO FILE

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(); 
} 

ERROR

main.cpp:11: undefined reference to `five' 
collect2: ld returned 1 exit status 

TRY#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 
+1

我很想在您評估[我的其他答案]後回答此問題(http://stackoverflow.com /一個/176769分之9741887)。 – karlphillip 2012-03-16 18:37:50

+0

謝謝karlphillip,我永遠不會忘記接受答案,這只是你的答案沒有奏效(還)。檢查該帖子的更新。 WinMain @ 16錯誤。 – L123 2012-03-16 18:48:05

+0

我做了,我已經更新了。 – karlphillip 2012-03-16 18:49:09

回答

0

同樣的事情before

calc.S

.data 
hello: 
    .string "Hello World\n" 

.globl helloasm 
helloasm: 
    movl $4, %eax 
    movl $1, %ebx 
    movl $hello,%ecx 
    movl $12,%edx 
    int $0x80 

    ret 

爲主。 c

#include <stdio.h> 

void helloasm(); 

int main() 
{ 
    printf("* Calling ASM function:\n"); 

    helloasm(); 

    printf("* Done!\n"); 

    return 0; 
} 

calc.pro

TEMPLATE = app 
CONFIG += console 
CONFIG -= qt 

SOURCES += main.c \ 
    calc.S 

把同一目錄中的所有這些文件之後,執行:

qmake 
make 

驗證輸出:

$ ./calc 
* Calling ASM function: 
Hello World 
* Done! 
+0

謝謝,我真的很感謝你的幫助,但(1)我需要C++而不是C(2),即使使用C,它也不起作用。再次,我在WindowXP 32bit上,你的Linux上。也許這是明威的事情? – L123 2012-03-16 19:14:11

+0

查看此頁面的頂部是否有錯誤。我不能在評論中發佈錯誤,因爲格式化破壞了而且沒有足夠的字符。 – L123 2012-03-16 19:20:04

+0

更改.pro文件中聲明的順序,讓'main.c'成爲最後一個條目。不確定這有什麼關係。 – karlphillip 2012-03-16 19:26:10

0

爲什麼你沒有添加calc.o到你的來源?

+0

我將它添加到LIBS中,因爲它是一個編譯文件,而不是源文件。將它添加到源會產生相同的錯誤。 – L123 2012-03-16 19:56:14

2

我發現在QT Creator中使用匯編工作的最佳方式是配置.pro文件。我將nasm編譯器添加到.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 

後,用戶必須分配給NASM_SOURCES變量的所有ASM文件,在我的情況下,它只是一個和它所謂arregloAsm.asm

NASM_SOURCES += arregloAsm.asm 

我希望所有這也適合你!

0

我第一次做了什麼,前面提到:

testasm.pro

QT += core 
QT -= gui 

CONFIG += c++11 

TARGET = testasm 
CONFIG += console 
CONFIG -= app_bundle 

TEMPLATE = app 

SOURCES += main.cpp 

# The following define makes your compiler emit warnings if you use 
# any feature of Qt which as been marked 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 

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 

的main.cpp

#include <QCoreApplication> 

extern "C" void test(); 

int main(int argc, char *argv[]) 
{ 
    QCoreApplication a(argc, argv); 
    test(); 
    return a.exec(); 
} 

TEST.ASM

global test 

bits 64 
section .text 

test: 
    mov rax,0 
    ret 

在菜單Build中點擊build all,應用程序應該運行。在這個例子中它是一個空端子,但在我的系統上它工作。 我正在尋找解決方案,並感謝這篇文章,我終於有了一個。 只記得添加

的extern 「C」無效測試();

到您的main.cpp文件。因爲那是我最大的錯誤,所以請注意「C」。 (在出現語言錯誤的情況下:我不是英語母語的人,如果您喜歡請更正文字)

相關問題