gkamani2011
gkamani2011

Reputation: 223

Interfacing c++ and asm compile error

I am trying to interface c++ and asm code. c++ calls a function and that function is defined in asm

I tried compiling it and it gave me a few errors. The primary question is, do I need to create separate c++ and asm source files or just one. and if one, which type ? (cpp or asm).

Two errors that i get when I try compiling it as separate source files:

Error1: error A1000: cannot open file : ..\..\..\..\..\Desktop\test.asm
Error2: error MSB3721: The command "ml.exe /c /nologo /Zi /Fo"Debug\test.obj" /W3 /errorReport:prompt  /Ta..\..\..\..\..\Desktop\test.asm" exited with code 1.

This might me really stupid and trivial, but I am stuck. Any ideas?

Upvotes: 1

Views: 2780

Answers (2)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

I'd say if you really need to provide assembly code, then inline it within a usual c++ function definition (e.g. using _asm {} with GCC) and call that one. This will at least serve you well in not getting into trouble with c++ name mangling and ABI contracts at linking stage.

IMHO usually it's not worth trying to optimize at assembly language level. I've seen rare cases of such hand written 'optimizations' that really could compete with a fairly good compilers optimized output.

Premature optimization is the root of all evil -- Edsger Dijkstra

Upvotes: 0

Dweeberly
Dweeberly

Reputation: 4777

It depends on what you are doing, the content of your files and the OS you are using. It looks like you are on Windows using the MS macro assembler. Your "test.asm" file should contain only assembly language. Your first error indicates that your assembly file can't be located (or opened). make sure you have the right file location. Once the asm file is actually assembled successfully it will generate an obj file. You can use this file as you would any obj file. Make sure whatever function is in your assembly it obeys the correct call linkage (for C++). You might want to look at this question how do i properly link asm files to c++?

If you only have a small amount of assembly you can use 'inline assembly'. You can find out more about that here: http://msdn.microsoft.com/en-us/library/4ks26t93(v=vs.110).aspx

Upvotes: 1

Related Questions