简体   繁体   English

Makefile 中的 C 链接外部库

[英]C Link External Library in Makefile

I am having issues linking a library (termbox) when compiling.编译时我在链接库(术语框)时遇到问题。 I get the error:我收到错误:

make: *** No rule to make target `termbox.h', needed by `test.o'.  Stop.

Makefile:生成文件:

edit: test.o
    gcc -Wall -o edit test.o

test.o: test.c termbox/src/termbox.h
    gcc -Wall -c test.c -ltermbox/src

Include:包括:

#include "termbox/src/termbox.h"

I have also tried using the compiled library but ran into similar issues.我也尝试过使用编译库,但遇到了类似的问题。 Do I have to use some sort of combination of specifying the header file and the location of the compiled library?我是否必须使用某种指定头文件和编译库位置的组合?

The directory of my termbox folder is in the same directory as test.c.我的termbox文件夹的目录和test.c在同一个目录下。

Thanks!谢谢!

You have managed to compile and include the header file for the library, but you did not yet tell the compiler where the code (definitions) are - ie you did not tell it to link in the library yet.您已经设法编译并包含库的头文件,但是您还没有告诉编译器代码(定义)在哪里——也就是说,您还没有告诉编译器链接到库中。

You will need to do that next, this is done in a similar way to telling the linker what files to link, but with some extra syntax.您接下来需要这样做,这与告诉链接器要链接哪些文件的方式类似,但有一些额外的语法。 It appears to be a static library ( .a suffix) so you can link like this:它似乎是一个静态库( .a后缀),因此您可以像这样链接:

test.o: test.c termbox/src/termbox.h
    gcc -Wall -c test.c -Itermbox/src -Lsrc -ltermbox

Where -L... specifies where libraries can be found and -l... specifies the library name to link to minus the lib prefix and the .a or .so suffix.其中-L...指定可以找到库的位置, -l...指定要链接到的库名称减去lib前缀和.a.so后缀。 Also note that order is important, so leave the library linkage at the end.还要注意顺序很重要,所以在最后留下库链接。

More on library linking order here有关库链接顺序的更多信息,请点击此处

UPDATE更新

Sorry I added the linking to the wrong line!抱歉,我将链接添加到了错误的行中! - here is the updated answer: - 这是更新的答案:

# The linker stage
edit: test.o
    gcc -Wall -o edit test.o -Lsrc -ltermbox

# Compile stage
test.o: test.c termbox/src/termbox.h
    gcc -Wall -c test.c -ltermbox/src

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM