简体   繁体   English

makefile CFLAGS 忽略 $(mysql_config --libs)?

[英]makefile CFLAGS ignore $(mysql_config --libs)?

I make a toy makefile example to test mysql, but the makefile does not recognize mysql_config.我制作了一个玩具makefile示例来测试mysql,但是makefile无法识别mysql_config。 this is the makefile script:这是生成文件脚本:

CFLAGS = -g -O2  -Wall -Wextra -Isrc -rdynamic $(OPTFLAGS)
LDLIBS = $(OPTLIBS)
SOURCES =$(wildcard *.c)
OBJECTS = asd
all: LDLIBS += $(mysql_config --libs_r) -lm
     CFLAGS += -Isrc $(mysql_config --cflags)
all: $(OBJECTS)

When i run make all, it only execute:当我运行 make all 时,它只执行:

cc -g -O2  -Wall -Wextra -Isrc -rdynamic  -Isrc     asd.c   -lm -o asd

Where did all the mysql CFLAGS and LDLIBS go?所有 mysql CFLAGS 和 LDLIBS 都去哪儿了? Or is there something wrong with my script?还是我的脚本有问题?

this returns when i type 'mysql_config --cflags' in the shell, for demonstration:当我在 shell 中键入 'mysql_config --cflags' 时返回,用于演示:

-I/usr/include/mysql

The content $(mysql_config --libs_r) is intended to ask the shell to invoke that command and replace the string with its output.内容$(mysql_config --libs_r)旨在要求shell调用该命令并将字符串替换为其输出。

But, make uses the $(...) syntax to expand variables.但是, make使用$(...)语法来扩展变量。 So, your attempt at running a shell command mysql_config --libs_r is actually being interpreted as expanding a make variable named mysql_config --libs_r , of which there is not one, and so you get an empty string here.因此,您尝试运行 shell 命令mysql_config --libs_r实际上被解释为扩展名为mysql_config --libs_r的 make 变量,其中没有一个,因此您在这里得到一个空字符串。

You need to escape the $(...) syntax from make so that it's passed to the shell.您需要从 make 中转义$(...)语法,以便将其传递给 shell。

Also, your indentation seems to imply you want both LDLIBS and CFLAGS to be target-specific variables on the all target, however if that's really what you want you have to use a backslash at the end of the first line.此外,您的缩进似乎在暗示你要LDLIBS和CFLAGS既要对目标特定变量all目标,但如果这就是你要真的是你必须在第一行的末尾使用反斜杠。 Simply indenting the line doesn't make it a continuation of the previous line.简单地缩进该行并不能使其成为上一行的延续。

You want this:你要这个:

all: LDLIBS += $$(mysql_config --libs_r) -lm \
     CFLAGS += -Isrc $$(mysql_config --cflags)

There are some efficiency issues with this as it will run mysql_config twice for every compile and link operation.这存在一些效率问题,因为它会为每个编译和链接操作运行两次mysql_config Much more efficient would be something like:更有效的将是这样的:

mysql_LIBS := $(shell mysql_config --libs_r)
mysql_FLAGS := $(shell mysql_config --cflags)

then use the make variables $(mysql_LIBS) and $(mysql_FLAGS)然后使用 make 变量$(mysql_LIBS)$(mysql_FLAGS)

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

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