22. Building a standalone executable
To build a single executable from your program that you can give to someone else, who runs it without installing Meem.
Explanation
Section titled “Explanation”meem برنامج.مcompiles the program and runs it straight away, for trying things out.meem build برنامج.م -o برنامجwrites an executable:./برنامجruns on its own.- The file is static by default: it contains the whole C library (musl), so it needs nothing from the machine.
--dynamicbuilds it dynamically against the system’s C library (smaller, and sometimes faster in allocation-heavy programs, but it needs glibc on the machine). - The program’s arguments come after
--in a direct run (meem برنامج.م -- سلمى), and after the file name for a built file (./برنامج سلمى).
meem build عداد.م -o عداد./عدادWhen this site was built, the following example really was built, the resulting file was run on its own, and its output matched the output of the direct run:
لكل ع في مدى(1, 4): اطبع("العدّ: ", ع)Also built with meem build when the site was built: a static file of 30.1 kB, run on its own, and its output matched the output above.
Exercise
Section titled “Exercise”A program that greets the name it is given as an argument (نظام.وسائط()), and prints how to use it if it is given no name. Build it, then run it: ./تحية سلمى.
Show the solution
استورد نظاموسائط = نظام.وسائط()اذا طول(وسائط) == 0: اطبع("الاستعمال: تحية <اسم>")غير ذلك: اطبع("أهلاً يا ", وسائط[0], "!")Also built with meem build when the site was built: a static file of 40.1 kB, run on its own, and its output matched the output above.
Common mistakes
Section titled “Common mistakes”- A program that uses
خارجيto call a C library other than the standard C library (such as zlib) cannot be built statically: build it with--dynamic, and the library must be installed on the machine that runs it. - The built file is for Linux x86_64 only. Windows and macOS are not available in 1.0.
See also
Section titled “See also”Building: meem build · Command-line options