g++ 递归包含所有/usr/include

2022-01-23 00:00:00 g++ c++

我正在尝试用

#include <gtkmm.h>

gtkmm.h 的路径是 /usr/include/gtkmm-2.4/gtkmm.h.g++ 看不到这个文件,除非我特别告诉它-I/usr/include/gtkmm-2.4.

The path to gtkmm.h is /usr/include/gtkmm-2.4/gtkmm.h. g++ doesn't see this file unless I specifically tell it -I /usr/include/gtkmm-2.4.

我的问题是,如何让 g++ 自动递归查看 /usr/include 中包含的所有头文件的所有目录,为什么这不是默认操作?

My question is, how can I have g++ automatically look recursively through all the directories in /usr/include for all the header files contained therein, and why is this not the default action?

推荐答案

在这种情况下,正确的做法是在你的 Makefile 中使用 pkg-config 或构建脚本:

In this case, the correct thing to do is to use pkg-config in your Makefile or buildscripts:

# Makefile
ifeq ($(shell pkg-config --modversion gtkmm-2.4),)
  $(error Package gtkmm-2.4 needed to compile)
endif

CXXFLAGS += `pkg-config --cflags gtkmm-2.4`
LDLIBS += `pkg-config --libs gtkmm-2.4`

BINS = program
program_OBJS = a.o b.o c.o

all: $(BINS)

program: $(program_OBJS)
        $(CXX) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@

# this part is actually optional, since it's covered by gmake's implicit rules
%.o: %.cc
        $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

如果您缺少 gtkmm-2.4,这将产生

If you're missing gtkmm-2.4, this will produce

$ make
Package gtkmm-2.4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtkmm-2.4.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtkmm-2.4' found
Makefile:3: *** Package gtkmm-2.4 needed to compile.  Stop.

否则,您将获得所有合适的路径和库,而无需手动指定它们.(检查 pkg-config --cflags --libs gtkmm-2.4 的输出:这远远超过您想要手动输入的内容.)

Otherwise, you'll get all the appropriate paths and libraries sucked in for you, without specifying them all by hand. (Check the output of pkg-config --cflags --libs gtkmm-2.4: that's far more than you want to type by hand, ever.)

相关文章