我在brew install libre中从macOS安装了一个实时库 libre 。它位于/usr/local/Cellar/libre/0.5.7目录中。我试图在Makefile中明确添加此路径,所以我声明RT_LIBS_PATH=-L/usr/local/Cellar/libre/0.5.7。整个makefile如下所示:

TARGET = run
LIBS = -O2 -lm
CC = gcc-7
CFLAGS = -fopenmp
RT_LIBS_PATH=-L/usr/local/Cellar/libre/0.5.7/lib


.PHONY: default all clean

all: $(TARGET)

OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c))
HEADERS = $(wildcard *.h)

%.o: %.c $(HEADERS)
    @$(CC) $(CFLAGS) $(RT_LIBS_PATH) -c $< -o $@

.PRECIOUS: $(TARGET) $(OBJECTS)

$(TARGET): $(OBJECTS)
    @$(CC) $(OBJECTS) $(CFLAGS) $(LIBS)  -o $@

clean:
    -rm -f *.o
    -rm -f $(TARGET)

但是,似乎makefile无法识别libre,因此我认为我以错误的方式使用了RT_LIBS_PATH=-L/usr/local/Cellar/libre/0.5.7/lib。请问这种方式有问题吗?

最佳答案

创建.o时必须指定库路径,将所有objs链接到可执行文件时必须指定库路径

$(TARGET): $(OBJECTS)
    @$(CC) $(OBJECTS) $(CFLAGS) $(RT_LIBS_PATH) $(LIBS)  -o $@

关于c - 如何通过makefile显式链接库路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49663449/

10-10 10:20