简体   繁体   English

用于插件编译的Makefile

[英]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: 我正在用bash脚本进行编译:

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. 无论如何,我想将其移植到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. 但是,这是行不通的,因为plugins规则为空,而且我真的找不到要在其中写什么内容以使其编译plugins文件夹内的所有plugins 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. 在我们解决您的问题之前,您的makefile存在许多问题。

The wildcard character in a rule target is % not * (in lines like your $(PLUGIN_DIR)/*.c: ). 规则目标中的通配符为 not * (在$(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. 因此,您的%.c规则告诉make如何构建.c文件(我相信您会同意)并不完全是您的意思。 (Similarly your %.o rule is telling make how to build .o files). (类似地,您的%.o规则告诉make如何构建.o文件)。

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). 您没有为构建规则列出任何先决条件(在目标行中:的右侧),因此make不能智能地重建目标,因为其先决条件已更改(并且永远不会重建它们)。

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. 而是要列出所需的插件输出文件(.so文件)作为plugins目标的前提条件。 (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.) (您还将希望在makefile文件中包含.PHONY: plugins ,以告知make plugins不是真实文件,而是虚假目标。)

Your %.so rule wants to be more like: 您的%.so规则希望更像是:

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

and your %.o rule wants to be more like: 并且您的%.o规则希望更像是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM