如何在Make中使用pkg-config [英] How to use pkg-config in Make

查看:190
本文介绍了如何在Make中使用pkg-config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

SOURCES = $(通配符* .c)
EXECUTABLES = $(patsubst%.c,%,$( SOURCES))

全部:$(EXECUTABLES)

它告诉我这个:

  gcc -g -Wall -Wextra -c -o main.o main.c 
main.c: 1:21:致命错误:gtk / gtk.h:没有这样的文件或目录
#include< gtk / gtk.h>
^
编译终止。
< builtin> ;: recipe for target'main.o'failed
make:*** [main.o]错误1

我在Makefile中将$(pkg-config --cflags --libs gtk + -3.0)放在哪里可以编译?



非常感谢您的帮助。

解决方案

有两个问题。



首先,你的 CFLAGS 这一行是错误的:你忘了说 gtk + -3.0 pkg-config 部分中,所以 pkg-config 会抛出一个错误:

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

其次,更重要的是, $(...)被自己截取代换。事实上,你已经看到了这一点:

  SOURCES = $(通配符* .c)
EXECUTABLES = $ (patsubst%.c,%,$(SOURCES))

全部:$(EXECUTABLES)


$

有两件事你可以做。



首先,您可以使用`...`,它可以做同样的事情( $(...)是新的shell语法)。

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

第二,要使用GNU make,您可以使用上面链接的答案Basile Starynkevitch中显示的 shell 替换命令:

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


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

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

However, if I use Make it doesnt work:

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?

Thanks very much in advance for your kind help.

解决方案

There are two issues.

First, your CFLAGS line is wrong: you forgot to say gtk+-3.0 in the pkg-config part, so pkg-config will spit out an error instead:

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:

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

all: $(EXECUTABLES)

is all done by make.

There are two things you can do.

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

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 shell substitution command, which was shown in the answer Basile Starynkevitch linked above:

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

这篇关于如何在Make中使用pkg-config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆