Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to include arbitrary C source text in @cImport blocks #2070

Closed
andrewrk opened this issue Mar 15, 2019 · 6 comments
Closed

ability to include arbitrary C source text in @cImport blocks #2070

andrewrk opened this issue Mar 15, 2019 · 6 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. translate-c C to Zig source translation feature (@cImport)
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Mar 15, 2019

Inspired by #1085 (comment)

Here's a use case:

const c = @cImport({
    @cInclude("curses.h");
    @cSource(
        \\static const int MY_A_STANDOUT = A_STANDOUT;
    );
});

// now we have access to A_STANDOUT even though it was
// unable to be automatically translated

This would be incredibly easy to implement. It's just a question of whether it's a good idea.

And then there's the follow-up question of, do we really need @cInclude, @cDefine, and @cUndef if we have the all-powerful @cSource?

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Mar 15, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Mar 15, 2019
@Akuli
Copy link
Contributor

Akuli commented Mar 15, 2019

+1. This might be a particularly good solution to #1085 because it could be used to solve a bunch of other problems that nobody can think of yet. For example, if there's something that you can do in C but not in Zig, you could define a C function to do the thing and then call that from Zig (I think).

@andrewrk
Copy link
Member Author

Note that you can already do something very similar with status quo:

const c = @cImport({
    @cInclude("curses.h");
    @cInclude("mycode.h");
});

mycode.h:

static const int MY_A_STANDOUT = A_STANDOUT;

@Akuli
Copy link
Contributor

Akuli commented Mar 15, 2019

To @cInclude with a build.zig project, this can be added to build.zig:

    exe.addIncludeDir(".");

Now @cInclude("src/mycode.h") should work.

@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Apr 30, 2019
@andrewrk andrewrk added the translate-c C to Zig source translation feature (@cImport) label Jun 16, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Oct 17, 2019
@andrewrk andrewrk added contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Jan 2, 2020
@daurnimator
Copy link
Contributor

I think this feature would be really useful.

And then there's the follow-up question of, do we really need @cInclude, @cDefine, and @cUndef if we have the all-powerful @cSource?

We wouldn't need them as builtins, but could have them as functions in the std library instead.

@andrewrk andrewrk removed contributor friendly This issue is limited in scope and/or knowledge of Zig internals. frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Jan 2, 2020
@tauoverpi
Copy link
Contributor

There are a hand full of languages that have taken this approach such as mercury, ats2 and would remove the need of creating separate C source files though I'd be concerned about debugger and editor support for the code contained. I guess a reason for would be the ability to replace C code function by function inline if you could embed all of it but that is also rather messy closer to the 40-60% replaced mark and would promote more C use in zig code as I see it.

@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.10.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.16.0, 0.14.0 Jul 14, 2024
@andrewrk
Copy link
Member Author

Solved instead by #20630

@andrewrk andrewrk closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. translate-c C to Zig source translation feature (@cImport)
Projects
None yet
Development

No branches or pull requests

4 participants