andrewrk / sdl-zig-demo

SDL2 hello world in zig
MIT License
116 stars 20 forks source link

Does not build in MacOS #10

Open spcutrell opened 3 years ago

spcutrell commented 3 years ago

Issue: Does not build on Mac.

Error:

./src/main.zig:1:11: error: C import failed
const c = @cImport({
          ^
./zig-cache/o/812b576719d56fc816f68bf98065eb2d/cimport.h:1:10: note: 'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>
         ^
./src/main.zig:7:9: note: referenced here
    if (c.SDL_Init(c.SDL_INIT_VIDEO) != 0) {
        ^

Fix: Change "SDL/SDL2" to "SDL2" in cImport.

Example of fix:

  const c = @cImport({                                                            
      @cInclude("SDL.h");                                                         
  }); 
baskerville commented 3 years ago

It would seem that, on macOS, the default include directories only originate from the output of pkg-config --cflags --libs sdl2, which is -D_THREAD_SAFE -I/usr/local/include/SDL2 -L/usr/local/lib -lSDL2.

hippietrail commented 2 years ago

For me today I get this error:

/Users/hippietrail/sdl-zig-demo/src/main.zig:28:25: error: use of undeclared identifier 'c_void'
        @ptrCast(*const c_void, &zig_bmp[0]),
                        ^~~~~~

Which is due to the new change from c_void to anyopaque

When I change this line

        @ptrCast(*const c_void, &zig_bmp[0]),

to

        @ptrCast(*const anyopaque, &zig_bmp[0]),

I get the expected

./src/main.zig:1:11: error: C import failed
const c = @cImport({
          ^
./zig-cache/o/f6a9a2ce854b815a301136d2734c1357/cimport.h:1:10: note: 'SDL2/SDL.h' file not found
#include <SDL2/SDL.h>

But then when I fix

    @cInclude("SDL2/SDL.h");

to

    @cInclude("SDL.h");

I still get an error:

./src/main.zig:1:11: error: C import failed
const c = @cImport({
          ^
./zig-cache/o/b7cf683efbb6bf33a94d96128858c0f8/cimport.h:1:10: note: 'SDL.h' file not found
#include <SDL.h>
theodesp commented 10 months ago

@spcutrell maybe the sdl2 on your Mac is not linked properly. This is what I did to resolve this:

❯ brew install sdl2
Warning: sdl2 2.28.5 is already installed, it's just not linked.
To link this version, run:
  brew link sdl2

Then

❯ brew link sdl2
Linking /usr/local/Cellar/sdl2/2.28.5... 
Error: Could not symlink bin/sdl2-config
Target /usr/local/bin/sdl2-config
already exists. You may want to remove it:
  rm '/usr/local/bin/sdl2-config'

To force the link and overwrite all conflicting files:
  brew link --overwrite sdl2

❯ brew link --overwrite sdl2 Linking /usr/local/Cellar/sdl2/2.28.5... 87 symlinks created. ❯ zig build run

Screenshot 2023-11-06 at 14 46 34