Linux 中C/C++ search path(头文件搜索路径)

摘要:
很困惑,所以我想在Linux下找到这个文件。下图显示了通过Linux命令的搜索结果:locateunist。h: 在这么多路径中查找同名文件,但不知道哪个unistd。h使用。因此,必须找到g++或gcc头文件的搜索路径。方法我在C/C++搜索路径MJN-CSDN博客中找到了一种方法。

https://blog.csdn.net/BjarneCpp/article/details/76135980

起因

我拿到了一套Linux下的C++代码,代码中有这个头文件#include <unistd.h>,在Windows上查看缺少这个头文件,而这个头文件就是Linux中的系统文件。

困惑

因此我想在Linux下去查找这个文件。通过Linux指令:

locate unistd.h

查找结果下图所示: 
图一

查找出来这么多路径下的同名文件,不知道要用哪一个unistd.h。因此必须要找g++或者gcc头文件搜索路径。

方法

我在这里 C/C++ search path(头文件搜索路径) - MJN - CSDN博客 找到了办法。

这里 GCC The C Preprocessor: Search Path 讲述了如何去查看GCC的搜索路径。我把内容粘贴到这里:

Search Path

By default, the preprocessor looks for header files included by the quote form of the directive #include “file” first relative to the directory of the current file, and then in a preconfigured list of standard system directories. For example, if /usr/include/sys/stat.h contains #include “types.h”, GCC looks for types.h first in /usr/include/sys, then in its usual search path.

For the angle-bracket form #include , the preprocessor’s default behavior is to look only in the standard system directories. The exact search directory list depends on the target system, how GCC is configured, and where it is installed. You can find the default search directory list for your version of CPP by invoking it with the -v option. For example,

cpp -v /dev/null -o /dev/null

There are a number of command-line options you can use to add additional directories to the search path. The most commonly-used option is -Idir, which causes dir to be searched after the current directory (for the quote form of the directive) and ahead of the standard system directories. You can specify multiple -I options on the command line, in which case the directories are searched in left-to-right order.

If you need separate control over the search paths for the quote and angle-bracket forms of the ‘#include’ directive, you can use the -iquote and/or -isystem options instead of -I. See Invocation, for a detailed description of these options, as well as others that are less generally useful.

If you specify other options on the command line, such as -I, that affect where the preprocessor searches for header files, the directory list printed by the -v option reflects the actual search path used by the preprocessor.

Note that you can also prevent the preprocessor from searching any of the default system header directories with the -nostdinc option. This is useful when you are compiling an operating system kernel or some other program that does not use the standard C library facilities, or the standard C library itself.

解决

我在Linux终端执行了这条指令:

cpp -v /dev/null -o /dev/null

显示效果如图所示: 
图二

最后我根据之前locate unistd.h查找到的路径(看图一),比对图二中的路径,确定就能在/usr/include路径下,找到我想要的unistd.h文件。

免责声明:文章转载自《Linux 中C/C++ search path(头文件搜索路径)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Tomcat常用的过滤器Unity中InputField 报错 FormatException: Input string was not in the correct format下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

GCC编译器原理(一)------交叉编译器制作和GCC组件及命令

1.1 交叉编译器制作 默认安装的 GCC 编译系统所产生的代码适用于本机,即运行 GCC 的机器,但也可将 GCC 安装成能够生成其他的机器代码。安装一些必须的模块,就可产生多种目标机器代码,而且可通过命令行选择一种希望使用的代码。 1.1.1 目标机 从网站 http://gcc.gnu.org/install/specific.html 可以得到有可...

LXC/KVM虚拟化基本概念

LXC:其名称来自Linux软件容器(Linux Containers)的缩写.LXC利用cgroups与名称空间的功能,提供应用软件一个独立的操作系统环境。LXC不需要Hypervisor这个软件层,软件容器(Container)本身极为轻量化,提升了创建虚拟机的速度。严格来说不算是虚拟化技术,只是进程隔离和资源限制,软件Docker被用来管理LXC的环...

Kali Linux 安装教程-转

rootoorotor昨天折腾了 Kali Linux 1.0,把大概的配置过程记录下来,希望对想接触或使用Kali Linux的同学有所帮助。 请注意:1.本文为面向新手的教程,没技术含量,没事瞎折腾,感觉好玩…..如果您可以熟练使用Debian Linux或者使用过Arch Linux、Gentoo或者是自己LFS你完全可以无视本文。 2.如果您使用k...

glibc升级,解决glib版本过低的问题

Debian wheezy下的glibc版本为2.13,安装几个软件都运行不了,报以下类似错误:xxxx: /lib/i386-linux-gnu/i686/cmov/libc.so.6: version `GLIBC_2.15' not found (required by xxxx)解决方法:把glibc升级到2.15版本:添加软件源,刷新系统: ec...

Linux中使用gdb dump内存

在应急响应中,我们往往会有dump出某一块内存下来进行分析的必要。今天要讲的是利用gdb命令dump出sshd进程的内存。 按照 Linux 系统的设计哲学,内核只提供dump内存的机制,用户想要dump什么样的内存,dump多少内存是属于策略问题,由用户来决定。 在真实的使用场景中,主要有两种使用方式: 一种是dump某一个进程的地址空间来供用户在进程...

Linux DHCP 服务器配置与管理

一、环境介绍:   运行软件:VMware Workstation Pro 14   系统环境:CentOS-7-x86_64-1810 二、操作配置: 1、DHCP 服务器搭建 1)安装DHCP yum install dhcp 2) 设置虚拟机为仅主机模式、关闭虚拟机依靠主机的 DHCP 服务 3) 配置dhcpd.conf (动态获取IP)...