Simple build program at visual studio 2017
.386
.model flat, c
.stack 100h
printf PROTO arg1:Ptr Byte
.data
msg1 byte "Hello World!", 0Ah, 0
.code
main proc
INVOKE printf, ADDR msg1
ret
main endp
end main
Hi I am getting the following errors:
I searched and found that someone said it could be fixed by linking the Microsoft Runtime Library
Can anyone teach me how can I fix this?
thank
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol _printf referenced in function _main testing C:\Users\Kin\Desktop\assembly\testing\testing\Source.obj 1
Error LNK1120 1 unresolved externals testing C:\Users\Kin\Desktop\assembly\testing\Debug\testing.exe 1
source to share
I don't have VS 2017 to try this. Important . Make sure you create Console Application
and not Windows Application
. After creating this project, make sure MASM is added to build settings. Add a .ASM
file to your project .
Take your code and paste the following lines at the top:
includelib libcmt.lib
includelib libvcruntime.lib
includelib libucrt.lib
includelib legacy_stdio_definitions.lib
The explanation as to why these lines are needed later in Visual Studio 2013, can be found in this fooobar.com/questions/1615961 / ... .
You want the C runtime to be the entry point to the console application (which in turn will call yours main
). Because of this, you MUST remove main
from the last line that says end main
. When you do end main
, it bypasses the C runtime start up. Improper C runtime initialization will most likely cause the program to crash when making calls like printf
. It should be simple end
, not end main
.
Final code you should check:
includelib libcmt.lib
includelib libvcruntime.lib
includelib libucrt.lib
includelib legacy_stdio_definitions.lib
.386
.model flat, c
.stack 100h
printf PROTO arg1:Ptr Byte
.data
msg1 byte "Hello World!", 0Ah, 0
.code
main proc
INVOKE printf, ADDR msg1
ret
main endp
end
source to share
As of Visual Studio 2015, printf is now "inline" in C code. Assembly code to work around this would be difficult. I ended up including a small C source file with an unused printf call in the project to work around this issue. I don't remember if the generated printf code was parameter dependent. I am simply using the same or more parameters in the source code for printf than what I am using in the assembly code.
source to share