{"id":15392626,"url":"https://github.com/jackdbd/zig-demos","last_synced_at":"2026-04-19T08:31:46.794Z","repository":{"id":227946790,"uuid":"629986302","full_name":"jackdbd/zig-demos","owner":"jackdbd","description":null,"archived":false,"fork":false,"pushed_at":"2023-04-19T12:37:16.000Z","size":295,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-17T21:29:37.134Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jackdbd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-19T12:36:48.000Z","updated_at":"2023-04-19T12:37:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"f5089cbb-5dc7-4212-be11-dd690260d86f","html_url":"https://github.com/jackdbd/zig-demos","commit_stats":{"total_commits":2,"total_committers":1,"mean_commits":2.0,"dds":0.0,"last_synced_commit":"6506dacc9ca97dc0484a2712c6182ea692832e59"},"previous_names":["jackdbd/zig-demos"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdbd%2Fzig-demos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdbd%2Fzig-demos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdbd%2Fzig-demos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jackdbd%2Fzig-demos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jackdbd","download_url":"https://codeload.github.com/jackdbd/zig-demos/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242988012,"owners_count":20217534,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T15:15:25.499Z","updated_at":"2026-04-19T08:31:41.770Z","avatar_url":"https://github.com/jackdbd.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Zig demos\n\nThis repository contains a collection of small Zig programs to help you developing some familiarity with the language, its toolchain and its ecosystem.\n\nYou can download a release of the zig compiler/toolchain/CLI from [here](https://ziglang.org/download/).\n\nIn alternative, download a release of the [zigup](https://github.com/marler8997/zigup) tool from [here](https://github.com/marler8997/zigup/releases) and use it to manage multiple versions of the Zig compiler.\n\n```sh\n# fetch a zig compiler from https://ziglang.org/download/\nzigup fetch master\n\n# print the list of all zig compilers installed on your machine\nzigup list\n\n# set the default zig compiler\nzigup default 0.11.0-dev.2401+348751462\n\n# double-check your default zig compiler\nzig version\n```\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n## 📚 Table of Contents\n\n- [1. Hello world for Linux, Windows and macOs](#1-hello-world-for-linux-windows-and-macos)\n  - [Compile for Linux](#compile-for-linux)\n  - [Debug vs ReleaseSmall](#debug-vs-releasesmall)\n  - [Cross-compile for Windows](#cross-compile-for-windows)\n  - [Cross-compile for macOs](#cross-compile-for-macos)\n- [Other](#other)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## 1. Hello world for Linux, Windows and macOs\n\n### Compile for Linux\n\nHere is how you can compile a Zig program:\n\n```sh\nzig build-exe --name hello-linux-musl \\\n  -target x86_64-linux \\\n  hello.zig\n```\n\n```sh\nzig build-exe --name hello-linux-glibc \\\n  -target x86_64-linux-gnu \\\n  hello.zig\n```\n\nThe Zig compiler has 4 build modes:\n\n1. Debug (default)\n1. ReleaseFast\n1. ReleaseSafe\n1. ReleaseSmall\n\nMy laptop has a [x86_64](https://en.wikipedia.org/wiki/X86-64) CPU and runs Xubuntu Linux. Given the CPU architecture and operating system of my machine, and the Zig compiler defaults, `zig build-exe` will generate a [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) 64-bit **object file** containing debug symbols, and a ELF 64-bit **statically-linked executable**.\n\n```sh\n.\n├── hello\n├── hello.o\n├── hello.zig\n└── README.md\n```\n\nWe can use `file hello.o` to verify that the object file was not stripped of debug symbols:\n\n```sh\nhello.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), with debug_info, not stripped\n```\n\nWe can also verify this using `objdump`. We will see a long list of debug symbols contained in the symbol table:\n\n```sh\nobjdump hello.o --syms # or -t\n```\n\nBut no dynamic symbols:\n\n```sh\nobjdump hello.o --dynamic-syms # or -T\n```\n\nWe can use `file hello` to verify that the executable is statically-linked:\n\n```sh\nhello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped\n```\n\nAs an alternative, we can use `ldd hello`, which will print:\n\n```sh\nnot a dynamic executable\n```\n\nOr even run the program using `ltrace -c ./hello`, which will output:\n\n```sh\nCouldn't find .dynsym or .dynstr in \"/proc/237133/exe\"\n% time     seconds  usecs/call     calls      function\n------ ----------- ----------- --------- --------------------\n------ ----------- ----------- --------- --------------------\n100.00    0.000000                     0 total\nHello, world!\n```\n\n### Debug vs ReleaseSmall\n\nZig binaries can be tiny, if compiled in `ReleaseSmall` mode. As a comparison, let's compile the same program in `Debug` mode and `ReleaseSmall` mode and give the executables different names to distinguish them:\n\n```sh\nzig build-exe -O Debug        --name hello-debug hello.zig\nzig build-exe -O ReleaseSmall --name hello-small hello.zig\n```\n\nIn this case, the executable compiled using `ReleaseSmall` (8.5K) is almost 10 times smaller than the one compiled using `Debug` (731K):\n\n```sh\n-rwxrwxr-x 1 jack jack 731K apr  7 18:17 hello-debug\n-rw-rw-r-- 1 jack jack 1,1M apr  7 18:17 hello-debug.o\n-rwxrwxr-x 1 jack jack 8,5K apr  7 18:17 hello-small\n-rw-rw-r-- 1 jack jack  17K apr  7 18:17 hello-small.o\n-rw-rw-r-- 1 jack jack   97 apr  7 17:12 hello.zig\n-rw-rw-r-- 1 jack jack 2,2K apr  7 18:18 README.md\n```\n\nWe can use the `size` utility to check the sizes of the different sections of the ELF files.\n\nThis the ELF file generated when compiling in `Debug` mode:\n\n```sh\n$ size --format=sysv hello-debug\n\nhello-debug  :\nsection             size      addr\n.rodata            36860   2097664\n.text             290177   2138624\n.tbss                 16   2428808\n.got                   8   2432904\n.bss               12680   2437120\n.debug_loc         27088         0\n.debug_abbrev        762         0\n.debug_info        92410         0\n.debug_ranges       3712         0\n.debug_str         80541         0\n.debug_pubnames    30125         0\n.debug_pubtypes    11112         0\n.debug_frame       35000         0\n.debug_line        80350         0\n.comment              19         0\nTotal             700860\n```\n\nThis the ELF file generated when compiling in `ReleaseSmall` mode:\n\n```sh\n$ size --format=sysv hello-small\n\nhello-small  :\nsection     size      addr\n.rodata     2376   2097552\n.text       2978   2104024\n.tbss         13   2107008\n.bss       12560   2113536\n.comment      19         0\nTotal      17946\n```\n\n### Cross-compile for Windows\n\nZig can produce binaries for a variety of architectures, operating systems, implementations of the C standard library, etc. You can list all supported compilation targets using `zig targets`. Since this command returns a JSON, a handy way to view it in a terminal is to use a terminal JSON viewer like [fx](https://github.com/antonmedv/fx):\n\n```sh\nzig targets | fx\n```\n\nIn most languages, cross compiling requires a separate toolchain. In Zig it's [first class](https://ziglang.org/learn/overview/#cross-compiling-is-a-first-class-use-case). Here is how to cross-compile `hello.zig` for Windows.\n\n```sh\nzig build-exe --name hello-windows \\\n  -target x86_64-windows -O ReleaseSmall \\\n  hello.zig\n```\n\nTo test it out from a Linux computer, we can use [Wine](https://www.winehq.org/):\n\n```sh\nwine hello-windows.exe\n```\n\n### Cross-compile for macOs\n\nZig can cross-compile for macOS, both for x64 and for ARM64, thanks to its [Mach-O](https://en.wikipedia.org/wiki/Mach-O) linker:\n\n```sh\nzig build-exe --name hello-macos \\\n  -target x86_64-macos -O ReleaseSmall \\\n  hello.zig\n```\n\nFor more details, see [Mach-O linker in Zig: linking in the era of Apple Silicon](https://archive.fosdem.org/2021/schedule/event/zig_macho/).\n\n## Other\n\nRun this command to generate the table of contents in the README:\n\n```sh\ndoctoc --github README.md --title \"## 📚 Table of Contents\"\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjackdbd%2Fzig-demos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjackdbd%2Fzig-demos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjackdbd%2Fzig-demos/lists"}