C语言 如何在 Make 中使用 pkg-config

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28533059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:45:27  来源:igfitidea点击:

How to use pkg-config in Make

cgccmakefilegtk

提问by Jenia Ivanov

I want to compile the simplest GTK program. I can compile it using the command line:

我想编译最简单的GTK程序。我可以使用命令行编译它:

gcc $(pkg-config --cflags --libs gtk+-3.0)  main.c -o main.o

However, if I use Make it doesnt work:

但是,如果我使用 Make 它不起作用:

CFLAGS=-g -Wall -Wextra $(pkg-config --cflags)
LDFLAGS=$(pkg-config --libs gtk+-3.0)
CC=gcc

SOURCES=$(wildcard *.c)
EXECUTABLES=$(patsubst %.c,%,$(SOURCES))

all: $(EXECUTABLES)

It tells me this:

它告诉我:

gcc -g -Wall -Wextra    -c -o main.o main.c
main.c:1:21: fatal error: gtk/gtk.h: No such file or directory
 #include <gtk/gtk.h>
                     ^
compilation terminated.
<builtin>: recipe for target 'main.o' failed
make: *** [main.o] Error 1

Where do I stick $(pkg-config --cflags --libs gtk+-3.0) in the Makefile to make it compile?

我在哪里粘贴 $(pkg-config --cflags --libs gtk+-3.0) 在 Makefile 中以使其编译?

Thanks very much in advance for your kind help.

非常感谢您的帮助。

回答by andlabs

There are two issues.

有两个问题。

First, your CFLAGSline is wrong: you forgot to say gtk+-3.0in the pkg-configpart, so pkg-configwill spit out an error instead:

首先,你的CFLAGS台词是错误的:你忘记gtk+-3.0pkg-config部分中说,所以pkg-config会吐出一个错误:

CFLAGS=-g -Wall -Wextra $(pkg-config --cflags gtk+-3.0)

Second, and more important, $(...)is intercepted by make itself for variable substitution. In fact, you've seen this already:

其次,更重要的$(...)是,被 make 自身拦截以进行变量替换。事实上,你已经看到了这一点:

SOURCES=$(wildcard *.c)
EXECUTABLES=$(patsubst %.c,%,$(SOURCES))

all: $(EXECUTABLES)

is all done by make.

都是由make完成的。

There are two things you can do.

你可以做两件事。

First, you can use `...`instead, which does the same thing ($(...)is newer shell syntax).

首先,您可以`...`改用它,它执行相同的操作($(...)是较新的 shell 语法)。

CFLAGS=-g -Wall -Wextra `pkg-config --cflags gtk+-3.0`
LDFLAGS=`pkg-config --libs gtk+-3.0`

Second, since you seem to be using GNU make, you can use the shellsubstitution command, which was shown in the answer Basile Starynkevitch linked above:

其次,由于您似乎在使用 GNU make,您可以使用shell替换命令,该命令显示在上面链接的 Basile Starynkevitch 答案中:

CFLAGS=-g -Wall -Wextra $(shell pkg-config --cflags gtk+-3.0)
LDFLAGS=$(shell pkg-config --libs gtk+-3.0)