在include指令中使用尖括号和引号有什么区别?
#包括<文件名>#包括“文件名”
在include指令中使用尖括号和引号有什么区别?
#包括<文件名>#包括“文件名”
当前回答
#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"
其他回答
它确实:
"mypath/myfile" is short for ./mypath/myfile
具有是#include所在文件的目录和/或编译器的当前工作目录,和/或default_include_path
and
<mypath/myfile> is short for <defaultincludepaths>/mypath/myfile
如果./在<default_include_paths>中,则不会有任何区别。
如果mypath/myfile位于另一个include目录中,则行为未定义。
要使用gcc查看系统上的搜索顺序,根据当前配置,可以执行以下命令。您可以在此处找到有关此命令的详细信息
cpp-v/dev/null-o/dev/null
Apple LLVM版本10.0.0(clang-1000.10.44.2)目标:x86_64-apple-darwin18.0.0线程模型:posix InstalledDir:Library/Developer/CommandLineTools/usr/bin“/Library/Developer/CommandLineTools/usr/bin/clag”-cc1-三重x86_64-apple-macsx10.14.0-不推荐使用objc-Weror=已弃用的objc isa用法-E-禁用free-禁用llvm验证器-丢弃值名称-主文件名null-mrelocation model pic-pic level 2-mthread model posix-mdisable fp elim-fno strict return-masm verbose-munwind tables-target cpu penryn-dwarf column info-debugger tuning=lldb-target linker version 409.12-v-resource-dir/库/开发人员/命令行工具/usr/lib/clag/10.0.0-isysroot/库/开发人员/命令行工具/sdk/MacOSX10.14.sdk-我/usr/local/include-fdebug compilation dir/Users/hogstrom-fferror limit 19-fmessage长度80-堆栈保护器1-fblocks-fencode扩展块签名-fbojc runtime=macosx-10.14.0-fmax type align=16-fdiagnostics show option-fcolor diagnostics-传统cpp-o-x c/dev/nullclang-cc1版本10.0.0(clang-1000.10.44.2)默认目标x86_64-apple-darwin18.0.0忽略不存在的目录“/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/local/include”忽略不存在的目录“/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/Libraries/Frameworks”#包括“…”搜索从这里开始:#包括<…>搜索从这里开始:/usr/local/include/库/开发人员/命令行工具/usr/lib/clag/10.0.0/include/库/开发人员/命令行工具/usr/include/库/开发人员/命令行工具/sdk/MacOSX10.14.sdk/usr/include/库/开发人员/命令行工具/SDKs/MacOSX10.14.sdk/System/Library/Frameworks(框架目录)搜索列表结束。
#include <filename>
当您想使用C/C++系统或编译器库的头文件时使用。这些库可以是stdio.h、string.h、math.h等。
#include "path-to-file/filename"
当您希望使用自己的自定义头文件(位于项目文件夹或其他位置)时使用。
有关预处理器和标头的详细信息。读取C-预处理器。
带有尖括号的#include将搜索“依赖于实现的位置列表”(这是一种非常复杂的方式来表示“系统头”)以查找要包含的文件。
带引号的#include将只搜索文件(并且,“以依赖于实现的方式”,bleh)。这意味着,在正常的英语中,它将尝试应用你扔给它的路径/文件名,而不会预先设置系统路径或篡改它。
此外,如果#include“”失败,则标准会将其重新读取为#include<>。
gcc文档有一个(特定于编译器的)描述,虽然它是特定于gcc而非标准的,但它比ISO标准的律师式描述更容易理解。
#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"