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

Automatically add includes for cimports #7771

Closed
Sobeston opened this issue Jan 13, 2021 · 5 comments
Closed

Automatically add includes for cimports #7771

Sobeston opened this issue Jan 13, 2021 · 5 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@Sobeston
Copy link
Contributor

Currently if I have a folder that contains two files: main.c and main.zig, the following code inside main.zig does not work:

const c = @cImport(
    @cInclude("main.c"),
);

This is because our way of including C files is modeled after how C does it (which makes total sense). However, this is a bad user experience and frankly unsatisfying.

What I propose here is automatically adding includes for cimports - one include at the directory of cimporting zig file.

@SpexGuy
Copy link
Contributor

SpexGuy commented Jan 13, 2021

We could do the C thing and have two:

@cInclude("foo.h"), // searches this directory first
@cInclude(<foo.h>), // searches only include directories

(but please don't actually do this)


In all seriousness though, the C compatibility features are there to match C. Since C's behavior for an include with quotes is to search the file's directory, and we use quotes (albeit for different reasons), I think we should do the same thing as C here and search the file's directory before searching include directories.

@Vexu Vexu added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Feb 21, 2021
@Vexu Vexu added this to the 0.8.0 milestone Feb 21, 2021
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk
Copy link
Member

@Sobeston I'm happy to re-open this if you clarify the proposal more. As it stands I don't really understand what it is suggesting. Can you include a full example with all files involved and the command line you expect to execute?

@SpexGuy
Copy link
Contributor

SpexGuy commented Nov 24, 2021

Here's a complete example:

// src/foo.c
#include "foo.h"
// src/foo.h
#define FOO 1
// src/foo.zig
comptime {
  _ = @cImport({
    @cInclude("foo.h");
  });
}

The command zig cc src/foo.c -c -o foo.o succeeds, because #include "foo.h" searches in the directory containing the .c file first. But the command zig build-obj src/foo.zig -lc fails, because src is not specified on the include path. The proposal here is to change the search path of @cInclude to start with the directory containing the .zig file.

@SpexGuy SpexGuy reopened this Nov 24, 2021
@andrewrk
Copy link
Member

Thanks!

@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 24, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
@andrewrk
Copy link
Member

Solved by moving @cImport to the build system. #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.
Projects
None yet
Development

No branches or pull requests

4 participants