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

Add "main entry in C" documentation section #21945

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

danielchasehooper
Copy link
Contributor

@danielchasehooper danielchasehooper commented Nov 9, 2024

While transitioning a C project to Zig, it tripped me up that Zig expected main() to be in Zig. Took a while for me to find the workaround. I felt the docs should mention it so others can avoid the same snag.

This is what it looks like:

Screenshot 2024-11-09 at 10 34 15 AM

@danielchasehooper danielchasehooper changed the title Add documentation section describing how to have the main entry point in C Add "main entry in C" documentation section Nov 9, 2024
@gooncreeper

This comment was marked as resolved.

@danielchasehooper
Copy link
Contributor Author

danielchasehooper commented Nov 9, 2024

This recommendation comes from Andrew. found here.

The example works with zig master on macos. Are you on windows or an older version of zig?

@rohlem
Copy link
Contributor

rohlem commented Nov 9, 2024

The example works with zig master on macos.

It probably works on macOS because that uses libc as its system ABI, therefore including it by default iirc.
The full example linked does in fact call .linkLibC(), which is equivalent to specifying the -lc flag.

@danielchasehooper
Copy link
Contributor Author

Ok, added -lc flag.

Copy link
Contributor

@gooncreeper gooncreeper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Under Root Source File there is already a relevant TODO: if linking with libc you can use export fn main, you should put the section there and additionally add an example of a C main in Zig.

@danielchasehooper
Copy link
Contributor Author

Under Root Source File ... you should put the section there

Since this information is specifically about mixed C/Zig code, I think it belongs in the section about integrating C and Zig. The fact that the solution involves the root source file is incidental.

additionally add an example of a C main in Zig.

I do not know how to do a C main in Zig. This PR is about the opposite problem.

Would someone with the power to merge mind chiming in? cc @andrewrk @alexrp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants