Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building DLLs #3

Open
mikejohnstn opened this issue Jun 23, 2017 · 4 comments
Open

Building DLLs #3

mikejohnstn opened this issue Jun 23, 2017 · 4 comments

Comments

@mikejohnstn
Copy link

Thanks for all your work helping to bring Swift to Windows.

Do you plan to support building a DLL instead of an executable? This would help a lot for those of us interested in using Swift to write cross-platform libraries.

@tinysun212
Copy link
Member

Thanks for your suggestion.

That's very good feature to support building libraries to share.
I didn't have the building-libraries-plan yet.
Instead, I have the plan porting the Foundation module and the Dispatch module to MinGW-w64 Swift.
I'll take the building-libraries-plan after those portings since Foundation module is very important for cross-platform coding.

@mikejohnstn
Copy link
Author

Agreed, porting Foundation and Dispatch will be great as well. Thanks for your efforts.

@gmondada
Copy link

Hi,
Thanks a lot for your amazing work.
Actually, do you know what is missing to generate a DLL containing swift code? The linker seems to know what a DLL is. You can generate one with:
swiftc hello.swift -o hello.dll -Xlinker -dll -Xlinker --export-all-symbols

Checking the DLL content with Microsoft dumpbin gives this:

$ /cygdrive/c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio\ 14.0/VC/bin/dumpbin.exe /DEPENDENTS /EXPORTS hello.dll
Microsoft (R) COFF/PE Dumper Version 14.00.24213.1
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file hello.dll

File Type: DLL

  Section contains the following exports for hello.dll

    00000000 characteristics
    5955224C time date stamp Thu Jun 29 16:52:44 2017
        0.00 version
           1 ordinal base
           6 number of functions
           6 number of names

    ordinal hint RVA      name

          1    0 00004036 __swift_reflection_version
          2    1 000016F0 get_number
          3    2 000015E0 hello
          4    3 000015B0 main
          5    4 000016E0 swift_rt_swift_release
          6    5 000016D0 swift_rt_swift_retain

  Image has the following dependencies:

    KERNEL32.dll
    msvcrt.dll
    libswiftSwiftOnoneSupport.dll
    libswiftCore.dll

  Summary

        1000 .CRT
        1000 .bss
        1000 .data
        3000 .debug_abbrev
        1000 .debug_aranges
        1000 .debug_frame
       49000 .debug_info
        3000 .debug_line
        3000 .debug_loc
        1000 .debug_ranges
        1000 .debug_str
        1000 .edata
        1000 .idata
        1000 .pdata
        1000 .rdata
        1000 .reloc
        1000 .swift1_autolink_entries
        1000 .swift_modhash
        2000 .text
        1000 .tls
        1000 .xdata

We see get_number and hello symbols corresponding to two functions I wrote in swift:

@_silgen_name("hello") public func hello() {
    print("hello world")
}
@_silgen_name("get_number") public func getNumber() -> Int32 {
    return 1234
}

However, linking the DLL to a program (msvcrt.dll based) makes that program crashing. Loading it with LoadLibrary() returns ERROR_DLL_INIT_FAILED.
I also tried to generate an EXE file which exports all symbols, with:
swiftc hello.swift -o hello.exe -Xlinker --export-all-symbols
and I was able to load the EXE file with LoadLibrary(), get the address of get_number() thanks to GetProcAddress() and call get_number() which returned 1234. Doing the same to call hello() results in a crash.
So, I think we miss the code that should initialise the swift environment or whatever needed to swift to run properly. Do you know how this should work?
I will probably try to call the DLL from a swift program. Maybe in this way the DLL runs in an environment that the main program has already set up. I think this is what happen with libswiftCore.dll which is a DLL written in swift, isn't it?

@WorikQCI
Copy link

WorikQCI commented May 4, 2022

Many years later: Can I compile my Swift code on Windows as a dynamic library (DLL)?

I wish to call it from programme written in a different language

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants