简体   繁体   中英

Makefile for plugins compilation

I have this simple structure:

.
test.c
plugins/a.c
plugins/b.c
plugins/c.c

And I'm compiling this with a bash script:

gcc -o test test.c -std=gnu99 -ldl -Wl,--export-dynamic

gcc -c plugins/a.c -o plugins/a.o -pedantic -g -Wall -std=c99 -fpic -I.
gcc plugins/a.o -o plugins/a.so  -shared

...same for b and c...

Anyways, I want to port that to a Makefile. Here's what I have:

CC          =   gcc

PLUGIN_DIR  =   plugins
PLUGINS_C   =   $(wildcard $(PLUGIN_DIR)/*.c)
PLUGINS_O   =   $(patsubst %.c,%.o, $(PLUGINS_C))

new: clean all

all: test plugins

test: test.o
    $(CC) -o $@ $^ -std=gnu99 -ldl -Wl,--export-dynamic

plugins:
    ???

$(PLUGIN_DIR)/*.c:
    $(CC) -c $(PLUGIN_DIR)/$@ $^ -pedantic -g -Wall -std=c99 -fpic -I.

$(PLUGIN_DIR)/*.o:
    $(CC) $@ $^ -shared

clean:
    rm -rf test *.o *.a plugins/*.o plugins/*.so

But this won't work as the plugins rule is empty and I really can't find out what should I write in there to make it compile all the plugins inside the plugins folder. Also, I'm not sure if I messed up things with $@ and $^ .

There are a number of problems with your makefile before we get to your question.

The wildcard character in a rule target is % not * (in lines like your $(PLUGIN_DIR)/*.c: ).

Rules specify how the files named by the target/target pattern are built. So your %.c rule is telling make how to build .c files (which I trust you'll agree) isn't exactly what you meant. (Similarly your %.o rule is telling make how to build .o files).

You don't have any prerequisites (right-hand side of : in a target line) listed for your build rules so make cannot intelligently rebuild targets as their prerequisites are changed (and will never rebuild them instead).

To get to your question, you likely don't want anything in the body of the plugins target. Instead you want to list the desired plugins output files (the .so files) as the prerequisites of the plugins target. (You will also want to include .PHONY: plugins in your makefile to tell make that plugins is not a real file but instead a phony target.)

Your %.so rule wants to be more like:

$(PLUGINS_DIR)/%.so: $(PLUGINS_DIR)/%.o
        $(CC) $^ -o $@ -shared

and your %.o rule wants to be more like:

$(PLUGINS_DIR)/%.o: $(PLUGINS_DIR)/%.c
        $(CC) -c $< -o $@ -pedantic -g -Wall -std=c99 -fpic -I.

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