Calling the standard C library function from asm in Visual Studio

I have a problem calling a C function from an asm project created in visual studio (Win10 x64, Visual Studio 2015). The project consists of a single asm file:

.586
.model flat, stdcall
option casemap:none
includelib msvcrt.lib

ExitProcess PROTO return:DWORD
extern printf:near

.data
text BYTE "Text", 0

.code
main PROC
    push offset text
    call printf
    add esp,4
    invoke ExitProcess,0
main ENDP
end main

When I create the project, the linker generates an error:

Error LNK2019 unresolved external symbol _printf referenced in function _main @ 0

Linker output parameters:

/OUT: "C:\Users\\Documents\SP_Lab7\Debug\SP_Lab7_Demo.exe" /: NO/NXCOMPAT /PDB: "C:\Users\apple\Documents\SP_Lab7\Debug\SP_Lab7_Demo.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" /: X86/SAFESEH: /: /PGD: "C:\Users\apple\Documents\SP_Lab7\Debug\SP_Lab7_Demo.pgd" /SUBSYSTEM: WINDOWS/MANIFESTUAC: "level = 'asInvoker' uiAccess = 'false'" /ManifestFile: "Debug\SP_Lab7_Demo.exe.intermediate.manifest" /ERRORREPORT: PROMPT/NOLOGO/TLBID: 1

call print, ( Windows API). C asm cpp, <cstdio>? ?

+4
3

Microsoft C- VS 2015. C ( C). Microsoft , legacy_stdio_definitions.lib legacy_stdio_wide_specifiers.lib, Visual Studio 2013 C.

: Project; Properties...; Configuration Properties/General Platform Toolset Visual Studio 2013 (v120)

+8

, Visual Studio 2015 .

  • : libcmt.lib, libvcruntime.lib, libucrt.lib, legacy_stdio_definitions.lib. includelib .
  • C main, PROC C
  • ( ) end main, end. .
  • ExitProcess , EAX ret. C main .

:

.586
.model flat, stdcall
option casemap:none

includelib libcmt.lib
includelib libvcruntime.lib
includelib libucrt.lib
includelib legacy_stdio_definitions.lib

ExitProcess PROTO return:DWORD
extern printf:NEAR

.data
text BYTE "Text", 0

.code
main PROC C                    ; Specify "C" calling convention
    push offset text
    call printf
    add  esp, 4
;   invoke ExitProcess,0       ; Since the C library called main (this function)
                               ; we can set eax to 0 and use ret`to have
                               ; the C runtime close down and return our error
                               ; code instead of invoking ExitProcess
    mov eax, 0
    ret
main ENDP
end                            ; Use `end` on a line by itself
                               ; We don't want to use `end main` as that would
                               ; make this function our program entry point
                               ; effectively skipping by the C runtime initialization
+7

C, C. , , , C . C, WCRT library.

, , - .

, , Windows API, WriteConsole.

+1

Source: https://habr.com/ru/post/1615961/


All Articles