News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Problem with linking to msvcrt.dll

Started by Greenhorn__, March 12, 2010, 10:53:37 PM

Previous topic - Next topic

Greenhorn__

Hi,

I'm new to this Forum and so I wanna take advantage of this situation and say "Hello".

But back to topic. I have a problem with linking to msvcrt.lib/crtdll.lib but only with the functions beginning with an underscore like _strrev.
The other functions without a leading underscore are working fine.

I'm using the Win32Inc and jwasm/jwlink. The Libraries are from VC9.
This is the output from NMAKE:
Microsoft (R) Program Maintenance Utility   Version 1.50
Copyright (c) Microsoft Corp 1988-94. All rights reserved.

WinCUI2.asm: 51 lines, 2 passes, 62 ms, 0 warnings, 0 errors
Error! E2028: __imp___strrev is an undefined reference
file .\WinCUI2.obj(WinCUI2.asm): undefined symbol __imp___strrev
NMAKE : fatal error U1077: 'C:\Windows\system32\cmd.exe' : return code '0x1'


If I call the function _strrev in a C source, using the same libs, all is ok ...

So, please, could someone tell me what's wrong ?

EDIT:
Of course, I have included the necessary headers and linked to the right libraries ... ;)


Regards
Greenhorn__
You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time.
(Abraham Lincoln)

BlackVortex

What headers have you included ?

Post your include directives.

Greenhorn__

Here is the code example ...

;+---------------------------+
;|  use CRT (MSVCRT) in ASM  |
;+---------------------------+

.386
.MODEL FLAT, stdcall
option casemap:none

ifndef __POASM__
.nolist
.nocref
endif       
WIN32_LEAN_AND_MEAN equ 1
include windows.inc
include stdio.inc
include string.inc
ifndef __POASM__       
.cref
.list
endif       

;--- CStr(): macro function to simplify defining a string

CStr macro pszText:REQ
local szText
.const
szText db pszText,0
.code
exitm <offset szText>
endm

.CODE

main proc c

local szBuffer[256]:byte

invoke printf, CStr(<"Hello, world!",10>)
invoke _strrev, addr szBuffer
invoke strstr, addr szBuffer, CStr("alfa")
ret

main endp

mainCRTStartup proc c
call main
invoke ExitProcess, 0
mainCRTStartup endp

end mainCRTStartup


The MAKEFILE ...

# Makefile for NMAKE. Tools to be used:
# either JWASM and WLINK (default)
# or      MASM and MS LINK
# the tools must be found somewhere in PATH.

# to create the binary using JWASM/WLINK, just enter NMAKE
# to create the binary using MASM/MS LINK, enter NMAKE MASM=1

# USEDLL=1 use either MSVCRT.DLL or CRTDLL.DLL
# USEDLL=0 if static CRT is to be used

USEDLL=1

# MASM=1 use MASM, MASM=0 use JWASM

!ifndef MASM
MASM=0
MSLINK=0
!else
MSLINK=1
!endif

NAME=WinCUI2
DEBUG=0
OUTDIR=.

!if $(DEBUG)
LOPTD=/DEBUG:FULL
AOPTD=-Zi
!else
LOPTD=
AOPTD=
!endif

!if $(USEDLL)
#CLIB=msvcrt.lib
CLIB=crtdll.lib
AOPTC=-D_DLL
!else
CLIB=libc.lib
AOPTC=
!endif

ASMOPT= -c -nologo -coff -Sg -Fl$* -Fo$* -I..\..\Include $(AOPTD) $(AOPTC)
!if $(MASM)
ASM = @ml.exe $(ASMOPT)
!else
ASM = @jwasm.exe $(ASMOPT)
!endif

!if $(MSLINK)
LOPT=/LibPath:..\..\Lib /MAP:$*.map /SUBSYSTEM:CONSOLE $(LOPTD)
LIBS=kernel32.lib $(CLIB)
LINK=@link.exe $(LOPT) /OUT:$*.exe $*.obj $(LIBS)
!else
LOPT= LibPath D:\JWASM\Lib op MAP, quiet $(LOPTD)
LIBS=library kernel32.lib,$(CLIB)
LINK=@jwlink.exe system nt file $(OUTDIR)\$(NAME).obj $(LOPT) $(LIBS)
!endif

$(OUTDIR)\$(NAME).exe: $(OUTDIR)\$(NAME).obj makefile
$(LINK)

$(OUTDIR)\$(NAME).obj: $(NAME).asm makefile
$(ASM) $(NAME).asm

CLEAN :
-@erase "$(OUTDIR)\$(NAME).obj"
-@erase "$(OUTDIR)\$(NAME).map"
-@erase "$(OUTDIR)\$(NAME).lst"



Regards
Greenhorn__
You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time.
(Abraham Lincoln)

clive

You need to be using msvcrt.lib for that is where "__imp___strrev" is defined

#CLIB=msvcrt.lib
CLIB=crtdll.lib


You need to move the hash/pound to the other line, for it is a comment, you are currently pulling from crtdll.lib which isn't in the MASM32 package as far as I can tell.

-Clive
It could be a random act of randomness. Those happen a lot as well.

dedndave

for masm, it looks like this...
        include    \masm32\include\msvcrt.inc

        includelib \masm32\lib\msvcrt.lib

should work for JwAsm, also

Greenhorn__

I tried it with both libs and the problem/output is still the same ...

I've attached the .lst and .map files ...

You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time.
(Abraham Lincoln)

hutch--

Greenhorn,

If you are going to use the MSVCRT library and include file from MASM32, the include file has all of the function names prefixed with crt_ so that you don't get naming conflicts with MASM reserve words. If you use the correct names from the include file with the matching library your MSVCRT function calls work fine.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

here is one where i use a couple crt functions
notice, i use
        include \masm32\include\masm32rt.inc
to cover all the includes and includelibs
i think some crt functions require you to balance the stack (i forget)
you will find out soon enough if that is true - lol

drizz

I'm sure it's jwlink problem, try forcing "MSLINK=1".

And use the same path "LOPT=/LibPath:D:\JWASM\Lib ..."
The truth cannot be learned ... it can only be recognized.

jj2007

This works just fine. Note the double understroke in crt__strrev.

include \masm32\include\masm32rt.inc

.data?
szBuffer db 256 dup(?)

.code
start:
invoke crt_printf, chr$("Hello, world!",10)
invoke crt__strrev, addr szBuffer
invoke crt_strstr, addr szBuffer, chr$("alfa")
exit

end start

japheth

Quote from: Greenhorn__ on March 12, 2010, 11:52:58 PM
I tried it with both libs and the problem/output is still the same ...

I've attached the .lst and .map files ...

I looked into this issue and found a potential problem with the options for jwlink. It's most likely much better not to use the SYSTEM directive when using jwlink. The SYSTEM directive depends on the contents of files WLINK.LNK and/or WLSYSTEM.LNK and might behave strange if these files are missing or contain "unexpected" stuff.

It's far better to avoid SYSTEM and use the plain FORMAT directive instead:

#LINK=@jwlink.exe system nt file $(OUTDIR)\$(NAME).obj $(LOPT) $(LIBS)          // OLD
LINK=@jwlink.exe format windows nt file $(OUTDIR)\$(NAME).obj $(LOPT) $(LIBS)  // NEW

I also verified that the MSVCRT.LIB does contain __imp___strrev already. So the object module should link fine with the modified linker parameters.


Greenhorn__

At first, thanks to all for replying.

I tried to (jw)link with the FORMAT directive with the same result.

The hint from japhet to check the msvcrt.lib for __imp___strrev brought to light that in my msvcrt.lib __imp__strrev is valid, but I can't find __imp___strrev !?

Hm, it seems that my msvcrt.lib is not correct, isn't it ? I've taken it from VC9 Xpress, so are these libs limited in use ?
Should I try to create my own msvcrt.lib with dumpbin.exe and lib.exe ... ?

EDIT:
I also checked the msvcrt.lib from the masm32 package and there is __imp___strrev valid and all is working fine. :)
Now, after installing the masm32 package, I can also give a try to the other approaches ...

All systems are up, situation: normal. ;)


Regards
Greenhorn
You can fool some of the people all of the time, and all of the people some of the time, but you can not fool all of the people all of the time.
(Abraham Lincoln)

Vortex

Hi Greenhorn,

You can create your own import libraries with Pelle's library manager Polib.exe :

\masm32\bin\polib.exe /OUT:msvcrt.lib /DEF:msvcrt.def /MACHINE:X86

The def file should look like the following :


LIBRARY msvcrt
EXPORTS
"$I10_OUTPUT"
"AdjustExceptionResult"
"_DllMain@12"
"LoadImm8"
"LoadOperand"
"UpdateResult"
"ValidateResult"
"WinMainCRTStartup"
"XMMI2_FP_Emulation"
"XMMI_FP_Emulation"
"_AdjustLocation"
"_AdjustStack"
"_CIacos"
"_CIasin"
"_CIatan"
"_CIatan2"
"_CIcos"
"_CIcosh"
"_CIexp"
"_CIfmod"
"_CIlog"
"_CIlog10"
"_CIpow"
"_CIsin"
"_CIsinh"
"_CIsqrt"
"_CItan"