1
//main.cpp

#include <stdio.h>
extern "C" void asmFunc();
int main() {
    asmFunc();
    return 0;
}
asm.asm
.data
    var byte "Hello",10
.code
    public asmFunc
    externdef printf:proc

asmFunc proc
    sub  rsp,56
    lea  rcx,var
    call printf
    add  rsp,56
    ret
asmFunc endp
        end

When trying build the project in visual studio There is message said

Unresolved external symbol printf refereenced in function 

And when I call printf before calling asmFunc, the error is gone.

But I want only call printf in assembly


VS Build commands:

C++

/JMC /permissive- /ifcOutput "x64\Debug\" /GS /W3 /Zc:wchar_t /ZI /Gm- /Od /sdl /Fd"x64\Debug\vc143.pdb" /Zc:inline /fp:precise /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MDd /FC /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\assembly.pch" /diagnostics:column 

linker

/OUT:"x64\Debug\assembly.exe" /MANIFEST /NXCOMPAT /PDB:"\x64\Debug\assembly.pdb" /DYNAMICBASE "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /DEBUG /MACHINE:X64 /INCREMENTAL /PGD:"x64\Debug\assembly.pgd" /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /ManifestFile:"x64\Debug\assembly.exe.intermediate.manifest" /LTCGOUT:"x64\Debug\assembly.iobj" /ERRORREPORT:PROMPT /ILK:"x64\Debug\assembly.ilk" /NOLOGO /TLBID:1 
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
abdallaEG
  • 103
  • 1
  • 6
  • 1
    Should not it be `_printf`? – 273K Aug 06 '22 at 19:42
  • Also the same error with _printf – abdallaEG Aug 06 '22 at 19:45
  • 2
    You aren't including the correct library file. printf is in msvcrt.lib. Take a look at [this](https://stackoverflow.com/a/73215879/2189500) for example. – David Wohlferd Aug 06 '22 at 19:56
  • Works for me on default compiler settings. Cannot build the code using provided settings (is not related to problem described). `VS2022 C/C++ Optimizing Compiler Version 19.32.31332 for x64` – Dmytro Ovdiienko Aug 06 '22 at 21:58
  • 1
    @273K: Windows x64 doesn't prefix symbols with `_`. `printf` is correct for x64, `_printf` would be correct for 32-bit x86. (Unless using MASM directives that make it prepend an underscore for you.) – Peter Cordes Aug 06 '22 at 22:35
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Aug 06 '22 at 22:59
  • @PeterCordes Please don't brag about your knowledge. You know that after the post has been edited. I could not know that before the update. – 273K Aug 06 '22 at 23:07
  • 3
    @273K: I wasn't bragging, just setting the record straight that `printf` was correct for this code, to make sure the OP or other future readers would know that wasn't the problem. It's somewhat surprising that the same OS changed naming conventions between similar ISAs, so it deserves some explanation. Before commenting, I hadn't looked at versions of the questions before the one I edited. It's a useful comment for the current state, regardless of history. BTW, revision 1 had the same code as now, using 64-bit registers like RSP and RCX, and using the Win x64 calling convention. – Peter Cordes Aug 06 '22 at 23:25
  • 1
    @273K: Or to put it another way, you asked if it should be `_printf`, which seemed like a reasonable genuine question; some people's experience with asm is limited to 32-bit code, and sometimes from years ago. That's fine, I'm happy to share knowledge, so I answered your question with some facts. I find it super weird that you seem to be unhappy about that. – Peter Cordes Aug 06 '22 at 23:33
  • @DavidWohlferd I did include msvcrt.lib And also the same error – abdallaEG Aug 07 '22 at 09:14
  • @KenWhite I am working on windows not linux – abdallaEG Aug 07 '22 at 09:22

0 Answers0