Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
Clojure
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Helion
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
zig source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
zig 0.10.0
zig 0.11.0
zig 0.12.0
zig 0.12.1
zig 0.13.0
zig 0.14.0
zig 0.14.1
zig 0.15.1
zig 0.15.2
zig 0.2.0
zig 0.3.0
zig 0.4.0
zig 0.5.0
zig 0.6.0
zig 0.7.0
zig 0.7.1
zig 0.8.0
zig 0.9.0
zig trunk
Options
Source code
const std = @import("std"); const builtin = @import("builtin"); comptime { _ = if (builtin.os.tag == .windows) struct { export const start_my_section: ?*i32 linksection("my_sec$a") = null; export const stop_my_section: ?*i32 linksection("my_sec$z") = null; } else if (builtin.os.tag.isDarwin()) struct { export const dummy: ?*const i32 linksection("__DATA,my_section") = null; comptime { asm ( \\.global start_my_section \\start_my_section = section$start$__DATA$my_section \\ \\.global stop_my_section \\stop_my_section = section$end$__DATA$my_section ); } } else struct { export const dummy: ?*const i32 linksection("my_section") = null; comptime { asm ( \\.pushsection .init_array,"aw",%init_array \\.reloc ., BFD_RELOC_NONE, my_section \\.popsection \\ \\.global start_my_section \\start_my_section = __start_my_section \\ \\.global stop_my_section \\stop_my_section = __stop_my_section ); } }; } extern const start_my_section: ?*const i32; extern const stop_my_section: ?*const i32; pub inline fn registerResource(comptime x: i32) void { const section_name = if (builtin.os.tag == .windows) "my_sec$u" else if (builtin.os.tag.isDarwin()) "__DATA,my_section" else "my_section"; _ = struct { const val_ptr: *const i32 = &x; comptime { @export(&val_ptr, .{ .name = "resource_" ++ @typeName(@This()), .section = section_name, .linkage = .strong, .visibility = if (builtin.os.tag == .windows) .default else .hidden, }); } }; } comptime { registerResource(5); registerResource(10); registerResource(15); registerResource(20); registerResource(25); registerResource(30); registerResource(35); registerResource(40); registerResource(45); } pub fn main() void { const start: [*]const ?*const i32 = @ptrCast(&start_my_section); const stop: [*]const ?*const i32 = @ptrCast(&stop_my_section); const num_resources = stop - start; for (start[0..num_resources]) |cursor| { if (cursor) |cur| std.debug.print("resource: {d}\n", .{cur.*}); } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree