Bionix1441 Bionix1441 - 1 year ago 69
C Question

Rule not recognized in makefile?

I have a basic makefile to compile C files necessary for compiling a shared library, I have an error because the makefile is not recognized:


$(CC) -fPIC -c $^ -o $@

sharedlib: $(OBJECTS)/%.o
$(CC) -shared -o $<

When I run make I get that there is no rule for target:
needed by
. While the rule is written right before sharedlib.

Answer Source

The main problem is that nowhere you are explicitly telling make what your source files are. Start by doing that:

SOURCES=$(wildcard $(SOURCEDIR)/*.c)

Then, derive the object file names from the source file names by substituting .c for .o:

OBJECTS=$(patsubst $(SOURCEDIR)/%.c,$(OBJECTDIR)/%.o,$(SOURCES))

You can still keep your generic rule to build object files:

        $(CC) -fPIC -c $^ -o $@

But you give the explicit list of object files to the rule to make sharedlib: $(OBJECTS)
        $(CC) -shared -o $@ $<

Note that I made the name of the rule the same as the file that is being generated. That's important, because calling make twice in a row will then skip building the library a second time. You can always add an alias if you want:


In that case, it's also good to tell make that sharedlib is not a real file:

.PHONY sharedlib

This prevents weird things from happening if you ever did have a file called sharedlib in the directory.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download