简体   繁体   中英

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.

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:

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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