嗨,我知道还有一些其他帖子的标题相同,但它们不在同一个上下文中。所以:我有一个名为mlx
的库,库文件名为libmlx.dylib
:当我尝试使用gcc main.c libmlx.dylib
用一个简单的main编译它时,它编译得很好,库也在工作,但当我尝试用我的makefile用我的项目编译时,编译不会引发任何错误,但当我们启动程序时,我会收到以下错误消息:
dyld: Library not loaded: libmlx.dylib
Referenced from: /Users/leo/Documents/42-cursus/so_long/./so_long
Reason: image not found
[1] 8313 abort ./so_long
这是我的makefile:
SHELL = /bin/sh
NAME = so_long
.SUFFIXES = .c .o .h .dylib
SRCDIR = src
INCDIR = inc
LIBDIR = lib
OBJDIR = .obj
SRC = $(addsuffix $(word 1, $(.SUFFIXES)),
$(addprefix exception/,
exception
bad_alloc
invalid_arguments
invalid_map
runtime_error)
$(addprefix parsing/,
get_map)
$(addprefix rendering/,
render_map)
$(addprefix utils/,
init_image
make_color)
$(addprefix cleaning/,
mlx_clear)
$(addprefix get_next_line/,
get_next_line
get_next_line_utils)
main)
INC = $(addsuffix $(word 3, $(.SUFFIXES)),
get_next_line
mlx
exception
so_long)
LIB = mlx
ft
OBJ = $(SRC:$(word 1, $(.SUFFIXES))=$(word 2, $(.SUFFIXES)))
CC = gcc
CFLAGS = -Wall -Wextra -Werror -I $(INCDIR)
LCFLAGS = $(addprefix -L, $(LIBDIR)) $(addprefix -l, $(lib))
#### COLORS ####
KNRM = x1B[0m
KRED = x1B[31m
KGRN = x1B[32m
KYEL = x1B[33m
KBLU = x1B[34m
KMAG = x1B[35m
KCYN = x1B[36m
KWHT = x1B[37m
######################
all: $(OBJDIR) $(NAME)
@printf "$(KGRN)`$(NAME)` is up to date.n$(KNRM)"
$(OBJDIR):
@printf "$(KYEL)➤ "
mkdir -p $@/exception $@/parsing $@/rendering $@/utils $@/cleaning $@/get_next_line
@printf "$(KNRM)"
$(NAME): $(addprefix $(OBJDIR)/, $(OBJ))
@printf "$(KCYN)[ Linking ]n➤ "
$(CC) $(CFLAGS) $^ -o $@ $(LCFLAGS)
@printf "$(KNRM)"
$(OBJDIR)/%$(word 2, $(.SUFFIXES)): $(SRCDIR)/%$(word 1, $(.SUFFIXES)) $(addprefix $(INCDIR)/, $(INC))
@printf "$(KMAG)[ Compiling ]n➤ "
$(CC) $(CFLAGS) -c $< -o $@
@printf "$(KNRM)"
clean:
@printf "$(KRED)➤ "
rm -rf $(OBJDIR)
@printf "$(KNRM)"
fclean: clean
@printf "$(KRED)➤ "
rm -f $(NAME)
@printf "$(KNRM)"
re: fclean all
我将它与标志-L $(LIBDIR) -lmlx
链接起来。我做错了什么?
我发现,对于动态库,您需要将.dylib
与目标放在同一目录中,我允许发布该帖子,以防有些人面临同样的问题。