1

test platform is windows 32bit.

So basically I want to assembly + link a snippet of assembly code in these commands:

nasm -fwin32 test.s
cl test.obj /link msvcrt.lib 

It says :

error LNK2001: unresolved external symbol printf

In my code I do have function call like this:

call printf

So I changed all of these into

call _printf

and it works.

I am not familiar with programming on windows, but is there any way to resolve the external symbol of printf ?

Because I am doing some automatically transform task, and transformation of all the function calls with _ begin should be very tedious...

Can anyone give me some help..? Thank you!

lllllllllllll
  • 8,519
  • 9
  • 45
  • 80
  • This article on calling conventions might be of use to you: http://msdn.microsoft.com/en-us/library/k2b2ssfy.aspx – HerrJoebob Jan 09 '14 at 19:03

1 Answers1

3

MSVCRT, like everything compiled with Visual C++, exports cdecl functions using an underscore prefix. For your own libraries, you can override this behaviour, but since MSVCRT is not your library, you can't change that.

You really are going to have to make your assembly calls use the underscore name. But nasm has an option, --prefix, which makes this easier: --prefix _. (Thanks to Frank Kotler for mentioning this.)

Community
  • 1
  • 1
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 2
    Nasm has a feature... one of the few options that require two hyphens: `--prefix _` that will put an underscore on anything declared `global` or `extern` if that helps. – Frank Kotler Jan 09 '14 at 19:30
  • @Frank Nice, thanks for that info. Do you want me to fold that into my answer? – C. K. Young Jan 09 '14 at 19:31
  • 2
    Sure! Go for it! Might as well note that for OpenWatcom: `--postfix _` - yeah, OpenWatcom uses a trailing underscore... – Frank Kotler Jan 09 '14 at 19:34