1

I was trying to follow up a tutorial on mixing c++ and x86_assembly with masm on visual studio

I have done all the steps after the instructor and the same steps found repeated here all the steps mentioned is done except for step 8 8) Change the Active solution platform to x64

After Doing all the above steps on trying to build the solution it gives about 18 errors of LNK2019

The error is resolved only when I remove the asm file from the solution

here is my c++ code

// CalculatreSum.cpp : This file contains the 'main' function. Program execution begins and ends there.

#include <iostream>

extern "C" int AdderAsm(int, int, int);
int AdderCpp(int, int, int);

int main() {
    int sum;
    sum = AdderCpp(4, 5, 6);
    printf("Sum from AdderCpp is: %d\n", sum);
    sum = AdderAsm(4, 5, 6);
    printf("Sum from AdderAsm is: %d\n", sum);
}

int AdderCpp(int a, int b, int c) {
    return a + b + c;
}

and here is my x86 assembly one

.386
.model flat, c

.code 

AdderAsm proc
    push    ebp;
    mov     ebp, esp;

    mov     eax, [ebp + 8];
    mov     ebx, [ebp + 12];
    mov     ecx, [ebp + 16];

    add     eax, ebx;
    add     eax, ecx;

    pop     ebp;

    ret

AdderAsm    ENDP
END         AdderAsm

[EDIT] The Error messages in the log

17 unresolved externals C:\Users\mahmo\source\repos\CalculatreSum\Debug\CalculatreSum.exe   1   
unresolved external symbol _strcat_s referenced in function "void __cdecl _RTC_StackFailure(void *,char const *)" (?_RTC_StackFailure@@YAXPAXPBD@Z) C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(error.obj)  1   
unresolved external symbol _strcpy_s referenced in function "void __cdecl _RTC_StackFailure(void *,char const *)" (?_RTC_StackFailure@@YAXPAXPBD@Z) C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(error.obj)  1   
unresolved external symbol _terminate referenced in function __except_handler4_noexcept C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(chandler4_noexcept.obj) 1   
unresolved external symbol _wcscpy_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned int)" (?GetPdbDllPathFromFilePath@@YAHPB_WPA_WI@Z)    C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(pdblkup.obj)    1   
unresolved external symbol __CrtDbgReport referenced in function __CRT_RTC_INIT C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(init.obj)   1   
unresolved external symbol __CrtDbgReportW referenced in function __CRT_RTC_INITW   C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(init.obj)   1   
unresolved external symbol __except_handler4_common referenced in function __except_handler4    C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(chandler4gs.obj)    1   
unresolved external symbol __imp____acrt_iob_func referenced in function _printf    C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\CalculatreSum.obj   1   
unresolved external symbol __imp____stdio_common_vfprintf referenced in function __vfprintf_l   C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\CalculatreSum.obj   1   
unresolved external symbol __wmakepath_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned int)" (?GetPdbDllPathFromFilePath@@YAHPB_WPA_WI@Z)    C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(pdblkup.obj)    1   
unresolved external symbol __wsplitpath_s referenced in function "int __cdecl GetPdbDllPathFromFilePath(wchar_t const *,wchar_t *,unsigned int)" (?GetPdbDllPathFromFilePath@@YAHPB_WPA_WI@Z)   C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(pdblkup.obj)    1   
unresolved external symbol ___current_exception referenced in function __except_handler4_noexcept   C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(chandler4_noexcept.obj) 1   
unresolved external symbol ___current_exception_context referenced in function __except_handler4_noexcept   C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(chandler4_noexcept.obj) 1   
unresolved external symbol ___stdio_common_vsprintf_s referenced in function __vsprintf_s_l C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(error.obj)  1   
unresolved external symbol ___vcrt_GetModuleFileNameW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPAUHINSTANCE__@@XZ)  C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(pdblkup.obj)    1   
unresolved external symbol ___vcrt_GetModuleHandleW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPAUHINSTANCE__@@XZ)    C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(pdblkup.obj)    1   
unresolved external symbol ___vcrt_LoadLibraryExW referenced in function "struct HINSTANCE__ * __cdecl GetPdbDll(void)" (?GetPdbDll@@YAPAUHINSTANCE__@@XZ)  C:\Users\mahmo\source\repos\CalculatreSum\CalculatreSum\MSVCRTD.lib(pdblkup.obj)    1   


Susano
  • 230
  • 2
  • 9
  • 1
    Oh, according to [Naming convention for labels, functions](https://stackoverflow.com/a/41541008), `.model flat,c` decorates with a leading `_` for you. So maybe you have some other link error. But you didn't quote them so we don't know what they are / what problem we're actually trying to find / fix. So the question should have been closed as lacking [mcve] (error message details), rather than duplicate, but it should still be closed until those details are added. – Peter Cordes May 11 '21 at 02:51
  • @beothunder: that phrasing was a bit confusing, but they *are* building as 32-bit, which presumably is the default so they *didn't* switch the file or project to x64. If they tried to assemble this source file as x64, you're right the `push ebp` wouldn't assemble, so you'd have some `A...` errors and not get to the stage of seeing LNK errors. – Peter Cordes May 11 '21 at 02:54
  • @PeterCordes Alright I copied the linking errors from the log into the Question – Susano May 11 '21 at 18:44
  • You call `printf`, but you didn't include `` (And you include `` for no apparent reason; that might be pulling in some dependencies you don't need?). IDK what Visual Studio's defaults are for linking libraries with C++/asm projects, but that seems odd. – Peter Cordes May 11 '21 at 18:51
  • @PeterCordes It's just the same code the instructor wrote and it's compiling and running perfectly when I remove the asm file so that's not the problem obviously – Susano May 11 '21 at 18:58

1 Answers1

0

Right-click the assembly file and go to properties. From there change Item Type to "Microsoft Macro Assembler"

enter image description here

JVApen
  • 11,008
  • 5
  • 31
  • 67
x2da
  • 1