How to use pkg-config in Make

2019-01-14 17:38发布

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.

标签: c gcc makefile gtk
2条回答
来,给爷笑一个
2楼-- · 2019-01-14 17:58

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)
查看更多
够拽才男人
3楼-- · 2019-01-14 18:10

to address the indicated error:

"main.c:1:21: fatal error: gtk/gtk.h: No such file or directory"

the error message is saying that it cannot find the gtk/gtk.h file

correct by adding the proper path to the gcc compile statement:

'gcc -g -Wall -Wextra    -c -o main.o main.c -I./gtk' 
-- this is a capital 'I'

correct by adding the path and library name to the gcc link statement:

'gcc -g -Wall -Wextra   -o main main.o -L<pathtolibrary> -lgtk+-3.0'  
-- this is a capital 'L' then a lower case 'l'

It would also be very worth while to look into the pkg-config file to see exactly what it contains/performs

This link:
<https://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_3.html>
will give you all the info on the parameters to gcc
查看更多
登录 后发表回答