如何通过makefile显式链接库路径

时间:2018-04-05 02:58:53

标签: c gcc makefile compiler-errors linker

我在brew install libre的{​​{1}}中安装了一个实时库 libre 。它位于目录macOS中。我试图在/usr/local/Cellar/libre/0.5.7中明确添加此路径,因此我声明Makefile。整个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) ,因此我假设我以错误的方式使用libre。那么,这样有什么不对吗?

1 个答案:

答案 0 :(得分:1)

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

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