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

时间:2022-12-17 15:07:35

I'm trying to compile a simple program, with

我正在尝试编译一个简单的程序

#include <gtkmm.h>

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.

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

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?

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

3 个解决方案

#1


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

在这种情况下,正确的做法是在Makefile或buildscripts中使用pkg-config:

# 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 $@

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

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

$ 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.

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.)

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

#2


I guess you are not using a makefile? The only thing that could be annoying is having to type the long -I option each time you compile your program. A makefile makes it a lot easier.

我猜你没有使用makefile?唯一令人烦恼的是每次编译程序时都必须输入long -I选项。 makefile使它变得更容易。

For example, you could modify the hello world makefile from wikipedia to something like the following:

例如,您可以将*的hello world makefile修改为以下内容:

INC=-I/usr/include/gtkmm-2.4/

helloworld: helloworld.o
    g++ -o $@ $<

helloworld.o: helloworld.c
    g++ $(INC) -c -o $@ $<

.PHONY: clean

clean:
    rm -f helloworld helloworld.o

#3


You can't. The whole point of include paths is so you can pick and choose what you want and what versions.

你不能。包含路径的重点是你可以选择你想要的和什么版本。

What you could do is..

你能做的是......

#include <gtkmm-2.4/gtkmm.h>

Which would achieve the same effect.

哪个会达到同样的效果。

#1


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

在这种情况下,正确的做法是在Makefile或buildscripts中使用pkg-config:

# 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 $@

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

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

$ 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.

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.)

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

#2


I guess you are not using a makefile? The only thing that could be annoying is having to type the long -I option each time you compile your program. A makefile makes it a lot easier.

我猜你没有使用makefile?唯一令人烦恼的是每次编译程序时都必须输入long -I选项。 makefile使它变得更容易。

For example, you could modify the hello world makefile from wikipedia to something like the following:

例如,您可以将*的hello world makefile修改为以下内容:

INC=-I/usr/include/gtkmm-2.4/

helloworld: helloworld.o
    g++ -o $@ $<

helloworld.o: helloworld.c
    g++ $(INC) -c -o $@ $<

.PHONY: clean

clean:
    rm -f helloworld helloworld.o

#3


You can't. The whole point of include paths is so you can pick and choose what you want and what versions.

你不能。包含路径的重点是你可以选择你想要的和什么版本。

What you could do is..

你能做的是......

#include <gtkmm-2.4/gtkmm.h>

Which would achieve the same effect.

哪个会达到同样的效果。