Search code examples
cgccx11

Undefined Symbols when compiling MinilibX on macOS


I am trying to compile this library (MinilibX) with a simple 'Hello world' type application, but I get the following error:

$ make -C mlx
$ gcc main.c -I. -Lmlx -lmlx ./mlx/libmlx_Darwin.a -framework OpenGL -framework Appkit

Undefined symbols for architecture arm64:
  "_XChangeWindowAttributes", referenced from:
      _mlx_int_set_win_event_mask in libmlx.a(mlx_int_set_win_event_mask.o)
  "_XCreateColormap", referenced from:
      _mlx_init in libmlx.a(mlx_init.o)
  "_XCreateGC", referenced from:
      _mlx_new_window in libmlx.a(mlx_new_window.o)
  "_XCreateWindow", referenced from:
      _mlx_new_window in libmlx.a(mlx_new_window.o)
  "_XGetVisualInfo", referenced from:
      _mlx_int_get_visual in libmlx.a(mlx_int_get_visual.o)
  "_XGetWMNormalHints", referenced from:
      _mlx_int_anti_resize_win in libmlx.a(mlx_int_anti_resize_win.o)
  "_XInternAtom", referenced from:
      _mlx_init in libmlx.a(mlx_init.o)
  "_XMapRaised", referenced from:
      _mlx_new_window in libmlx.a(mlx_new_window.o)
  "_XNextEvent", referenced from:
      _mlx_loop in libmlx.a(mlx_loop.o)
  "_XOpenDisplay", referenced from:
      _mlx_init in libmlx.a(mlx_init.o)
  "_XPending", referenced from:
      _mlx_loop in libmlx.a(mlx_loop.o)
  "_XPutBackEvent", referenced from:
      _mlx_int_wait_first_expose in libmlx.a(mlx_int_wait_first_expose.o)
  "_XSetWMNormalHints", referenced from:
      _mlx_int_anti_resize_win in libmlx.a(mlx_int_anti_resize_win.o)
  "_XSetWMProtocols", referenced from:
      _mlx_new_window in libmlx.a(mlx_new_window.o)
  "_XShmPixmapFormat", referenced from:
      _mlx_int_deal_shm in libmlx.a(mlx_init.o)
  "_XShmQueryVersion", referenced from:
      _mlx_int_deal_shm in libmlx.a(mlx_init.o)
  "_XStoreName", referenced from:
      _mlx_new_window in libmlx.a(mlx_new_window.o)
  "_XSync", referenced from:
      _mlx_loop in libmlx.a(mlx_loop.o)
  "_XWindowEvent", referenced from:
      _mlx_int_wait_first_expose in libmlx.a(mlx_int_wait_first_expose.o)
  "_XkbKeycodeToKeysym", referenced from:
      _mlx_int_param_KeyPress in libmlx.a(mlx_int_param_event.o)
      _mlx_int_param_KeyRelease in libmlx.a(mlx_int_param_event.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

GCC:

$ gcc -v
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

I don't know if I'm linking it wrong, or if there is a conflict between versions/compilers (minilibx uses GCC from what I can tell)...

Here is a minimal reproduction: https://github.com/herbievine/42/tree/so_long/so_long

To run:

git clone https://github.com/herbievine/42.git -b so_long
cd 42/so_long
git clone https://github.com/42Paris/minilibx-linux.git mlx
make

Solution

  • Like @n.m. suggested, linking my code with X11 solved the issue.

    My Makefile looks like this:

    MLX_DIR = ./mlx
    MLX_LIB = $(MLX_DIR)/libmlx_$(UNAME).a
    MLX_FLAGS = -Lmlx -lmlx -framework OpenGL -framework AppKit
    
    # added this line
    X11_FLAGS = -L/usr/X11/lib -lXext -lX11
    
    SRCS = \
        main.c \
    
    OBJS = $(SRCS:.c=.o)
    
    all: $(MLX_LIB) $(NAME)
    
    .c.o:
        $(CC) $(CFLAGS) -Imlx -c -o $@ $<
    
    $(NAME): $(OBJS)                 # link here 
        $(CC) $(CFLAGS) $(MLX_FLAGS) $(X11_FLAGS) -o $(NAME) $(OBJS)
    
    $(MLX_LIB):
        @make -C $(MLX_DIR)