Calvin Wu
Calvin Wu

Reputation: 21

can do static library link against shared library in the gnu makefile?

  1. I try to make a gnu makefile to generate a static library,and I need some function from a shared library, so I link a shared library in the makefile, but it get a failure about undefined reference to `fooo'
  2. I also noticed the compile log don't find the -lshared_lib

next fragment is what I wrote in the makefile

.cpp.o: 
    $(CC) $(CFLAGS) $(DEBUGFLAG) $(WARNING) -lshared_lib -c $*.cpp

all: $(LIB_NAME)
$(LIB_NAME) : $(OBJS)
    $(rm) $@
    $(AR) $@ $(OBJS)
    $(RANLIB) $@

Upvotes: 2

Views: 465

Answers (1)

MadScientist
MadScientist

Reputation: 100956

Well, first you can't "link" a static library at all. You don't use a linker to generate static library, as you can see above you use an archiver (ar).

Since a static library is not linked, you cannot link a shared library with it. A static library is just a bundled set of object files (you can think of it just like a tar file containing object files, with a table of contents (symbol map) added), so it can't refer to a shared library.

If you want your static library to use functions that are in some shared library, then that shared library has to be listed on the link line of the program that links in the static library.

You must have had such a recipe in your makefile otherwise you wouldn't have received errors about undefined references: references are not evaluated when you create a static library (the ar command doesn't resolve all references), so you won't get errors when creating it. References are only expanded by the linker, when linking. Since we don't have that part of the makefile available to use we can't advise you on how to fix it.

Upvotes: 3

Related Questions