0

I am trying to get a very simple bit of x64 assembly to work using visual studio 2019, but it keeps giving me "fatal error LNK1120: 1 unresolved externals" I made sure masm was checked in build dependencies, and that is was in x64 mode; so, not sure what else to do since this is my very first prod into asm. Any help would be greatly appreciated! Thanks.

#include <iostream>
using namespace std;
extern "C" int someNumber();
int main() {
cout << "Number: " << someNumber() << endl;
return 0;
}

and

.code
someNumber proc
    mov rax, 222
    ret
someNumber endp
end
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Jaws88
  • 21
  • 1
  • 8
  • 1
    What's the rest of the linker error? i.e., what is the mangled name of the symbol it is looking for? You might just need to change `someNumber` in the assembly source to `_someNumber`. – 1201ProgramAlarm Feb 09 '20 at 16:45
  • 1>------ Build started: Project: assemb, Configuration: Debug x64 ------ 1>assemb.cpp 1>assemb.obj : error LNK2019: unresolved external symbol someNumber referenced in function main 1>C:\Users\user\source\repos\assemb\x64\Debug\assemb.exe : fatal error LNK1120: 1 unresolved externals 1>Done building project "assemb.vcxproj" -- FAILED. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== – Jaws88 Feb 09 '20 at 17:02
  • I tried adding the underscore and still a no go, and not sure what you meant by the mangled name of the symbol, but I will look that up and post asap--I am headed out at the moment. Thanks for the response though. – Jaws88 Feb 09 '20 at 17:03
  • 1
    You need to include the .obj file created by the assembler when linking the CPP file. – 1201ProgramAlarm Feb 09 '20 at 17:05
  • https://en.m.wikipedia.org/wiki/Name_mangling – Jesper Juhl Feb 09 '20 at 19:05
  • Did you have a look at https://stackoverflow.com/a/33757749/3512216? The order of the steps is important. – rkhb Feb 09 '20 at 19:17
  • Normally all you have to do is check "masm" under "Project->Build Customizations..." for the project which contains the asm files; that tells MSBuild how to deal with `.asm` files (including linking, debugging, etc). It's possible that the `.asm` file is messed up in the `vcxproj`, in that case right click on the file -> Properties -> General -> Item Type: "Microsoft Macro Assembler". Using a custom build tool can also work, but it's unnecessary and VS won't understand it properly, so debugging won't work for example. – dialer Jul 11 '22 at 17:45

1 Answers1

0

You need to create a custom built tool (or step) for the assembly source file. Right click on the assembly source file name, then properties, ..., custom build tool ... .

For a debug build:

Command Line   ml64 /c /Zi /Fo$(OutDir)\someNumber.obj someNumber.asm
Outputs        $(OutDir)\someNumber.obj

For a release build, the same lines but without the /Zi

Command Line   ml64 /c /Fo$(OutDir)\someNumber.obj someNumber.asm
Outputs        $(OutDir)\someNumber.obj

In someNumber.asm, you need to declare the function as public:

        .code
        public someNumber
someNumber proc
        mov     rax, 222
        ret
someNumber endp
        end

VS2019 assembler ML64 defaults to "C" model, so there is no need to prefix names with "_".

rcgldr
  • 27,407
  • 3
  • 36
  • 61