1

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!

  • 3
    Just to be sure, does `termbox.h` exist in the same directory as your .c file (your current directory, where you run `make`)? You mentioned your "termbox folder" is in the same directory, but it's not clear where your .h is. You need to be more specific about where your files are relative to where your makefile is. – lurker Dec 07 '19 at 20:19
  • 1
    (error is from make, not related to compiling or linking: the command is not even executed) – user2864740 Dec 07 '19 at 20:20
  • termbox.h is in the relative directory /termbox/src/termbox.h – Eli Musgrove Dec 07 '19 at 20:28
  • 1
    And is your `Makefile` all in the same directory? Please provide a listing of your source tree. – kaylum Dec 07 '19 at 20:36
  • Project directory: test.c, Makefile, termbox termbox: src, build, etc... src: termbox.h, termbox.c, etc... build: src, etc... src: libtermbox.a, etc... – Eli Musgrove Dec 07 '19 at 20:38
  • 2
    You need `test.o: test.c termbox/src/termbox.h`. (or something like that - can't tell the exact structure from your comment). That is, you need to tell Makefile where the header file is. – kaylum Dec 07 '19 at 20:39
  • 1
    Make is looking for `termbox.h` in the same folder as `test.c`, and can't find it there, so it looks for a target that tells it how to create it and isn't finding one. If `termbox.h` is not in the same folder as `test.c`, then you need to tell make where to find it by specifying the path to `termbox.h`. – Ken White Dec 07 '19 at 20:43
  • `-ltermbox/src` is totally wrong, – n. m. could be an AI Dec 07 '19 at 21:10
  • @n.'pronouns'm. How can I specify the location of the header file then? – Eli Musgrove Dec 07 '19 at 21:18
  • `-l` does not specify the location of a header file. I guess you are confusing the lowercase `l` with the uppercase `I`. – n. m. could be an AI Dec 07 '19 at 21:21
  • Wow ok that seems to be the issue. Now there seems to be the ever present error "Undefined symbols for architecture x86_64" but at least thats figured out. Thank you... – Eli Musgrove Dec 07 '19 at 21:28

1 Answers1

0

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 - i.e. 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
code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • Is anything special necessary to take the .o file and make an executable when linking the library? I get weird errors related to my clang compiler when I try to create an executable. – Eli Musgrove Dec 08 '19 at 20:43
  • I updated the answer - sorry, I had added the linkage to the wrong makefile line. No there is nothing special you need to do to create an executable - that is the default output type. If you wanted to create a shared/static lib then you need to specify other options but what you have not (or at least in the updated answer) should be right. I can't try it for you without the source project : ( – code_fodder Dec 10 '19 at 06:40