包括静态库和头Makefile问题(C)



我的文件堆叠如下

dir1/
mylib.a
myheader.h
file.c
executable
dir2/
dependentfile.c // depends on functions in myheader.h implemented in mylib.a

我想在我的Makefile中链接我的静态库文件,而不使用它的名称,只是指示它的路径。我所拥有的如下:

为dir2/创建文件

CC = gcc
CFLAGS   = -g -Wall $(INCLUDES)
INCLUDES = -I../dir1
LDFLAGS = -g -L../dir1
exec: dependentfile.o
dependentfile.o: dependentfile.c

运行"make"只会给我带来一堆隐含的声明错误和未定义的引用,因为它没有查找我用-L和-I指定的路径。我的dependentfile.c也有一行

#include "myheader.h"

它找不到。

我应该如何修改makefile才能使其工作?我尝试了很多方法,甚至用-l指定了lib文件并编写了完整的路径,但都无济于事。

感谢您的帮助!

#编辑:想明白了!

原来我忘了LDLIBS。所以对于其他人来说,makefile最终看起来像:

CC = gcc
CFLAGS   = -g -Wall $(INCLUDES)
INCLUDES = -I../dir1
LDFLAGS = -g -L../dir1
LDLIBS = -lmylib (my actual file was named libmylib.a, forgo the "lib") 
exec: dependentfile.o
dependentfile.o: dependentfile.c

我认为你应该使用

#include "myheader.h"

您的头文件名应该加引号。

最新更新