0

I'm working on a quite large and messy project that I haven't really got time to clean up and I've been tasked with providing an API for it. What I'm trying to do is wrapping all the ugly stuff up in a single static library and then provide it with a single header file.

But when trying to build an executable using the header file and library I get a bunch of "undefined reference" errors. I guess it would work if I provided all the header files, but I really don't want to have to do this.

Right now the makefile looks something (slightly altered for simplicity) like this:

myprog.elf : libgiant_lib.a main.o
    gcc -pg -o $@ main.o -lgiant_lib

main.o : main.c
    gcc -c $(CFLAGS) $(SOME_INCLUDES) $< -o $@

libgiant_lib.a : $(A_BUNCH_OF_LIBS) $(COUPLE_OF_OBJS) api.o
    ar rcs $@ $^

api.o : api.c api.h
    gcc -c $(CFLAGS) $(INCLUDES_FOR_BUNCH_OF_LIBS) $< -o $@

I've checked the libgiant_lib.a archive and it seems to contain all the libraries and object files it should. But still, linking against it doesn't work.

I'm pretty sure I'm missing something obvious here...

1 Answers1

0

Looks to me that your include files may have references to other include files. So, you will need to provide a flag to indicate the location of those include files. I'll suggest providing the location (directory containing all include files) as

INCL_DIR=/include/dir/location

and including it in the compilation phase using the -I flag. For example, your rule for api.o will become

api.o: api.c api.h
        gcc -c $(CFLAGS) -I$(INCL_DIR) $(INCLUDES_FOR_BUNCH_OF_LIBS) $< -o $@
unxnut
  • 8,509
  • 3
  • 27
  • 41