-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Support compiling single file header libraries as C source files #19423
Comments
There is no such thing as "compiling a header file". Is there any reason you can't just |
Yes, there are usually some issues with translate C that prevent this from working in my experience
What I am referring to is when normal c code is embedded in a .h file under a special compilation flag. Normally the file just acts as a header, but when the compilation flag is used, it needs to be compiled as source code |
I've done the same thing previously, trying to compile the header file directly leads to an error as mentioned above; My workaround for now was instead compiling a stub #include INCLUDE_STUB_C_FILE_PATH_TO_INCLUDE then adding this stub file in the stb_truetype_object.linkLibC();
stb_truetype_object.addSystemIncludePath(.{ .path = stb_truetype_header_directory_path });
stb_truetype_object.addCSourceFile(.{
.file = .{ .path = include_stub_path },
.flags = &.{ "-DSTB_TRUETYPE_IMPLEMENTATION", "-DINCLUDE_STUB_C_FILE_PATH_TO_INCLUDE=<" ++ stb_truetype_h_file_name ++ ">" },
}); While it's true that that's not how header files are meant to be used, this usage exists in practice. |
Related issue: #3495 |
@nektro In // Inherit dependencies on system libraries and static libraries.
for (module.link_objects.items) |link_object| {
//...
.c_source_file => |c_source_file| l: {
if (!my_responsibility) break :l;
if (c_source_file.flags.len == 0) {
if (prev_has_cflags) {
try zig_args.append("-cflags");
try zig_args.append("--");
prev_has_cflags = false;
}
} else {
try zig_args.append("-cflags");
for (c_source_file.flags) |arg| {
try zig_args.append(arg);
}
try zig_args.append("--");
prev_has_cflags = true;
}
try zig_args.append(c_source_file.file.getPath2(module.owner, step));
total_linker_objects += 1;
}, I will create an try zig_args.append(c_source_file.file.getPath2(module.owner, step)); With a line that appends the path to the It also seems odd to me that |
needs a .c file, ziglang/zig#19423
There is- it produces an artifact called a "precompiled header". However that's not what this issue is about. This issue is a feature request for overriding the file extension detection so that instead of being classified as a C header file, it is classified as a C source file (it does not need to generate a file in the cache as suggested by the OP). |
…ction. It is normally based on the file extension, however: - it can be ambiguous. for instance, ".h" is often used for c headers or c++ headers. ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655) - a singular file may be interpreted with different languages depending on the context. in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle) (ziglang#19423)
…ction. It is normally based on the file extension, however: - it can be ambiguous. for instance, ".h" is often used for c headers or c++ headers. ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655) - a singular file may be interpreted with different languages depending on the context. in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle) (ziglang#19423)
…ction. It is normally based on the file extension, however: - it can be ambiguous. for instance, ".h" is often used for c headers or c++ headers. ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655) - a singular file may be interpreted with different languages depending on the context. in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle) (ziglang#19423)
…ction. It is normally based on the file extension, however: - it can be ambiguous. for instance, ".h" is often used for c headers or c++ headers. ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655) - a singular file may be interpreted with different languages depending on the context. in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle) (ziglang#19423)
…ction. It is normally based on the file extension, however: - it can be ambiguous. for instance, ".h" is often used for c headers or c++ headers. ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655) - a singular file may be interpreted with different languages depending on the context. in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle) (ziglang#19423)
…tection. and change `addAssemblyFile()` to use a `AsmSourceFile` option struct as well. (breaking change) Language is inferred from the file extension, however: - it can be ambiguous. for instance, ".h" is often used for c headers or c++ headers. ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655) - a singular file may be interpreted with different languages depending on the context. in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle) (ziglang#19423)
…tection. and change `addAssemblyFile()` to use a `AsmSourceFile` option struct as well. (breaking change) Language is inferred from the file extension, however: - it can be ambiguous. for instance, ".h" is often used for c headers or c++ headers. ".s" (instead of ".S") assembly files may still need the c preprocessor. (ziglang#20655) - a singular file may be interpreted with different languages depending on the context. in "single-file libraries", the source.h file can be both a c-header to include, or compiled as a C file (with a #define as toggle) (ziglang#19423)
Currently there are issues when you try to compile a single file header as a C source file
build.zig
To fix this you have to manually create a .c file that includes the .h file, and then pass that .c file to exe.addCSourceFile. Given that single file header libraries are fairly common in c, you shouldn't need to use a workaround to gain this functionality.
From my understanding, this error is caused by issues with how clang compiles header files. However, this could be fairly easily fixed by the zig build system automatically generating a .c file somewhere in the cache that includes the .h file. Then zig can pass that .c file to clang for compilation, rather than requiring the user to do this manually.
Fixing this bug would make managing C dependencies thru the zig package manager much nicer
The text was updated successfully, but these errors were encountered: