用C++编译MASM文件外部参考

发布于 2025-02-02 02:53:56 字数 2520 浏览 3 评论 0原文

我正在尝试在Linux中编译一个具有一些“外部”变量的MASM文件,但我无法将“ .o”文件链接在一起。 我的意思是我有一个“ .cpp”文件,上面有一些“外部”变量和功能,然后我有一个带有一些“ extern”变量的“ .asm”文件。 这些文件就是以下内容:

integersexploration.cpp:

#include <iostream>

extern "C" char GlChar = 10;
extern "C" short GlShort = 20;
extern "C" int GlInt = 30;
extern "C" long long GlLongLong = 0x000000000FFFFFFFF;

extern "C" void IntegerAddition(char a, short b, int c, long long d);

int main(){
    IntegerAddition(3, 5, -37, 11);
    
    printf("int value %d\n", GlInt);

    return 0x0;
}

integersex.asm:

.386
.model flat, c

extern GlChar:byte
extern GlShort:word
extern GlInt:dword
extern GlLongLong:qword

.code

IntegerAddition proc
    push ebp
    mov ebp,esp

; Compute GlChar += a       (char = byte = 8 bit            =>  I use AL)
    mov al,[ebp + 8]
    add [GlChar],al

;Compute GlShort += b       (short = word = 16 bit          =>  I use AX)
    mov ax,[ebp + 12]
    add [GlShort],ax

;Compute GlInt += c         (int = dword = 32 bit           =>  I use EAX)
    mov eax,[ebp + 16]
    add [GlInt],eax

;Compute GlLongLong += d    (long long = qword = 64 bit     =>  I use EAX + EDX)
    mov eax,[ebp + 20]
    mov edx,[ebp + 24]
    add dword ptr[GlLongLong],eax
    adc dword ptr[GlLongLong],edx

    pop ebp
    ret

IntegerAddition endp
                end

我正在使用:我正在与:

g++ -Wall -c IntegersExploration.cpp

我一起编译“ integersexploration.cpp”:然后,我将“ integersex.asm”汇总:

jwasm -coff IntegersEx.asm

但是当我链接2“ .o”对象时,我会使用:

g++ -m32 IntegersExploration.o IntegersEx.o

我会收到以下错误:

/usr/bin/ld: i386:x86-64 architecture of input file `IntegersExploration.o' is incompatible with i386 output
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x8): undefined reference to `_GlChar'
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x13): undefined reference to `_GlShort'
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x1c): undefined reference to `_GlInt'
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x28): undefined reference to `_GlLongLong'
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x2e): undefined reference to `_GlLongLong'
/usr/bin/ld: IntegersExploration.o: in function `main':
IntegersExploration.cpp:(.text+0x19): undefined reference to `IntegerAddition'
collect2: error: ld returned 1 exit status

就像“ ASM”文件未正确链接到“ CPP”文件的外部变量。我该如何解决?

非常感谢! 有一个不错的编码!

I'm trying to compile in linux a MASM file that's have some "extern" variables but I'm not able to link the ".o" files together.
I mean I have a ".cpp" file with some "extern" variables and function on it and then I have a ".asm" file with some "extern" variables on it.
The files are these:

IntegersExploration.cpp:

#include <iostream>

extern "C" char GlChar = 10;
extern "C" short GlShort = 20;
extern "C" int GlInt = 30;
extern "C" long long GlLongLong = 0x000000000FFFFFFFF;

extern "C" void IntegerAddition(char a, short b, int c, long long d);

int main(){
    IntegerAddition(3, 5, -37, 11);
    
    printf("int value %d\n", GlInt);

    return 0x0;
}

IntegersEx.asm:

.386
.model flat, c

extern GlChar:byte
extern GlShort:word
extern GlInt:dword
extern GlLongLong:qword

.code

IntegerAddition proc
    push ebp
    mov ebp,esp

; Compute GlChar += a       (char = byte = 8 bit            =>  I use AL)
    mov al,[ebp + 8]
    add [GlChar],al

;Compute GlShort += b       (short = word = 16 bit          =>  I use AX)
    mov ax,[ebp + 12]
    add [GlShort],ax

;Compute GlInt += c         (int = dword = 32 bit           =>  I use EAX)
    mov eax,[ebp + 16]
    add [GlInt],eax

;Compute GlLongLong += d    (long long = qword = 64 bit     =>  I use EAX + EDX)
    mov eax,[ebp + 20]
    mov edx,[ebp + 24]
    add dword ptr[GlLongLong],eax
    adc dword ptr[GlLongLong],edx

    pop ebp
    ret

IntegerAddition endp
                end

I am compiling "IntegersExploration.cpp" with:

g++ -Wall -c IntegersExploration.cpp

And I'm compiling "IntegersEx.asm" with:

jwasm -coff IntegersEx.asm

But when I link the 2 ".o" objects I get using:

g++ -m32 IntegersExploration.o IntegersEx.o

I get the following errors:

/usr/bin/ld: i386:x86-64 architecture of input file `IntegersExploration.o' is incompatible with i386 output
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x8): undefined reference to `_GlChar'
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x13): undefined reference to `_GlShort'
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x1c): undefined reference to `_GlInt'
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x28): undefined reference to `_GlLongLong'
/usr/bin/ld: IntegersEx.o:IntegersEx.asm:(.text+0x2e): undefined reference to `_GlLongLong'
/usr/bin/ld: IntegersExploration.o: in function `main':
IntegersExploration.cpp:(.text+0x19): undefined reference to `IntegerAddition'
collect2: error: ld returned 1 exit status

It's like the "asm" file doesn't link properly the extern variables to the "cpp" file. How can I solve this?

Thank you very much!
Have a nice coding!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文