Static linking on Linux
I am happy to announce that with the release of Swift 5.3.1 statically linking the Swift stdlib components is now fully supported on Linux. This includes linking against Dispatch and the different Foundation components. Additionally building self-contained binaries is now possible by manually adding a few linker flags.
How to use it?
The Swift compiler supports two different static linking modes:
static-stdlib
The -static-stdlib flag tells the Swift compiler to only …
I appreciate this article is regarding linux, but I assume this support was once available on macos, until M1 came along is that correct?
swift build -c release -Xswiftc -static-stdlib
error: -static-stdlib is no longer supported for Apple platforms
I would like to install a released product into a suitable bin folder. Something like:
install .build/release/<product> /usr/local/bin/<product>
I can build successfully by simply calling
swift build
...but the above install command doesn't work on the generated .product
files found in
.build/arm64-apple-macosx/release/
Jon_Shier
(Jon Shier)
December 24, 2021, 5:35am
2
No, Apple platforms don't support static linking because the Swift runtime is included with the OS, so it must always be dynamically loaded.
I don't use install
or do anything special, I simply copy my executable to the appropriate directory.
Thanks for the reply.
Where is the executable located?
The contents of .build/arm64-apple-macosx/release
has a file named <name>.product
.
Jon_Shier
(Jon Shier)
December 24, 2021, 5:45am
4
My executable product is in the release
directory. I copy that wherever needed.
Jon_Shier
(Jon Shier)
December 24, 2021, 5:52am
5
I've found that if I make a universal binary it's put in .build/apple/Products/Release
.
That directory doesn't exist on my machine
Jon_Shier
(Jon Shier)
December 24, 2021, 6:04am
7
Universal binaries are only made if you explicitly pass both archs. swift build -c release --arch arm64 --arch x86_64
1 Like
jonprescott
(Jonathan Prescott)
December 24, 2021, 3:30pm
8
I think it was turned into an error when MacOS started shipping the Swift libraries as part of the system as dynamic libraries which was MacOS 10.12. But, I'm not sure how this question relates to the install question. It looks like you have the wrong source path in the install
command.