How to tell g++ compiler where to search for include files?(如何告诉 g++ 编译器在哪里搜索包含文件?)
问题描述
In a "working directory" I have a lot of *.cpp and *.h files that #include
each other and files from subdirectories.
For example:
#include "first.h"
#include "second.h"
#include "dir1/third.h"
#include "dir2/fourth.h"
In my own directory (that is different from the "working" directory) I would like to create a new *.cpp and *.h file that includes one of the files from the "working" directory. For example:
#include "/root/workingdirectory/first.h"
However, it does not work. Because "first.h" might include "second.h" and "second.h" is not located in my directory. Is there a way to tell the compiler that it needs to search included files not in the current but in the working directory: /root/workingdirectory/
?
To make it even more complex, dir1
and dir2
are not located in my working directory. They are located in /root/workingdirectory2/
. So, my second question is if it is possible to resolve this problem by letting compiler know that subdirectories are located somewhere else?
I also need to add, that I do not use any environment for development and compile from the command line (using g++
).
As you've already been told, it's useful to read the manual - specifically this chapter - and even more specifically right here.
Specifically, you want
g++ -I/root/workingdirectory -I/root/workingdirectory2
Note also the documentation on #include
directive syntax, described here as:
2.1 Include Syntax
Both user and system header files are included using the preprocessing directive
#include
. It has two variants:#include <file>
This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option (see Invocation).
#include "file"
This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for
<file>
. You can prepend directories to the list of quote directories with the -iquote option. The argument of#include
, whether delimited with quote marks or angle brackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus,#include <x/*y>
specifies inclusion of a system header file named x/*y.However, if backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus,
#include "x \y"
specifies a filename containing three backslashes. (Some systems interpretas a pathname separator. All of these also interpret
/
the same way. It is most portable to use only/
.)It is an error if there is anything (other than comments) on the line after the file name.
So for example
#include "first.h"
will start looking in the same directory as the .cpp file containing this directive (or take a relative path as relative to this directory).
If you want to use the include path (specified by -I
) you should use
#include <dir1/third.h>
Usual practice is to use the #include "local.h"
form for headers inside a library/package/module (however you've chosen to organize that), and the #include <external.h>
form for headers from external/3rd-party or system libraries.
这篇关于如何告诉 g++ 编译器在哪里搜索包含文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何告诉 g++ 编译器在哪里搜索包含文件?
- Easyx实现扫雷游戏 2023-02-06
- C语言qsort()函数的使用方法详解 2023-04-26
- c++ const 成员函数,返回一个 const 指针.但是返回的指针是什么类型的 const? 2022-10-11
- C++ 数据结构超详细讲解顺序表 2023-03-25
- 我应该为我的项目使用相对包含路径,还是将包含目录放在包含路径上? 2022-10-30
- Qt计时器使用方法详解 2023-05-30
- 详解C语言中sizeof如何在自定义函数中正常工作 2023-04-09
- C语言手把手带你掌握带头双向循环链表 2023-04-03
- C语言详解float类型在内存中的存储方式 2023-03-27
- ubuntu下C/C++获取剩余内存 2023-09-18