Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

c++ - g++ include all /usr/include recursively

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...