简体   繁体   English

g++ -I 包括所有子目录与 header 文件

[英]g++ -I include all subdirectories with header files

I've just stumbled upon this post about compiling all.cpp files, including those in subdirectories using the linux find command:我刚刚偶然发现这篇关于编译 all.cpp 文件的帖子,包括使用 linux查找命令的子目录中的文件:

g++ -g $(find RootFolderName -type f -iregex ".*\.cpp") -o OutputName

The problem with this is that all files need their relative path written out when doing #include for this to work.这样做的问题是所有文件都需要在执行#include 时写出它们的相对路径才能使其工作。 You can get around it by adding what ever directory you need using the -I tag:你可以通过使用-I标签添加你需要的目录来解决它:

g++ -g $(find RootFolderName -type f -iregex ".*\.cpp") -o OutputName -I./somePath

But that's still quite a hassle if you have multiple subdirectories.但是,如果您有多个子目录,这仍然很麻烦。 Is it possible to use find again with some other regular expression to include all of the subdirectories?是否可以再次使用 find 和其他一些正则表达式来包含所有子目录?

Is it possible to use find again with some other regular expression to include all of the subdirectories?是否可以再次使用 find 和其他一些正则表达式来包含所有子目录?

Yes it is - some projects, like mbed and arduino, seem to include all possible directories to include paths.是的 - 一些项目,如 mbed 和 arduino,似乎包含所有可能的目录以包含路径。 In shell assuming there are no whitespaces, you could:shell 中假设没有空格,您可以:

find . -type f -iname '*.h' -printf "-I%h\n" | sort -u

This is error prone to whitespaces in path.这是路径中容易出现空格的错误。 When using:使用时:

command $(stuff)

you will have problems with spaces in filenames.遇到文件名中的空格问题。 Research other methods and how to handle whitespaces in shell.研究其他方法以及如何处理 shell 中的空格。 Better yet, do not write such stuff manually and reinvent the wheel and move to a build system, like cmake .更好的是,不要手动编写这些东西并重新发明轮子并转向构建系统,例如cmake

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM