本文介绍了我可以在目录和子目录中搜索头文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以指示 g ++ 以递归方式搜索文件夹中的头文件?在我的示例中,我希望 g ++ 进行搜索


$ b

以及头文件的所有子目录。这可以做到吗?这里有一个简单的 Makefile 例子:

  C = g ++ 
CFLAGS = -c -Wall
LDFLAGS =
INC1 = / ARDrone_SDK_2_0_1 / ARDroneLib / Soft /< - 可以递归吗?
INCDIRS = -I $ {INC1}
SOURCES = src / dronevid.cpp
OBJECTS = $(SOURCES:.cpp = .o)
EXECUTABLE = build / dronevid
$ b $ all:$(SOURCES)$(EXECUTABLE)

$(EXECUTABLE):$(OBJECTS)
$(CC)$(LDFLAGS)$(OBJECTS) - o $ @

.cpp.o:
$(CC)$(CFLAGS)$(INCDIRS)$< -o $ @


解决方案

你在混合两种不同的工具, make g ++



无法获取 g ++ 搜索给定目录的所有子目录。每个你想用来查找包含文件的目录都必须在命令行上用 -I 标志分别指定。



如果你愿意,你可以得到 make 来构造这些参数并将它们放到命令行中。假设你正在使用GNU make,并且支持 find 命令的类UNIX系统,你可以这样做:

  INCDIRS:= $(addprefix -I,$(shell find / ARDrone_SDK_2_0_1 / ARDroneLib / Soft -type d -print))

我应该预先说,这不是一个好主意。您不知道这些目录将显示的顺序,而且您不知道是否在不同目录中有相同头文件的多个副本可能会导致问题。



通常情况下,子目录中的头文件的工作方式是将顶级目录添加到编译行中,然后在 #include 行中使用相对路径你的代码。例如:

  #include< subdir / subsubdir / header.h> 

然后添加:

  -I / top / level / dir 

g ++ 编译行。


Is is possible to instruct g++ to search a folder recursively for header files? In my example I would like g++ to search

and all subdirectories for header files. Can this be done? Here's a simple Makefile example:

C=g++
CFLAGS=-c -Wall
LDFLAGS=
INC1=/ARDrone_SDK_2_0_1/ARDroneLib/Soft/ <- can this be recursive?
INCDIRS= -I${INC1}
SOURCES=src/dronevid.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=build/dronevid

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) $(INCDIRS) $< -o $@
解决方案

The question is a little confusing because you're conflating two different tools, make and g++.

There is no way to get g++ search all subdirectories of a given directory. Every directory you want to use to find an included file must be individually specified on the command line with a -I flag.

If you want to, you can get make to construct those arguments and put them on your command line. Assuming you're using GNU make, and a UNIX-like system that supports the find command, you can do something like this:

INCDIRS := $(addprefix -I,$(shell find /ARDrone_SDK_2_0_1/ARDroneLib/Soft -type d -print))

I should just say up-front, this is not really a good idea. You don't know what order those directories will show up in, and you don't know if there are multiple copies of the same header file in different directories that might cause problems.

Generally the way headers in subdirectories are expected to work is that you add the top-level directory to the compile line, then use relative paths in the #include line in your code. Something like:

#include <subdir/subsubdir/header.h>

Then add:

-I/top/level/dir

to the g++ compile line.

这篇关于我可以在目录和子目录中搜索头文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 19:01