A general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
If you are looking at this README file in a source tree, please refer to the Release Notes, Language Reference, or Standard Library Documentation corresponding to the version of Zig that you are using by following the appropriate link on the download page.
Otherwise, you're looking at a release of Zig, and you can find documentation here:
- doc/langref.html
- doc/std/index.html
A Zig installation is composed of two things:
- The Zig executable
- The lib/ directory
At runtime, the executable searches up the file system for the lib/ directory, relative to itself:
- lib/
- lib/zig/
- ../lib/
- ../lib/zig/
- (and so on)
In other words, you can unpack a release of Zig anywhere, and then begin
using it immediately. There is no need to install it globally, although this
mechanism supports that use case too (i.e. /usr/bin/zig and /usr/lib/zig/).
Ensure you have the required dependencies:
- CMake >= 3.5
- System C/C++ Toolchain
- LLVM, Clang, LLD development libraries == 17.x
Then it is the standard CMake build process:
mkdir build
cd build
cmake ..
make install
For more options, tips, and troubleshooting, please see the Building Zig From Source page on the wiki.
In this case, the only system dependency is a C compiler.
cc -o bootstrap bootstrap.c
./bootstrap
This produces a zig2 executable in the current working directory. This is a
"stage2" build of the compiler,
without LLVM extensions, and is
therefore lacking these features:
- Release mode optimizations
- aarch64 machine code backend
@cImport/zig translate-c- Ability to compile C files
- Ability to compile assembly files
- Some ELF linking features
- Most COFF/PE linking features
- Some WebAssembly linking features
- Ability to create import libs from def files
- Automatic importlib file generation for Windows DLLs
- Ability to create static archives from object files
- Ability to compile C++, Objective-C, and Objective-C++ files
However, a compiler built this way does provide a C backend, which may be useful for creating system packages of Zig projects using the system C toolchain. In such case, LLVM is not needed!
Zig is Free and Open Source Software. We welcome bug reports and patches from everyone. However, keep in mind that Zig governance is BDFN (Benevolent Dictator For Now) which means that Andrew Kelley has final say on the design and implementation of everything.
One of the best ways you can contribute to Zig is to start using it for an open-source personal project.
This leads to discovering bugs and helps flesh out use cases, which lead to further design iterations of Zig. Importantly, each issue found this way comes with real world motivations, making it straightforward to explain the reasoning behind proposals and feature requests.
You will be taken much more seriously on the issue tracker if you have a personal project that uses Zig.
The issue label Contributor Friendly exists to help you find issues that are limited in scope and/or knowledge of Zig internals.
Please note that issues labeled Proposal but do not also have the Accepted label are still under consideration, and efforts to implement such a proposal have a high risk of being wasted. If you are interested in a proposal which is still under consideration, please express your interest in the issue tracker, providing extra insights and considerations that others have not yet expressed. The most highly regarded argument in such a discussion is a real world use case.
For more tips, please see the Contributing page on the wiki.
The Zig community is decentralized. Anyone is free to start and maintain their own space for Zig users to gather. There is no concept of "official" or "unofficial". Each gathering place has its own moderators and rules. Users are encouraged to be aware of the social structures of the spaces they inhabit, and work purposefully to facilitate spaces that align with their values.
Please see the Community wiki page for a public listing of social spaces.
from https://github.com/ziglang/zig
------------------------------------------------
General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org/
A general-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
Resources
- Introduction
- Download & Documentation
- Chapter 0 - Getting Started | ZigLearn.org
- Community
- Contributing
- Code of Conduct
- Frequently Asked Questions
- Community Projects
Installation
- download a pre-built binary
- install from a package manager
- build from source
- bootstrap zig for any target
Zigcli is a toolkit for building command lines programs in Zig.
This package provides:
- Modules
- Zig modules to help developers build their awesome cli programs. Currently there are:
simargs, which is used to parse argumentspretty-table, which is used to print aligned and formatted tables
- Programs
- Practical command line programs which can be used directly. Currently there are:
loc, lines of code.tree, list contents of directories in a tree-like format.yes, output a string repeatedly until killed.repeat, repeat a command until it succeeds.pidof, like pidof, but for macOS.night-shift, control Night Shift in macOS.dark-mode, control dark mode in macOS.
Prebuilt binaries can be found in CI’s artifacts, or you can build from source:
git clone https://github.com/jiacai2050/zigcliThen build with
make buildTo build zigcli locally, Zig master is required, which can be downloaded here.
from https://github.com/jiacai2050/zigcli
(https://codeberg.org/jiacai2050/zigcli)
------------------------------------------------------
zigcc
A util script that aims to ease use zig cc to compile C/C++/Rust/Go programs.
In most cases, we can use following command to use Zig for compile
CC='zig cc' CXX='zig c++' ...However in real world, there are many issues this way, such as:
- zig cc: parse -target and -mcpu/-march/-mtune flags according to clang #4911
- Targets compare: Rust to Zig
- unsupported linker arg
- Rust + `zig cc` CRT conflict. : rust
So this project was born, it will
- Convert target between Zig and Rust/Go
- Ignore link args when
zig ccthrow errors, hopefully this will make compile successfully, WIP.
Download zigcc file, give it executable permission, and create two soft links zigcxx, zigcargo to it.
curl -Lo zigcc https://github.com/jiacai2050/zig-cc/raw/main/zigcc
chmod +x zigcc
ln -s zigcc zigcxx
ln -s zigcc zigcargoThen use like this
CC=zigcc CXX=zigcxx ...zigcargo can used to replace cargo, it will automatically set
CCCARGO_TARGET_<triple>_LINKERtozigccCXXtozigcxx
Adding a step to your workflow like this:
- uses: jiacai2050/zigcc@v1
with:
zig-version: masterThen you can invoke zigcc zigcxx zigcargo in following steps.
There some are env variable to config zigcc:
ZIGCC_FLAGS, space separated flags, pass to zig cc. An example is set this to-fno-sanitize=undefinedto disable sanitize since they may break your programs. See Catching undefined behavior with zig ccZIGCC_BLACKLIST_FLAGS, space separated flags, used to filter flagszig ccdon’t support, such as-Wl,-dylibotherwise you could see errors below= note: error: unsupported linker arg: -dylib
ZIGCC_LOG Set to 1 enable verbose logs.from https://github.com/jiacai2050/zigcc
(https://codeberg.org/jiacai2050/zigcc)
---------------------------------------------------
https://github.com/ziglibs/repository
--------------------------------------------------
zig学习资料
由于 Zig 目前还处于快速迭代,因此最权威的资料无疑是官方的 Zig Language Reference,遇到语言的细节问题,基本都可以在这里找到答案。其次是社区的一些高质量教程,例如:
- 学习 Zig
- 该系列教程最初由 Karl Seguin 编写,该教程行文流畅,讲述的脉络由浅入深,深入浅出,是入门 Zig 非常不错的选择
- Zig 语言圣经
- 一套完整的 Zig 教程
- ziglings/exercises
- Learn the Zig programming language by fixing tiny broken programs.
- Zig Cookbook
- A collection of simple Zig programs that demonstrate good practices to accomplish common programming tasks
- zigcc/zig-idioms
- Common idioms used in Zig
- zigcc/awesome-zig
- A collection of some awesome public Zig programming language projects.
- from https://ziglang.cc/
- (https://github.com/zigcc, Zig Chinese Community, Zig 语言中文社区
- ---------------------------------------------------------------------------------
How build systems run
Different with Rust, there is no rustc-like
zigcto compilebuild.zig, then how doesbuild.zigworks?The answer is sub-process. When you build a fresh project,
zig buildwill first compilebuild.zigto a binary, then run this binary to execute specific chosen steps.The build binary sub-process to other Zig sub-commands to finish their jobs, such as
zig build-exeforCompileStep.build_runner.zig contains the main entry for the build binary,
build.zigis imported usingconst root = @import("@build"), it requires four arguments when start:- Zig executable
- directory where
build.ziglocates - Local cache directory(optional)
- Global cache directory(optional)
We can manually invoke the build binary like this:
$ find . -name build ./zig-cache/o/0c7d271c608c27670403b31835132c7d/build $ ./zig-cache/o/0c7d271c608c27670403b31835132c7d/build $(which zig) $(pwd) .cache .glocal-cache $ zig-out/bin/demo All your codebase are belong to us. Run `zig build test` to run the tests.So when you have bugs in
build.zig, you first need to find where the build binary is, then run it in gdb/lldb with args above.Conclusion
A fully featured build system is necessary to keep developers productive, but flexibility come at a cost of misuse, so design a good build system is hard.
Zig's build system is based on the language itself, developers don't need to learn a new language to write their build(CMake, I'm looking at you!). And Zig provides a declarative API to construct steps' DAG, lots of pre-defined steps to finish common task, powerful as will as practical.
Besides build system, package manager in Zig is in rapid development. With those, developer can build old/complex C/C++ projects in just one command:
zig build. Some early adopters:- pcre2, Perl-Compatible Regular Expressions
- llama.cpp, Port of Facebook's LLaMA model in C/C++
- raylib, A simple and easy-to-use library to enjoy videogames programming
Discussions on Lobsters and Reddit
References
Click on my profile below to say hellow.
ReplyDelete