4

Got stuck with Visual Studio, Assembly and C++

C++:

extern "C" void asm_calculate_reals();
int main()
{
   asm_calculate_reals();
   return 0;
}

Assembly:

PUBLIC _asm_calculate_reals
.386
.model flat, stdcall
option casemap :none
.stack 100h
.code
_asm_calculate_reals PROC
   ;code goes here
   ret
_asm_calculate_reals ENDP
end

When I build my project, Visual Studio reports the following error:

error LNK1120 error lnk1120 1 unresolved externals

I actually can't understand what's wrong with this easy part of program.

Log file:

Build started 16.04.2015 16:53:21.
     1>Project "somepath\Reals loop 3variants.vcxproj" in node 2 (targets Build).
     1>Link:
         somepath\link.exe /ERRORREPORT:PROMPT /OUT:"somepath\Debug\Reals loop 3variants.exe" /INCREMENTAL /NOLOGO somelibs /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"somepath\Debug\Reals loop 3variants.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"somepath\Debug\Reals loop 3variants.lib" /MACHINE:X86 /SAFESEH:NO Debug\main.obj
         Debug\reals.obj
     1>main.obj : error LNK2019: reference to the unresolved external symbol _asm_calculate_reals in function _main
     1>somepath\Debug\Reals loop 3variants.exe : fatal error LNK1120: unresolved identifiers: 1
     1>Building project "somepath\Reals loop 3variants.vcxproj" finished (targets Build) with errors.

Build failed.
Olexiy Pyvovarov
  • 870
  • 2
  • 17
  • 32
  • 2
    Use `extern "C"` to turn off name mangling and make sure the leading underscore is expected. Obviously also double check that you are actually linking against the object file created from the assembly ;) – Jester Apr 16 '15 at 13:14
  • @Jester, extern "C" doesn't solve the problem, well maybe something could be wrong with project properties? – Olexiy Pyvovarov Apr 16 '15 at 13:18
  • What version of VC++? AFAIK, they don't support assembly calls after VC++ 2010. – SChepurin Apr 16 '15 at 13:31
  • @SChepurin, VC++ 2013 – Olexiy Pyvovarov Apr 16 '15 at 13:35
  • Correction - Only for AMD64 and Itanium targets. - See "Why does MSVC not support inline assembly for AMD64 and Itanium targets?"(http://stackoverflow.com/questions/1295452/why-does-msvc-not-support-inline-assembly-for-amd64-and-itanium-targets) – SChepurin Apr 16 '15 at 13:37
  • @SChepurin, well the code that i try to execute is working perfectly in inline assembler, i can't just put this code into asm file and call this procedure... – Olexiy Pyvovarov Apr 16 '15 at 13:40
  • @Olexiy Pyvovarov - So, the problem with the project configuration under VC++. - "Visual C++ does not compile assembly source code. It must call an external program named ml.exe to perform the compilation. The commandline must be added to Custom Build options of the Project Settings." – SChepurin Apr 16 '15 at 13:48
  • Check this link - http://stackoverflow.com/questions/20326262/error-lnk2019-unresolved-external-symbol-referenced-in-function-main – SChepurin Apr 16 '15 at 14:50
  • @SChepurin, still no result using your link, made everything, but nothing has helped, it's a nuisance. – Olexiy Pyvovarov Apr 16 '15 at 16:42
  • I'm just in shock. I have no words. The whole problem in one line. VS does not understand the layout model stdсall ... and that was the problem ... – Olexiy Pyvovarov Apr 16 '15 at 17:10
  • 1
    In addition to Olexiy Pyvovarov's answer, you should probably remove the .stack directive, as Visual Studio will create a default stack since there is also a C or C++ source code file as part of the project. – rcgldr Mar 25 '17 at 13:57

2 Answers2

1

The actual problem was in the model definition inside assembly source.

.model flat, stdcall - was when the error appears.

.model flat, c - changing to this, works as expected.

Olexiy Pyvovarov
  • 870
  • 2
  • 17
  • 32
0

Try to add this line to assembly code - global _asm_calculate_reals - To make it visible to linker. The "global" keyword in assembly tells the assembler to make the label visible from outside the file.

SChepurin
  • 1,814
  • 25
  • 17