|
| 1 | +[](https://www.python.org) |
| 2 | +[](https://github.com/psf/black) |
| 3 | + |
| 4 | +[](https://github.com/noprd/example-zig/actions/workflows/manual.yaml) |
| 5 | +[](https://github.com/noprd/example-zig/actions/workflows/manual.yaml) |
| 6 | + |
| 7 | +[](https://github.com/noprd/example-zig/actions/workflows/auto.yaml) |
| 8 | +[](https://github.com/noprd/example-zig/actions/workflows/auto.yaml) |
| 9 | + |
| 10 | +# Example Zig # |
| 11 | + |
| 12 | +This repository provides a simple Hello-World example for the [zig]() language, |
| 13 | +which in recent times has become quite a hot option |
| 14 | +as a simpler alternative to Rust and C++. |
| 15 | + |
| 16 | +## Installation of Zig ## |
| 17 | + |
| 18 | +- Unpack an appropriate distribution from <https://ziglang.org/download> to an appropriate location on your machine. |
| 19 | + |
| 20 | +- Add the directory in which the zig-binary lies to your system `PATH`: |
| 21 | + |
| 22 | + ```bash |
| 23 | + touch ${HOME}/.bash_profile # ensures this exists |
| 24 | + echo "# path for zig" >> ${HOME}/.bash_profile |
| 25 | + echo "export PATH=\"/path/to/your zig binaries:\${PATH}\";" >> ${HOME}/.bash_profile |
| 26 | + ``` |
| 27 | + |
| 28 | + for linux. |
| 29 | + Or edit the file directly, adding the lines |
| 30 | + |
| 31 | + ```bash |
| 32 | + ... |
| 33 | + # path for zig |
| 34 | + export PATH="/path/to/your zig binaries:${PATH}"; |
| 35 | + ``` |
| 36 | + |
| 37 | + For windows, do this via the system environment variables (which requires admin access); |
| 38 | + or just install [bash for windows](https://gitforwindows.org) |
| 39 | + and simply perform the above. |
| 40 | + |
| 41 | +- Clone the **ZLS** (_zig langauge server_) repository <https://github.com/zigtools/zls> and run |
| 42 | + |
| 43 | + ```bash |
| 44 | + zig build |
| 45 | + ``` |
| 46 | + |
| 47 | + This will generate a folder **zig-out/bin**. |
| 48 | + Copy just the binary from here to the same path where your zig-compiler binary is stored. |
| 49 | + (Otherwise choose your own location and add this to `PATH`.) |
| 50 | + |
| 51 | +### Intellisense ### |
| 52 | + |
| 53 | +If you are using VSCode, install the [Zig Language](https://marketplace.visualstudio.com/items?itemName=ziglang.vscode-zig) extension. |
| 54 | +For this to work, it needs to detect your **ZLS** binary. |
| 55 | + |
| 56 | +Open the _User Settings JSON_ (e.g. via command palette) |
| 57 | +and add the following attributes to the dictionary: |
| 58 | + |
| 59 | +```js |
| 60 | +{ |
| 61 | + ... |
| 62 | + // NOTE: remove the .exe extension for windows |
| 63 | + "zig.path": "/path/to/your zig binaries/zig.exe", |
| 64 | + "zig.zls.zigLibPath": "/path/to/your zig binaries/lib", |
| 65 | + "zig.zls.path": "/path/to/your ZLS binaries/zls.exe", |
| 66 | + // optional |
| 67 | + "zig.zls.buildOnSaveStep": "", |
| 68 | + "zig.checkForUpdate": false, |
| 69 | + "zig.zls.checkForUpdate": false, |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Build and run ## |
| 74 | + |
| 75 | +Install the [justfile](https://github.com/casey/just?tab=readme-ov-file#installation) tool. |
| 76 | +Run |
| 77 | + |
| 78 | +```bash |
| 79 | +just setup # only needed once - then adjust the created .env file |
| 80 | +just build # compiles the binary |
| 81 | +just run {args} # runs without the compiled binary |
| 82 | +just run-exe {args} # runs the compiled binary |
| 83 | +``` |
0 commit comments