Comments (2)
Disclaimer: I have no idea what happens if you're not making an ELF file.
It is possible to register code to run before/after main (including if std.process.exit()
is called, but not if a panic occurs, or it exits due to an unhandled signal) when linking libc by placing an array of function pointers into the .init_array
and .fini_array
sections. I'm not sure if this is exactly equivalent to the constructor
/destructor
but it may work for you.
Here's a minimal example:
pub fn main() !void {
try std.io.getStdOut().writeAll("this is main\n");
std.process.exit(2);
}
fn constructor1() callconv(.C) void {
std.io.getStdOut().writeAll("this is constructor1\n") catch @panic("constructor1 failed");
}
fn constructor2() callconv(.C) void {
std.io.getStdOut().writeAll("this is constructor2\n") catch @panic("constructor2 failed");
}
fn destructor() callconv(.C) void {
std.io.getStdOut().writeAll("this is destructor\n") catch @panic("destructor failed");
}
export const init_array: [2]*const fn () callconv(.C) void linksection(".init_array") = .{&constructor1, &constructor2};
export const fini_array: [1]*const fn () callconv(.C) void linksection(".fini_array") = .{&destructor};
const std = @import("std");
Which produces the following:
> zig run init_fini.zig -lc
this is constructor1
this is constructor2
this is main
this is destructor
I think this is basically how the constructor
/destructor
attributes work though someone could correct me.
from zig.
I think this is basically how the
constructor
/destructor
attributes work though someone could correct me.
I believe you're right.
The thing is, I compile this library for Windows, macOS, and Linux, so doing this for each OS would get a bit hairy. I think it's also tricky in the more general case because the linker is supposed to have "append semantics" (if memory serves) for these arrays. It's not clear how that interacts with Zig's language semantics.
from zig.
Related Issues (20)
- fuzz testing: support macOS
- fuzz testing: support Windows
- fuzzing: support more than one executable at once
- debug info audit - many virtual memory addresses have strange source locations
- `-OReleaseSafe` breaks fuzzing entry points feature; incorrect already-sorted assumption
- unreachable code paths need to be excluded from having coverage instrumentation
- more optimized and correct management of 8-bit PC counters
- fuzzer web interface: play a little jingle when it finds a bug!
- fuzzer web interface: ability to scroll to source locations that newly gain coverage HOT 1
- Ability to determine the file a module is in without complete knowledge of the compilations module dependency graph HOT 5
- --verbose-llvm-bc= compile option does not work when providing a file name
- ICE involving empty slice of anonymous struct type
- --unresolved-symbols seems not work for zig cc
- [Feature Request/Bug] Make MultiArrayList behave like normal ArrayList HOT 5
- compiler: remove unused struct fields HOT 7
- std.json: parsing: possibility for missing optional fields to not trigger an error HOT 2
- @alignOf() returns incorrect alignment for structs containing multiple SIMD vectors HOT 2
- Crash (likely undefined behavior) of `std.process.Child.run` in `ReleaseFast` mode only
- ICE: self-hosted generating an atom for an extern declaration
- translate-c misses an alignment cast in translated C code HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zig.