#Please help me understand an error (using imported SDL3)

1 messages · Page 1 of 1 (latest)

scenic crag
#

Hi all,

I'm new to Zig, switching over from C. I have a project setup which uses Zig 0.13.0 and @cImport to use SDL 3.1.6. I started to do some basic input handling functions when I ran into an error I don't understand.

SDL is imported with this:

const c = @cImport({
    @cInclude("SDL3/SDL.h");
});

Here is a snippet from my main function where I get the error:

var event: c.SDL_Event = undefined;
while (c.SDL_PollEvent(&event)) {
    input.handleEvent(event);
}

The definition of handleEvent from input.zig:

pub fn handleEvent(event: c.SDL_Event) void {}

I get this error I don't understand:

src\main.zig:29:35: error: expected type 'cimport.union_SDL_Event', found 'cimport.union_SDL_Event'
                input.handleEvent(event);
                                  ^~~~~
E:\zage\.zig-cache\o\...\cimport.zig:4139:36: note: union declared here
pub const union_SDL_Event = extern union {
                            ~~~~~~~^~~~~
E:\zage\.zig-cache\o\...\cimport.zig:3854:36: note: union declared here
pub const union_SDL_Event = extern union {
                            ~~~~~~~^~~~~
src\input.zig:33:28: note: parameter type declared here
pub fn handleEvent(event: c.SDL_Event) void {
                          ~^~~~~~~~~~

It seems like the types are the same, right? I also tried passing in the event by pointer:

// input.zig:
pub fn handleEvent(event: [*c]c.SDL_Event) void {}

// main.zig:
var event: c.SDL_Event = undefined;
while (c.SDL_PollEvent(&event)) {
    input.handleEvent(&event);
}

Similar error:

src\main.zig:29:35: error: expected type '[*c]cimport.union_SDL_Event', found '*cimport.union_SDL_Event'
                input.handleEvent(&event);
                                  ^~~~~~
src\main.zig:29:35: note: pointer type child 'cimport.union_SDL_Event' cannot cast into pointer type child 'cimport.union_SDL_Event'

What am I doing wrong? And what should I do instead? Any help is appreciated.

hollow lake
#

you're likely calling @cImport more than once

scenic crag
#

I'm happy to provide more details or code if needed. I ran into the Discord character limit 😅

#

I am calling @cImport in both files to access SDL

tiny void
#

ull want to only do it once

#

cause they each create their own versions of the types

#

which wont be compatible

scenic crag
#

Ah I think I understand. So there's actually 2 versions of the SDL types and functions being generated?

tiny void
#

yeah

hollow lake
#

i recommend instead of using @cImport to use the build.zig step addTranslateC

#

and put the output of that into a module

tiny void
#

use can cImport once and make the constant pub to access it in other files

hollow lake
#

then you can call @import("translated_module") however much you want

#

or that can work also

tiny void
hollow lake
#

it's been there for like a year

tiny void
#

oh huh

#

til

#

ig i conflated it with the possible removal of cImport

hollow lake
#

the idea is that after removing it, the build.zig step will be the only way

#

and instead of just wrapping zig translate-c like it does now, it'll be an external package

#

so the transition is nearly seamless

scenic crag
#

Thanks both for the help. I temporarily added the @cImport to its own file and imported that file instead and it does solve the issue. I'll properly add the translate step to my build.zig instead soon. Cheers!

hollow lake
#

np!