在include指令中使用尖括号和引号有什么区别?

#包括<文件名>#包括“文件名”


当前回答

“”将搜索。/第一然后搜索默认包含路径。您可以使用以下命令打印默认包含路径:

gcc -v -o a a.c

以下是一些例子,让事情更清楚:代码交流工作

// a.c
#include "stdio.h"
int main() {
        int a = 3;
        printf("a = %d\n", a);
        return 0;

}

b.c的代码也有效

// b.c
#include <stdio.h>
int main() {
        int a = 3;
        printf("a = %d\n", a);
        return 0;

}

但当我在当前目录中创建名为stdio.h的新文件时

// stdio.h
inline int foo()
{
        return 10;
}

a.c将生成编译错误,但b.c仍然有效

和“”,<>可以与相同的文件名一起使用。因为搜索路径优先级不同。所以直流电也起作用

// d.c
#include <stdio.h>
#include "stdio.h"
int main()
{
        int a = 0;

        a = foo();

        printf("a=%d\n", a);

        return 0;
}

其他回答

#include "filename" // User defined header
#include <filename> // Standard library header.

例子:

这里的文件名是Seller.h:

#ifndef SELLER_H     // Header guard
#define SELLER_H     // Header guard

#include <string>
#include <iostream>
#include <iomanip>

class Seller
{
    private:
        char name[31];
        double sales_total;

    public:
        Seller();
        Seller(char[], double);
        char*getName();

#endif

在类实现中(例如,Seller.cpp,以及将使用文件Seller.h的其他文件中),现在应该包含用户定义的头,如下所示:

#include "Seller.h"

对于#include“”,编译器通常搜索包含该include的文件的文件夹,然后搜索其他文件夹。对于#include<>,编译器不会搜索当前文件的文件夹。

预处理器的确切行为因编译器而异。以下答案适用于GCC和其他几个编译器。

#include<file.h>告诉编译器在其“include”目录中搜索头文件,例如,对于MinGW,编译器将在C:\MinGW\include\或安装编译器的任何位置搜索file.h。

#include“file”告诉编译器在当前目录(即源文件所在的目录)中搜索文件。

您可以使用GCC的-I标志告诉它,当它遇到带有尖括号的include时,它还应该在-I之后的目录中搜索标头。GCC将把标志后面的目录当作includes目录。

例如,如果您在自己的目录中有一个名为myheader.h的文件,那么如果您使用标志-I调用GCC,可以说#include<myheader.h>。(表示应搜索当前目录中的includes。)

如果没有-I标志,则必须使用#include“myheader.h”来包含文件,或将myheader.h移动到编译器的include目录。

#include <abc.h>

用于包含标准库文件。因此编译器将检查标准库头所在的位置。

#include "xyz.h"

将告诉编译器包含用户定义的头文件。因此编译器将在当前文件夹或-I定义的文件夹中检查这些头文件。

#include <filename>

当您想使用C/C++系统或编译器库的头文件时使用。这些库可以是stdio.h、string.h、math.h等。

#include "path-to-file/filename"

当您希望使用自己的自定义头文件(位于项目文件夹或其他位置)时使用。

有关预处理器和标头的详细信息。读取C-预处理器。