Swift - windows - linking original mac dynamic library (.dylib) at runtime

Hello,
is it possible to have windows swift wrapper exe program compiled in windows (allready have) which links shared library compiled on mac (something like LoadLibrary in c++)?
i am able to create (compile) shared library on windows but as .dll output only
my goal is to use mac compiled one and instantiate class from library and call it's public methods from windows application
(mac user should implement desired protocol, create and offer dylib, but windows process manages it;s methods calls)

thank you very much

Is your Mac library compiled as a native macOS dynamic library/framework, or is it cross-compiled into a Windows library? Native Mac executable code (applications, command line tools, frameworks/libraries) cannot not execute in a native Windows environment.

Thank you for your answer,
so, if i understand well, the only way is to force native mac user to cross-compile his code into windows platform and me, as a library windows manager can load library dynamically in my swift exe app at runtime and play with his dynamic library (.dll) as it was native c - compiled windows library?

if so:

  1. is it simple way for mac user to compile code as windows target by changing setting in xcode environment for example or it is necessary to use third-party compiler using cmake or something similar?
  2. on my side of windows - how could i instantiate class of swift library (now as windows dll) at runtime? using unsafe c way or using swift technique but still adopted for windows platform?
    (like LoadLibrary() in c++)

(final goal is integrating several libraries (from different platforms written natively in different languages) and communicate between them by defined interfaces / protocols at windows system)

Thank you

What you're referring to is called "cross-compiling". Like clang, swift does support cross-compiling, but you'll need a bunch of additional materials to make it work: most importantly, the SDK of the target platform.

Xcode supports cross-compiling to iOS/tvOS/watchOS devices, because the SDKs for those targets are bundled together with Xcode. It does not have any particular support to help you cross-compile to other targets (linux, windows, etc).

Generally speaking, obtaining the target SDK is the most challenging part of cross-compiling. For open-source platforms such as linux, it's relatively straightforward, but for proprietary platforms such as Windows or macOS, the terms and conditions generally prohibit you from redistributing the SDK. There may be other restrictions, too - like they may say that you can only use the SDK on Windows, or on an Apple computer, or whatever. In any case, the main difficulty is legal, not technical.

For Windows, Microsoft actually provides virtual machine images, which you (or your users) can download for free. You can install Swift in that VM and use it to build your projects on Windows. Overall, it's probably even easier than cross-compiling, and it can offer a better debugging experience.

EDIT: The terms and conditions for those VMs say:

  1. INSTALLATION AND USE RIGHTS.
    ...
    c. You may use the software in the virtual hard disk image only to demonstrate and internally evaluate it. You may not use the software for commercial purposes. You may not use the software in a live operating environment.

What exactly does "demonstrate and internally evaluate" mean? What is a "live operating environment"? My guess is that you can use it to check that your project works, but if you actually want to distribute a built product, you might need to buy a Windows license :man_shrugging:. It’s hard to say, and I'm not a lawyer. Again, the main challenges here are all legal, not technical. There are plenty of technical solutions.

1 Like

really sad news :slightly_frowning_face:

i wanted to user have all native luxury of his environment
programming and compiling in VM (windows) was the last backup solution for me, but not so cool already
and rights - it never occurred to me as a potential problem

thank you for your time