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

时间:2014-03-01 14:03:38

标签: c static compilation makefile compiler-flags

我的文件堆叠如下

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

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

dir2 /

的Makefile
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

1 个答案:

答案 0 :(得分:0)

我认为你应该使用

#include "myheader.h"

应引用您的标题文件名。