GithubHelp home page GithubHelp logo

Comments (2)

dweiller avatar dweiller commented on August 11, 2024

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.

alexrp avatar alexrp commented on August 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.