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

Documentation issue in section "Exporting a C Library" #6753

Open
robertblackwell opened this issue Oct 20, 2020 · 9 comments
Open

Documentation issue in section "Exporting a C Library" #6753

robertblackwell opened this issue Oct 20, 2020 · 9 comments
Labels
docs emit-h This issue is related to generating .h files for C interop
Milestone

Comments

@robertblackwell
Copy link

The example given in the Documentation section Exporting C Libraries does not build (at least when I try). The required C headers file (mathtest.h) is not found.

Need to add -femit-h to

zig build-lib mathtest.zig  -femit-h 

and or add the line

lib.emit_h = true

following

const lib = b.addSharedLibrary("mathtest", "mathtest.zig", version(1,0,0));

in the build.zig file.

There is, I think, a similar issue with the example under the heading Mixing Object Files because it fails to build for a similar reason.

@kliberty
Copy link

Adding -femit-h with the current master build zig-linux-x86_64-0.6.0+4b48fccad does not actually produce a .h file.

log.warn("-femit-h is not available in the stage1 backend; no .h file will be produced", .{});

@robertblackwell
Copy link
Author

robertblackwell commented Oct 23, 2020

I am using zig 0.6.0+288198e51. It does produce a .h file.
So if I upgrade to to a more recent 0.6.0 my "fix" will not work ?
How do I produce a .h file then and make the example in the docs work?
I finally found this comment

// TODO we would like to #include "mathtest.h" here but this feature has been disabled in
// the stage1 compiler. Users will have to wait until self-hosted is available for
// the "generate .h file" feature.

in zig/test/shared_library/test.c

So at this point zig does not deliver "One of the primary use cases for Zig is exporting a library with the C ABI" without writing the libraries .h file by hand. AND the docs don't tell users that.

@kliberty
Copy link

It probably should be mentioned in the docs, however, we're both trying to use daily builds from the master branch. In theory when 0.7.0 is released the self hosting compiler will support -femit-h again, so I think there is an argument to made about allowing the discrepancy to remain.

In the meantime, my solution will probably be to use the version you mentioned.

@andrewrk andrewrk added the docs label Oct 26, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 26, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 6, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@SpexGuy SpexGuy added the emit-h This issue is related to generating .h files for C interop label Oct 23, 2021
@hhstore
Copy link

hhstore commented Aug 3, 2022

 zig version
0.10.0-dev.2617+47c4d4450

errors:

zig build-lib  src/zigmath.zig -femit-h

warning(compilation): -femit-h is not available in the stage1 backend; no .h file will be produced

@hhstore
Copy link

hhstore commented Aug 4, 2022

@everyone

Refer to my example below, it can be solved.

  1. First generate the c header file based on zig v0.6.0 + docker.
# cd this-dir/; do:
docker run --platform linux/x86_64 -v $PWD:/app euantorano/zig:0.6.0 build-lib src/mathtest.zig -femit-h
  1. Then, compile normally based on the current version (zig v0.10). check build.zig

Chinese Reference:

@kassane
Copy link
Contributor

kassane commented Aug 4, 2022

It is possible to export to C only using stage2. So if you download version 0.9.1 or higher, add the -fno-stage1 flag and the header will be generated.

Example: https://jedi.sh/zigo

@edyu
Copy link
Contributor

edyu commented May 4, 2023

Still doesn't work as of 0.11.0-dev.2969+855493bb8

@edyu
Copy link
Contributor

edyu commented May 4, 2023

It is possible to export to C only using stage2. So if you download version 0.9.1 or higher, add the -fno-stage1 flag and the header will be generated.

Example: https://jedi.sh/zigo

site doesn't work

@kassane
Copy link
Contributor

kassane commented May 4, 2023

It is possible to export to C only using stage2. So if you download version 0.9.1 or higher, add the -fno-stage1 flag and the header will be generated.

Example: https://jedi.sh/zigo

site doesn't work

Old reference.

Archived website:
https://web.archive.org/https://jedi.sh/zigo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs emit-h This issue is related to generating .h files for C interop
Projects
None yet
Development

No branches or pull requests

7 participants