Skip to content

'Sleep' not working when compiled in Win32 (x86) #738

Open
@davidpfister

Description

@davidpfister

Description

The Sleep function does not even compile on Windows using ifort (ia-32 2021.5.0). One gets
fatal error LNK1120: 1 unresolved external
error LNK2019: unresolved external symbol _Sleep

It works fine when building in x64 though.

Expected Behaviour

The code should compile for x86 and x64 platforms.

Version of stdlib

v0.3.0

Platform and Architecture

Windows

Additional Information

After playing around I came up with a solution that works for me.
In x86 the sleep function on Windows should be called Sleep@4.

I changed the code as follows

#ifdef _WIN32
#ifdef _WIN64
        subroutine  winsleep(dwMilliseconds) bind (C, name='Sleep')
            import :: DWORD
            integer(DWORD) :: dwMilliseconds
        end subroutine
#else
        subroutine  winsleep(dwMilliseconds) bind (C, name='Sleep@4')
            import :: DWORD
            integer(DWORD) :: dwMilliseconds
        end subroutine
#endif
#else
        integer(c_int) function usleep(usec) bind (C)
            import c_int
            integer(c_int), value, intent(in) :: usec
        end function
#endif

subroutine sleep(millisec)
        integer, intent(in) :: millisec
        integer(c_int) :: ierr

#ifdef _WIN32
        call winsleep(%val(transfer(millisec, DWORD)))
#else
        ierr = usleep(int(millisec * 1000, c_int))
        if (ierr/=0) error stop 'problem with usleep() system call'
#endif
    end subroutine

and the whole thing works like a charm

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions