Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use x64 assembly with C++ on windows?

cpp.cpp:

#include <iostream>

extern "C" int returnnum();

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

asm.asm:

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

First, I compile the assembly file with nasm -f win64 asm.asm.
Then, I compile the C++ file: g++ cpp.cpp asm.obj, but this gives me an error: asm.obj: file not recognized: File format not recognized collect2.exe: error: ld returned 1 exit status

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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. 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
nasm --version: NASM version 2.15rc12 compiled on Jun 26 2020

>Solution :

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.

You’ll need to switch if you want to build true 64 bit executables.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading