简体   繁体   English

如何在 Windows 上使用带有 C++ 的 x64 程序集?

[英]How to use x64 assembly with C++ on windows?

cpp.cpp: cpp.cpp:

#include <iostream>

extern "C" int returnnum();

int main() { std::cout << returnnum() << "\n"; }

asm.asm: asm.asm:

segment .text
global _returnnum
_returnnum:
    mov rax, 420
    ret

First, I compile the assembly file with nasm -f win64 asm.asm .首先,我使用nasm -f win64 asm.asm编译程序集文件。

Then, I compile the C++ file with g++ cpp.cpp asm.obj .然后,我用g++ cpp.cpp asm.obj编译 C++ 文件。

But this gives me an error:但这给了我一个错误:

asm.obj: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status

I know that I can change rax to eax and compile with -f win32 and it'll work, but I want a 64-bit application.我知道我可以将rax更改为eax并使用-f win32进行编译,它会工作,但我想要一个 64 位应用程序。 Is there any way I can do this?有什么办法可以做到这一点吗?

If this is helpful:如果这有帮助:

g++ --version : g++ (MinGW.org GCC-6.3.0-1) 6.3.0 g++ --version : g++ (MinGW.org GCC-6.3.0-1) 6.3.0
nasm --version : NASM version 2.15rc12 compiled on Jun 26 2020 nasm --version : NASM 版本 2.15rc12 编译于 2020 年 6 月 26 日

Based on your information from the comments and your g++ --version output, it looks like you installed plain MinGW (which is purely 32 bit, and works just fine if you only need to build 32 bit executables since Windows supports running 32 bit executables on a 64 bit OS) rather than MinGW-W64 , the fork with native support for 64 bit Windows.根据您的评论信息和您的g++ --version输出,看起来您安装了普通的MinGW (它是纯 32 位的,如果您只需要构建 32 位可执行文件就可以正常工作,因为 Windows 支持运行 32 位可执行文件64 位操作系统)而不是MinGW-W64 ,它是对 64 位 Windows 的本机支持的分支。

You'll need to switch if you want to build true 64 bit executables.如果要构建真正的 64 位可执行文件,则需要进行切换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM