Zig๋กœ gcc/clang ์ปดํŒŒ์ผ๋Ÿฌ ๋Œ€์ฒดํ•˜๊ธฐ

last updated: {{ โ€œ1712680347โ€ | date: โ€œ%Y-%m-%d %H:%Mโ€ }} Intro zig๋กœ c ์†Œ์Šค๋ฅผ ์ปดํŒŒ์ผํ•  ์ˆ˜ ์žˆ๋‹ค. zig์˜ ์žฅ์ ์€ ์—ฌ๋Ÿฌ ํ”Œ๋žซํผ๊ณผ ์•„ํ‚คํ…์ฒ˜์šฉ ์ปดํŒŒ์ผ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. static, dynamic ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋‹ค. Linux, iOS, Windows ๋“ฑ ์—ฌ๋Ÿฌ OS ํ™˜๊ฒฝ ์ง€์›. x86, , x86-64, aarch64 ๋“ฑ ์•„ํ‚คํ…์ฒ˜ ์ง€์› Target List ๋‹ค์Œ ๋ฐฉ๋ฒ•์œผ๋กœ ์›ํ•˜๋Š” ํ”Œ๋žซํผ, ์•„ํ‚คํ…์ณ๋ฅผ ๋จผ์ € ์ฐพ๋Š”๋‹ค. $ zig targets $ zig targets | grep x86 $ zig targets | grep x86-64 $ zig targets | grep aarch64 $ zig targets | grep linux $ zig targets | grep windows $ zig targets | grep macos x86, x86_64 - intel ๊ณ„์—ด aarch64 - arm ๊ณ„์—ด (raspberrypi) linux - Linux windows - Ms Windows macos - Mac OS Local Compile dynamic: Alpine Linux (x86_64) $ zig cc -o hello_dynamic hello.c $ file hello_dynamic hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, with debug_info, not stripped $ ./hello Hello World! static: Alpine Linux (x86_64) $ zig cc -o hello hello.c -target x86_64-linux-musl $ file hello ./hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped $ ./hello Hello World! Cross Compile For windows $ zig cc -o hello.exe hello.c -target x86_64-windows-gnu $ file ./hello.exe hello: PE32+ executable (console) x86-64, for MS Windows, 7 sections Windows c:> .\hello.exe Hello World! For Linux dynamic with Gnu Libs $ zig cc -o hello hello.c -target x86_64-linux-gnu $ file ./hello.exe ./hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.0.0, with debug_info, not stripped gnu-linux$ ./hello Hello World! For Linux Static with musl $ zig cc -o hello.exe hello.c -target x86_64-linux-gnu $ file ./hello.exe ./hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, with debug_info, not stripped any-linux$ ./hello Hello World! Makefile or Build.sh ํ•ด๋‹น ํŒŒ์ผ์—์„œ gcc, clang ๋ถ€๋ถ„์„ zig cc๋กœ ์ˆ˜์ •ํ•œ๋‹ค. make ๋˜๋Š” ./Build.sh ์‹คํ–‰ Links zig cc: a Powerful Drop-In Replacement for GCC/Clang https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html

2024-04-03 ยท 313 words

Zig + Nvim ์„ค์ •

last updated: {{ โ€œ1710903265โ€ | date: โ€œ%Y-%m-%d %H:%Mโ€ }} Intro neovim์—์„œ zig ์„ค์ • ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ๋‹ค. Install on Alpine ์„ค์น˜ํ•˜๊ธฐ $ doas apk add zig zls neovim $ nvim ~/.local/nvim/init.lua require 'lspconfig'.zls.setup{} ๋‹ค๋ฅธ ์–ด๋–ค ์–ธ์–ด๋ณด๋‹ค๋„ ์„ค์ •์ด ๊ฐ„๋‹จํ•˜๋‹ค. zls๋Š” zig language server lspconfig๋Š” neovim์— ๋‚ด์žฅ๋œ LSP ์„ค์ •์„ ๋งํ•œ๋‹ค. nvim ์„ค์ • ํ›„ zigํŒŒ์ผ์„ ์—ด๊ณ  ์ฝ”๋”ฉํ•˜๋ฉด ๋„์›€๋ง์ด ๋‚˜ํƒ€๋‚œ๋‹ค. tab์œผ๋กœ ์ด๋™ํ•˜๊ณ  Enter๋กœ ์„ ํƒํ•œ๋‹ค. Install zls from Source ์†Œ์Šค๋ฅผ ์ปดํŒŒ์ผํ•˜๋Š” ๋ฐฉ๋ฒ•. ์ตœ์‹  ๋ฒ„์ „์˜ zig(ํ˜„์žฌ 0.12)์ด์–ด์•ผ git ์†Œ์Šค๋กœ ์ปดํŒŒ์ผ ๋œ๋‹ค. zig ์ตœ์‹  ๋ฒ„์ „์€ zig ํ™ˆ์—์„œ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ ์ ˆ์ฐจ์— ๋”ฐ๋ผ ๋นŒ๋“œํ•œ๋‹ค. $ git clone https://github.com/zigtools/zls $ cd zls $ zig build -Doptimize=ReleaseSafe $ mkdir ~/bin && cp zig-out/bin/zls ~/bin $ echo 'export PATH=$HOME/bin:$PATH' >> ~/.bash_profile $ source ~/.bash_profile $ zls --version Links Neovim setup for Zig https://terminalprogrammer.com/neovim-setup-for-zig

2024-03-12 ยท 121 words

Zig Programming Language

last updated: {{ โ€œ1712202776โ€ | date: โ€œ%Y-%m-%d %H:%Mโ€ }} Intro a general-purpose programming language and toolchain for maintaining robust, optimal and reusable software. Install Debian/Ubuntu $ sudo apt install zig Alpine Linux $ doas apk add zig ์ตœ์‹  ๋ฆฌ๋ˆ…์Šค ๋ฒ„์ „ ์„ค์น˜ https://ziglang.org/download/ os์™€ ์•„ํ‚คํ…์ณ์— ๋งž๋Š” ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค. ์••์ถ•์„ ํ‘ผ ํ›„ PATH๋ฅผ ์„ค์ •ํ•ด์ฃผ๋ฉด ๋ฐ”๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ˜„์žฌ ์‚ฌ์šฉ ๋ฒ„์ „: 0.12 $ tar xJf zig-linux-x86_64-0.12.0-dev.3212+40e64245f.tar.xz $ ln -s zig-linux-x86_64-0.12.0-dev.3212+40e64245f/ zig $ cd zig; ls $ ./zig version $ ./zig help Hello World hello-world.zig const std = @import("std"); pub fn main() !void { std.debug.print("Hello, World!\n", .{}); } run / test $ zig run hello-world.zig $ zig test hello-world.zig build (default=debug, small, fast, safe) $ zig build-exe hello.zig $ ./hello $ zig build-exe --name hello-small -O ReleaseSmall hello.zig $ zig build-exe --name hello-fast -O ReleaseFast hello.zig $ zig build-exe --name hello-safe -O ReleaseSafe hello.zig $ zig build-exe --name hello-debug -O Debug hello.zig $ ls -l $ ./hello-small More Docs zig-nvim - neovim์— zig ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์ •ํ•˜๊ธฐ zig-cc - Zig๋กœ gcc/clang ์ปดํŒŒ์ผ๋Ÿฌ ๋Œ€์ฒดํ•˜๊ธฐ Links home - https://ziglang.org/ awesome zig - https://www.trackawesomelist.com/catdevnull/awesome-zig/readme/ Study and Docs zig-guide - https://zig.guide/ reference - https://ziglang.org/documentation/0.11.0/ zig-by-example - https://zig-by-example.com/ zig std - https://ziglang.org/documentation/master/std/ exercises - https://exercism.org/tracks/zig/exercises learn x in y minutes - https://learnxinyminutes.com/docs/zig/ Compile and Build zig-cc - https://andrewkelley.me/post/zig-cc-powerful-drop-in-replacement-gcc-clang.html Zig Build System Internals - https://mitchellh.com/zig/build-internals

2024-03-12 ยท 214 words