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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -2242,7 +2242,7 @@ or
{#code|test_aligned_struct_fields.zig#}

<p>
Equating packed structs results in a comparison of the backing integer,
Equating packed structs results in a comparison of the backing integer,
and only works for the `==` and `!=` operators.
</p>
{#code|test_packed_struct_equality.zig#}
Expand Down Expand Up @@ -6791,6 +6791,36 @@ $ ./zig-out/bin/test
all your base are belong to us{#end_shell_samp#}
{#see_also|Targets|Zig Build System#}
{#header_close#}
{#header_open|Main entry in C#}
<p>
If you are transitioning an existing C project to Zig and your {#syntax#}main{#endsyntax#} function is still in C, you might get the following error:
</p>
{#shell_samp#}error: root struct of file 'my_zig_helpers' has no member named 'main'{#end_shell_samp#}
<p>
You can let Zig know not to look for main by assigning void to {#syntax#}_start{#endsyntax#} and {#syntax#}WinMainCRTStartup{#endsyntax#}:

</p>

{#syntax_block|zig|my_zig_helpers.zig#}
pub const _start = void;
pub const WinMainCRTStartup = void;

export fn times_two(x: c_int) c_int {
return x * 2;
}
{#end_syntax_block#}
{#syntax_block|c|main.c#}
#include <stdio.h>

int times_two(int);
int main(int argc, char **argv) {
fprintf(stderr, "3 doubled is %i\n", times_two(3));
return 0;
}
{#end_syntax_block#}
<p>To compile:</p>
{#shell_samp#}$ zig build-exe main.c my_zig_helpers.zig -lc {#end_shell_samp#}
{#header_close#}
{#header_close#}
{#header_open|WebAssembly#}
<p>Zig supports building for WebAssembly out of the box.</p>
Expand Down
Loading