-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
use case: @cImport of Kero Platform library #2257
Comments
Have a look at #2259 which might explain a little bit about what's going on. One thing you can do is use |
Compiling with --verbose-cimport there were a couple of warnings about variables being initialized so I just removed their initialization which cleared that. However there seems to be a warning for most or all functions saying: The first error is: Am I supposed to do something special in my C code to cast pointers? This used to be a malloc call which produced the same error so I tried the calloc call which includes the size of the data type because I thought that might fix the alignment of the pointer. Also getting:
Not sure what I'm supposed to do about this one. It seems like Zig thinks visual_info.depth is int when it should be uint, but visual_info is of
graphics_context is X11's type GC so I'm not sure what I'm supposed to do about this one either. Thanks Andrew! |
It looks like you've found a great use case which is finding faults in zig's translate-c code and related semantics. As a rule of thumb, zig code using C API should be able to roughly look the same as the C code that uses the C API without too much hoop jumping. So all this stuff you are running into is definitely considered problems with Zig that need to be solved. I think this is sort of the thing where someone knowledgeable about how translate-c works in Zig (or wishes to become so) will have to sit down and try this out for themselves, fixing the issues along the way one by one. This issue is a "use case" issue, meaning the way to solve it is to do the thing that @UltimaN3rd is trying to do in the original post, and identify and file other issues for the problems that arise. Then once those issues are all solved and this use case works well, we can close it. |
This was fixed via #2318 |
@UltimaN3rd could you give this another go with zig master? |
Downloaded the zig master branch, made a small update to hello-zig to use the latest version of Kero_Platform and tried to compile. Got this error: cimport.zig:5376:34: error: invalid character: ';' |
That bug got fixed yesterday by #4047 |
Downloaded master again. Having trouble initializing a struct. Looked at the docs, searched the threads and could only find that a struct has to have every single member named and assigned a value? So I can't do this: var platform: c.kero_platform_t = {0}; If there is a way to initialize a struct to 0 I'm happy to try again and see if it'll compile. |
var platform: c.kero_platform_t = undefined;
@memset(@ptrCast([*]u8, &platform), 0, @sizeOf(c.kero_platform_t)); with a (as far as I'm aware does not exist yet) std lib helper function: var platform = std.mem.zeroes(c.kero_platform_t); |
Thanks Andrew, I'll certainly look forward to that std lib function to init structs to 0. I compiled with the suggested code and got a bunch of errors:
I'm sure some casting can fix those expected type errors, but the "unable to translate function" errors seem to revolve around KP_UpdateMouse so I'll paste the source for that function: void KP_UpdateMouse(kero_platform_t *platform) {
Window window_returned;
int display_x, display_y;
unsigned int mask_return;
if(XQueryPointer(platform->display, platform->xwindow, &window_returned,
&window_returned, &display_x, &display_y, &platform->mouse.x, &platform->mouse.y,
&mask_return) == True) {
}
if(platform->mouse.invertx) {
platform->mouse.x = platform->window.w - platform->mouse.x - 1;
}
if(platform->mouse.inverty) {
platform->mouse.y = platform->window.h - platform->mouse.y - 1;
}
} I think that XQueryPointer call is in an if statement because there used to be some code in the brackets. Anyway I don't see anything special about this function so I wonder why it fails to translate. |
Well @UltimaN3rd, it seems the Zig community is dead set on solving this use case 😄 @FireFox317 implemented @LemonBoy implemented #4078 and #4081 and notes that this should solve the remaining problems with importing Kero .h files. |
Thanks @andrewrk, @FireFox317 and @LemonBoy! It compiles without a problem now. However when running it crashes with these errors: Segmentation fault at address 0x0 incorrect alignment Panicked during a panic. Aborting. |
Is your Zig code available somewhere? I tried the following snippet and everything seemed to work as expected: const std = @import("std");
const warn = @import("std").debug.warn;
const c = @cImport({
@cInclude("/home/lemonboy/Downloads/kero_platform.h");
});
pub fn main() void {
var platform: c.kero_platform_t = undefined;
c.KP_Init(&platform, 1280, 720, "Zig test");
} And let's not forget @travisstaloch work on #4113, that solved the very last problem we had in translating your library headers! |
My code looks like this: const std = @import("std"); pub fn main() void { My apologies for not linking it earlier, but I have a small repo just for this: https://gitlab.com/UltimaN3rd/hello-zig I tried the exact code you provided too, changing the kero_platform.h directory and got the same errors as before. I'm also using this build file: const std = @import("std"); pub fn build(b: *Builder) void {
} Perhaps that is the issue? Since the documentation for the zig build system is still TODO it's tough figuring out what to write just based on other build.zig files. Ah and I'm on Linux. I haven't bothered to try it on Windows/Mac with Zig until I get it running on Linux first, but perhaps you're on Windows and it works there but not Linux? I had imagined that most Zig contributors would be using Linux so I didn't think of that possibility until now. Cheers |
That's extremely weird, I'm able to build & run your example code just fine, with or without |
Here's the backtrace: (gdb) bt After looking at the code that calls XMatchVisualInfo I believe I found and fixed a bug in kero_platform.h, so thanks Zig! platform->visual_info is an XVisualInfo pointer which is undefined so I changed it to a normal XVisualInfo instance and passed its address to the function instead. Now it compiles and runs with no errors! Thanks a lot everyone :) |
Using Zig 0.4.0 from the Snap store on Linux Mint 19.1 Mate.
I've got a C library called Kero Platform which just takes care of platform specific stuff like opening a window. I'm trying to compile a simple Zig program by importing the library and calling the first function, KPInit and am getting the error above. Here's the main zig file:
The full project is here: https://gitlab.com/UltimaN3rd/hello-zig
And my Kero Platform library is here: https://gitlab.com/UltimaN3rd/croaking-kero-c-libraries/blob/master/include/kero_platform.h
So what am I doing wrong?
The text was updated successfully, but these errors were encountered: