[英]how to add pkg-config to a Makefile?
My Makefile looks like this :我的 Makefile 看起来像这样:
all:main.cpp
g++ -I../../DrAPI/ -Wl,--no-as-needed -ldl -lrt -o SampleApp01 $<
clean:
rm -f SampleApp01
this is what I would do without Makefile:这就是我在没有 Makefile 的情况下会做的事情:
gcc main.cpp -o test $(pkg-config --cflags --libs libmongoc-1.0)
now because of importing DrAPI I have to use Makefile to include that API as well, but I would lose libmongoc-1.0 without pkg-config.现在由于导入 DrAPI,我也必须使用 Makefile 来包含该 API,但是如果没有 pkg-config,我会丢失 libmongoc-1.0。 In that case, how should I add $(pkg-config --cflags --libs libmongoc-1.0) into my Makefile so that it works?
在这种情况下,我应该如何将 $(pkg-config --cflags --libs libmongoc-1.0) 添加到我的 Makefile 中以使其正常工作?
I would have made a Makefile
that looks like this, so it use the rules that are already in make(1)
.我会制作一个看起来像这样的
Makefile
,所以它使用make(1)
中已经存在的规则。 Try make -f /dev/null --print-data-base
to see them.尝试
make -f /dev/null --print-data-base
来查看它们。 Suggest grep
-ing COMPILE.c
and LINK.c
to get the important stuff.建议使用
grep
-ing COMPILE.c
和LINK.c
来获取重要的东西。 COMPILE.cpp
, COMPILE.C
and COMPILE.cxx
are for c++ and COMPILE.c
are for c. COMPILE.cpp
、 COMPILE.C
和COMPILE.cxx
适用于 c++, COMPILE.c
适用于 c。
#!/bin/make
# Makefile
# 2020-02-10 Anders Jackson
# Basic setup
CXX = g++
CXXFLAGS = -c -g -Wall -Wextra
CXXFLAGS += -I../../DrAPI/
LDFLAGS =
LDLIBS = -ldl -lrt
# `pkg-config --list-all | grep -e mongo` will find if installed
# Here is the libmongoc-1.0 setup.
CXXFLAGS += `pkg-config --cflags libmongoc-1.0`
LDFLAGS += `pkg-config --libs-only-L libmongoc-1.0`
LDLIBS += `pgk-config --libs-only-other libmongoc-1.0`
LDLIBS += `pkg-config --libs-only-l libmongoc-1.0`
# Adjusting for this project
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECS = main
all: $(EXECS) # Link exec from objects
$(OBJECTS): # Make all .o from .cpp (no local header files)
clean:
-rm $(OBJECTS)
-rm $(EXECS)
# eof
Notice that here there that object files only has dependencies on the source file.请注意,这里的目标文件仅依赖于源文件。 If a source file has some local header file dependencies, you need to add them manually.
如果源文件有一些本地头文件依赖项,则需要手动添加它们。
This file also see to that when linking, those -L
switches are before object files and -l
switches are after.该文件还确保在链接时,那些
-L
开关在目标文件之前,而-l
开关在之后。 If you try to just use just one pkg-config --libs
in LDFLAGS
, it will not work.如果您尝试在
LDFLAGS
只使用一个pkg-config --libs
,它将不起作用。 I also just add one thing at the time to the variables with +=
, like for LDLIBS
, as it makes it easier to see what is added.我当时也只是用
+=
向变量添加一件事,就像LDLIBS
,因为它可以更容易地查看添加的内容。
The built in rules are:内置规则是:
OUTPUT_OPTION = -o $@
# Compilation for C and C++
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $(OUTPUT_OPTION) $<
COMPILE.cpp = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $(OUTPUT_OPTION) $<
# Linking for C and C++
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@
LINK.cpp = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBES) $(LDLIBS) -o $@
It helps to understand which variable goes where.它有助于了解哪个变量去哪里。
all:main.cpp g++ -I../../DrAPI/ `pkg-config --cflags --libs libmongoc-1.0` -Wl,--no-as-needed -ldl -lrt -o SampleApp01 $< clean: rm -f SampleApp01
When I compile my C code using the GTK+ library I create a variable called GTK
and set it to pkg-config
like this:当我使用 GTK+ 库编译我的 C 代码时,我创建了一个名为
GTK
的变量并将其设置为pkg-config
如下所示:
GTK = `pkg-config --cflags --libs gtk+-3.0`
Compiling the code using:使用以下代码编译代码:
$(CC) $(CFLAGS) $(OBJECTS) $(GTK) -o $@
Where CC = gcc
, CFLAGS
are some other compiler flags (like -I ../../DrAPI
) and OBJECTS
are the source and object files.其中
CC = gcc
, CFLAGS
是其他一些编译器标志(如-I ../../DrAPI
),而OBJECTS
是源文件和目标文件。
This way your makefile could look like this:这样你的 makefile 可能看起来像这样:
DRAPI = `pkg-config --cflags --libs libmongoc-1.0`
all:main.cpp
g++ -I../../DrAPI/ -Wl,--no-as-needed -ldl -lrt $(DRAPI) -o SampleApp01 $<
clean:
rm -f SampleApp01
EDIT : Forget about this.编辑:忘记这个。 Use a
CFLAGS
variable and store pkg-config --cflags [some library, eg. gtk+-3.0]
使用
CFLAGS
变量并存储pkg-config --cflags [some library, eg. gtk+-3.0]
pkg-config --cflags [some library, eg. gtk+-3.0]
and a LIBS
variable to store pkg-config --libs [some library again]
. pkg-config --cflags [some library, eg. gtk+-3.0]
和一个LIBS
变量来存储pkg-config --libs [some library again]
。 Now your makefile could look like this:现在您的 makefile 可能如下所示:
CFLAGS = -I../../DrAPI/ -Wl,--no-as-needed -ldl -lrt `pkg-config --cflags libmongoc-1.0`
LIBS = `pkg-config --libs libmongoc-1.0`
all:main.cpp
g++ $(CFLAGS) $(LIBS) -o SampleApp01 $<
clean:
rm -f SampleApp01
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.