英文:
How to pack files into one executable file for Linux and Windows?
问题
我正在使用Golang和Muon UI(使用Ultralight而不是Chromium)创建一个桌面应用程序,并为Linux和Windows交叉构建我的应用程序。目前应用程序工作正常,但需要Ultralight库(.dll文件用于Windows,.so文件用于Linux)。但是我想将我的应用程序分发为单个可执行文件。我该如何创建两个可执行文件?第一个文件用于Linux,应包含Linux的主可执行文件和仅包含*.so库文件。第二个文件应包含Windows的主可执行文件和仅包含*.dll库文件。我该如何做到这一点?
是否有任何命令行工具可以实现这一点?(例如在Docker中使用gitlab CI)或者我可以通过Golang来实现这一点(例如使用embed包)。我可以将库嵌入到可执行文件中,以便它可以运行吗?
或者我可以使用cgo将动态库链接为静态库并嵌入到二进制文件中吗?
英文:
I'm creating an desktop app on Golang with Muon UI (using Ultralight instead of Chromium) and cross-build my app for Linux and Windows. For now the app work fine but it required Ultralight libraries (*.dll for Windows and *.so for Linux). But I wanna distribution my app as single executable file. How I can create two executable files? First file for Linux, it's should include main executable file for Linux and only *.so libraries. And second file should include main executable file for Windows and only *.dll libraries. How I can to do this?
Are there any CLI utils for this? (for using in gitlab CI inside Docker for example) Or maybe I can to do this via Golang (for example using embed package. Can I embedded libraries into exe file, that it is can run)?
Or can I use cgo for link dynamic libs as static into binary file?
答案1
得分: 2
诚实的回答是:“非常困难,需要付出很多痛苦、流血和眼泪。”
稍长一点的回答是,预编译的 DLL/.so 可能包含的不仅仅是一个静态库。将 DLL/.so 转换为静态库是否可能?有点可能。这涉及将其内容转储到目标文件中,还原所有的重定位项,可能还涉及处理版本化符号和弱符号。不,目前没有可以在可执行二进制级别上为您完成所有这些工作的工具。
如果您只限于 Linux,您可以考虑使用 Flatpak。它将所有内容打包成一种“自解压存档”,在启动时会自动将其解压到一个临时挂载点(在系统的其他部分看不到)。
现在,一个选择是自己构建程序的所有依赖项,并将这些构建设置为静态库。在这种情况下,您将不再处理 DLL。然而,有些库不希望以静态链接方式构建,所以在这方面可能会有所不同。
说实话:为什么分发多个文件会成为问题呢?在 Linux/*BSD 上,您必须分别提供图标和 .desktop
文件,以便在桌面应用程序菜单中显示。是的,如果我们可以将所有这些信息放入一个特殊的只读部分(比如称为 .xdgdata
),并使用一些众所周知的符号名称,那样我们就可以拥有真正的单文件可分发的可执行文件,那将是很好的。
我诚实的建议是:不要太过担心。只需将所有文件一起分发,不要太在意“看起来如何”。
英文:
The honest answer would be: "With great difficulty, lots of pain, blood and tears."
The somewhat longer answer is, that a precompiled DLL/.so may contain slightly more than a mere static library. It it possible to "convert" a DLL/.so into a static library? Somewhat. It boils down to dumping its contents into object files, reverting all the relocation entries, possibly dealing with versioned symbols and weak symbols. No, there are no kitchen sink utilities out there, doing all that for you on an executable binary level.
If you can limit yourself to Linux, you may want to look into Flatpak. What this does is wrapping everything up into a sort of "self extracting archive", which upon launch will transparently and invisibly unpack itself into an in-situ temporary mount point (which you won't see from the rest of the system).
Now, one option would be to build all the dependencies of your program yourself, and arranging for those builds to be created as static libraries. In that case you're no longer dealing with DLLs. However some libraries do not want to be built for static linking, so your mileage may vary there.
Truth to be told: Why is distributing multiple files any issue at all? On Linux/*BSD you must ship separate icon and .desktop
files anyway, so that stuff shows up in the Desktop application menus. Yes, it'd be nice if instead of dealing with XDG desktop entry files we had the option to place all of that information into a special – let's call it .xdgdata
– readonly section, with some well known symbol names, so that we could have truly single file distributable executables.
My honest suggestion: Don't sweat about it. Just ship the whole bunch of files and don't worry too much about "how this looks".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论