我有以下两个项目:
`/home/jagbirs/Downloads/sample/SampleExtLib1` containing:
ext_lib1.c
ext_lib1.h
Makefile
/home/jagbirs/Downloads/sample/SampleMakeTest
包含:main.c
Makefile
我正在学习如何链接外部库。我在ext_lib1.h中有一个函数
在main.c中使用,但在make后出现以下错误:
gcc -o main main.c -L/home/jagbirs/Downloads/sample/SampleExtLib1 -lext_lib1
main.c:10:22: fatal error: ext_lib1.h: No such file or directory
compilation terminated.
make: *** [all] Error 1
这两个makefile如下:
SampleMakeTest / Makefile:
LIB = main
SRC := main.c
CFLAGS := -I../SampleExtLib1
all: $(SRC)
gcc -o $(LIB) $(SRC) -L/home/jagbirs/Downloads/sample/SampleExtLib1 -lext_lib1
clean:
rm -f *.o
SampleExtLib1 / Makefile:
all: ext_lib1.o
ar r libext_lib1.a ext_lib1.o
clean:
rm -f *.o *.a
可以请我帮忙弄清楚为什么链接不正确吗?
谢谢
最佳答案
尝试添加-I
以包含库的.h
文件:
gcc -o $(LIB) $(SRC) -I/home/jagbirs/Downloads/sample/SampleExtLib1 -L/home/jagbirs/Downloads/sample/SampleExtLib1 -lext_lib1