编译C++和乳胶在一起

发布于 2025-01-31 16:17:50 字数 1971 浏览 3 评论 0原文

我正在进行有关 masm ,特别是使用C +++++的项目,和玛斯。

C ++文件的代码是:

#include <stdlib.h>
#include <iostream>

extern "C" void reverser(int *y, const int *x, int n);

int main() {
    using std::cout;
    using std::endl;

    const int n = 10;
    int x[n], y[n];

    for(int i = 0; i < n; i++)
        x[i] = i;

    reverser(y, x, n);

    for(int i = 0x0; i < n; i++) {
        printf("x: %5d     y: %5d", x[i], y[i]);
    }

    return 0;
}

MASM文件的代码是:

.386
.model flat, c
.code

reverser    proc
            push ebp
            mov ebp,esp
            push esi
            push edi        ; Function Prologue

            xor eax,eax
            mov edi,[ebp+8]
            mov esi,[ebp+12]
            mov ecx,[ebp+16]
            test ecx,ecx

            lea esi,[esi+ecx*4-4]
            pushfd
            std

@@:         lodsd
            mov [edi], eax
            add edi,4
            dec ecx
            jnz @B

            popfd
            mov eax,1

            pop edi
            pop esi
            pop ebp

            ret

reverser endp
         end

在在线课程中,使用Visual Studio的人会毫无问题地对其进行编译,但是如果我尝试使用GCC编译.CPP文件,我会得到错误:

未定义的引用'Reverser'

因此我尝试添加

#include "reverser.asm"

到代码中,但是后来我得到了错误:

reverser.asm:1:error: expected unqualified-id before numeric constant
reverser.asm:1:error: expected ',' or ';' before numeric constant
reverser.asm:9:error: 'Prologo' does not name a type
reverser.asm:21:error: stray '@' in program

所以我有以下问题:

  1. 如何解决此问题?
  2. 如何使用命令行在Windows上将两个文件一起编译在一起?喜欢使用 masm.exe gcc.exe 进行编译。
  3. 为什么在课程中没有任何问题进行编译?

I'm doing an exercise of an online course about MASM, specifically a project that uses both C++ and MASM.

The code of the C++ file is this:

#include <stdlib.h>
#include <iostream>

extern "C" void reverser(int *y, const int *x, int n);

int main() {
    using std::cout;
    using std::endl;

    const int n = 10;
    int x[n], y[n];

    for(int i = 0; i < n; i++)
        x[i] = i;

    reverser(y, x, n);

    for(int i = 0x0; i < n; i++) {
        printf("x: %5d     y: %5d", x[i], y[i]);
    }

    return 0;
}

The code of the MASM file is this:

.386
.model flat, c
.code

reverser    proc
            push ebp
            mov ebp,esp
            push esi
            push edi        ; Function Prologue

            xor eax,eax
            mov edi,[ebp+8]
            mov esi,[ebp+12]
            mov ecx,[ebp+16]
            test ecx,ecx

            lea esi,[esi+ecx*4-4]
            pushfd
            std

@@:         lodsd
            mov [edi], eax
            add edi,4
            dec ecx
            jnz @B

            popfd
            mov eax,1

            pop edi
            pop esi
            pop ebp

            ret

reverser endp
         end

In the online course, the person speaking compiles it without any problem using Visual Studio, but if I try to compile the .cpp file using GCC, I get an error:

undefined reference to 'reverser'

So I tried to add

#include "reverser.asm"

to the code, but then I get the errors:

reverser.asm:1:error: expected unqualified-id before numeric constant
reverser.asm:1:error: expected ',' or ';' before numeric constant
reverser.asm:9:error: 'Prologo' does not name a type
reverser.asm:21:error: stray '@' in program

So I have the following questions:

  1. How can I fix this?
  2. How I can compile the two files together on Windows using the command-line? Like using masm.exe and gcc.exe to compile.
  3. Why did it compile without any problem in the course?

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

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

发布评论

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