Search code examples
cmakefilestatic-libraries

Makefile undefined references with 3 libraries


I took an old project and tried to make it work but for some reason it didn't.

Makefile:

### COMPILATION ###
C      = gcc
FLAGS  = -Wall -Wextra -Werror

### EXECUTABLE ###
NAME   = fdf

### INCLUDES ###
LIBFT  = libft
OBJ_PATH  = objs
HEADER = incl
SRC_PATH  = srcs
LIBFTMATH_PATH = libftmath
MLX = libmlx

### SOURCES ###
SOURCES = main.c \
          parsing_map.c \
          fdf.c \
          ft_draw.c \
          color_map.c \
          ft_color_converter.c \
          ft_settings.c \
          user_hook.c \

### OBJECTS ###

SRCS = $(addprefix $(SRC_PATH)/,$(SOURCES))
OBJS = $(addprefix $(OBJ_PATH)/,$(SOURCES:.c=.o))

### COLORS ###
NOC         = \033[0m
BOLD        = \033[1m
UNDERLINE   = \033[4m
BLACK       = \033[1;30m
RED         = \033[1;31m
GREEN       = \033[1;32m
YELLOW      = \033[1;33m
BLUE        = \033[1;34m
VIOLET      = \033[1;35m
CYAN        = \033[1;36m
WHITE       = \033[1;37m

### RULES ###

all: tmp $(NAME)

$(NAME): $(OBJS)
    @echo "$(GREEN)Creating lib files$(CYAN)"
    @make -C $(LIBFT)
    @make -C $(LIBFTMATH_PATH)
    @make -C $(MLX)
    @$(CC) $(FLAGS) -L $(LIBFT) -lft -L $(MLX) -lmlx -L $(LIBFTMATH_PATH) -lftmath  -o $@ $<
    @echo "$(GREEN)Project successfully compiled"

tmp:
    @mkdir -p objs

$(OBJ_PATH)/%.o: $(SRC_PATH)/%.c $(HEADER)/$(NAME).h
    @$(CC) $(FLAGS) -c -o $@ $<
    @echo "$(BLUE)Creating object file -> $(WHITE)$(notdir $@)... $(RED)[Done]$(NOC)"

clean:
    @echo "$(GREEN)Supressing libraries files$(CYAN)"
    @make clean -C $(LIBFT)
    @make clean -C $(LIBFTMATH_PATH)
    @rm -rf $(OBJ_PATH)

fclean:
    @echo "$(GREEN)Supressing libraries files$(CYAN)"
    @rm -rf $(OBJ_PATH)
    @rm -f $(NAME)
    @make fclean -C $(LIBFT)
    @make fclean -C $(LIBFTMATH_PATH)

re: fclean
    @$(MAKE) all -j

.PHONY: temporary, re, fclean, clean

The folder hierarchy is organized this way: The project folder contains the 3 libraries that I compile, srcs folder that contains project .c files and incl folder that contains header fdf.h.

You can see the arborescence of the project here which I will add to the question if it is the reason of the problem, but from what I see it seems to have to do with the way libraries are linked to the project.

I get the following errors:

main.c:(.text+0x99): undefined reference to `ft_isdigit'
main.c:(.text+0x134): undefined reference to `ft_strndup'

etc, like if the Makefile was not able to get the .h

Thank you very much for your help

edit: I was able to get rid of all the errors thanks to MadScientist but now I have:

mlx_init.c:(.text+0x2c): undefined reference to `XShmQueryVersion'
mlx_init.c:(.text+0x11c): undefined reference to `XShmPixmapFormat'
libmlx/libmlx.a(mlx_init.o): In function `mlx_init':
mlx_init.c:(.text+0x14f): undefined reference to `XOpenDisplay'
mlx_init.c:(.text+0x1ca): undefined reference to `XInternAtom'
mlx_init.c:(.text+0x2ee): undefined reference to `XCreateColormap'
libmlx/libmlx.a(mlx_new_window.o): In function `mlx_new_window':
mlx_new_window.c:(.text+0x91): undefined reference to `XCreateWindow'
mlx_new_window.c:(.text+0xbb): undefined reference to `XStoreName'
mlx_new_window.c:(.text+0xd0): undefined reference to `XSetWMProtocols'
mlx_new_window.c:(.text+0xfb): undefined reference to `XCreateGC'
mlx_new_window.c:(.text+0x128): undefined reference to `XMapRaised'
libmlx/libmlx.a(mlx_pixel_put.o): In function `mlx_pixel_put':
mlx_pixel_put.c:(.text+0x4b): undefined reference to `XChangeGC'
mlx_pixel_put.c:(.text+0x61): undefined reference to `XDrawPoint'
mlx_pixel_put.c:(.text+0x70): undefined reference to `XFlush'
etc

Which seems to be related to the link between -lmlx -lXext -lX11 and libmlx

edit2: Finally solved it with this line:

$(CC) $(FLAGS) -L $(LIBFT) -L $(MLX) -o $@ $^ -lft -lmlx -lXext -lX11 -lm

Solution

  • This line is wrong in various ways; I don't think it ever worked before:

    @$(CC) $(FLAGS) -L $(LIBFT) -lft -L $(MLX) -lmlx -L $(LIBFTMATH_PATH) -lftmath  -o $@ $<
    

    It's always, always a bad idea to prefix your commands with @. At least, you should never do so until your build actually works correctly. By hiding the link line you can't see that the line is wrong. If you want a more flexible way of cleaning up your output try reading this blog post.

    First, you shouldn't be using $< because that expands to only the first prerequisite, not all the prerequisites. That means you're only actually linking the first .o file. You should use $^ on a link line so that all object files are included.

    Second, virtually all modern linkers are "single-pass" linkers which means that the order of libraries on the link line is very important. In particular, you should always add all library options (e.g., -lfoo) to the end of the link line, after all the object files (.o, or $^ as above).