如何使用软链接?(how does include work with soft links?)

编程入门 行业动态 更新时间:2024-10-07 18:30:55
如何使用软链接?(how does include work with soft links?)

我想知道当包含或includee在同一目录中仅作为软链接时引用包含会发生什么。

想象一下三个标题, a , b和c ,以及目录d 。 a和b在CWD中, c在d下。 现在想象一下d下的软链接指向b (名为b )。

如果仅使用引用包括且c包括包含b ,则将在目录d搜索,这是到b的软链接的位置,还是在CWD中的b的实际物理位置?

我在MSDN上看过一页,但我找不到明确的答案。 “与包含#include语句的文件相同的目录”是什么意思?

I wonder what happens to quoted inclusion when included or includee are in the same directory only as a soft link.

Imagine three headers, a, b and c, and a directory d. a and b are in CWD and c is under d. Now imagine a soft link under d pointing to b (named b).

If only quoted includes are used and c includes b which includes a, will a be searched for in directory d, which is the location of the soft link to b, or the actual physical location of b, which is in CWD?

I've read a page at MSDN but I could not find a definite answer. What is the meaning of "same directory as the file that contains the #include statement"?

最满意答案

解决软链接的机制不考虑当前工作目录,而是考虑链接解析的目录。 让我演示一个更自然的例子,如果你试图读取/usr/include/X11/Xlib.h (这是编译器在linux中搜索<X11/Xlib.h>之一,并且碰巧/usr/include/X11是../../opt/X11/include的软链接,算法解析为/usr/include/X11 ../../opt/X11/include /usr/include/X11 (或者实际上是/opt/X11/include ,在解决所有路径组件之后,独立于当前目录。 当前或根目录是在您的u区域(专用于您的进程的操作系统资源的一部分)中保留的inode表条目,以便启动机制或将名称解析为inode条目任务,最终处理到inode您要打开的文件。 这对于编译器是透明的,因此尽管已经说过它是实现定义的,但是所有posix系统都有一个简单的机制来搜索最终文件的系统相关目录列表。 但是软链接机制是透明的,您不需要期望特殊的编译器行为。

编译器将唯一考虑其当前工作目录的时间是,如果您实际强制它执行,如:

#include "./header.h"

要么

#include "../../includes/myprojectheader.h"

编辑

如果软链接指向文件(而不是目录),则显示此示例:

假设您的安装已将ncurses包含在/usr/include中的一个子目录中(假设为ncurses ),并且您希望使用#include <ncurses.h> ,因此您已安排以下内容:

ln -s ncurses/curses.h curses.h

你将有一个指向文件/usr/include/curses.h的链接,它指向ncurses/curses.h然后当你打开/usr/include/curses.h ,内核到达路径的最后一部分( curses.h )并且看到它是一个符号链接,因此它替换最后一部分(因为指向的链接不是绝对的)并用ncurses/curses.h替换它,并打开文件/usr/include/ncurses/curses.h 。

The mechanism to solve softlinks doesn't take into account the current working directory, but the directory at which the link is being resolved. Let me demonstrate with a more natural example, if you try to read /usr/include/X11/Xlib.h (this is one of the places where the compiler is going to search for <X11/Xlib.h> in linux, for example) and it happens that /usr/include/X11 is a soft link to ../../opt/X11/include, the algorithm resolves to /usr/include/../../opt/X11/include (or actually /opt/X11/include finally, after solving all the path components), independently of your current directory. Current or root directory are inode table entries that are reserved in your u-area (the part of the operating system resources devoted to your process) in order to initiate the mechanism or resolving the name to inode entry task, that finally deals to the inode of the file you want to open. This is transparent for the compiler, so despite it has been said that it is implementation defined, it happens that all posix systems have a simple mechanism of a searching a system dependant list of directories for the final files. But the soft link mechanism is transparent and you don't need to expect special compiler behaviour.

The only time wher the compiler will consider its current working directory, is if you actually force it to do, as in:

#include "./header.h"

or

#include "../../includes/myprojectheader.h"

edit

in the case the soft link point to a file (and not to a directory) is shown with this example:

Suppose your installation has put the ncurses includes in one subdir (Let's say ncurses) in /usr/include, and you want to use #include <ncurses.h>, so you have arranged the following:

ln -s ncurses/curses.h curses.h

you'll have a link to the file /usr/include/curses.h which points to ncurses/curses.h then when you open /usr/include/curses.h the kernel arrives to the last part of the path (curses.h) and sees it is a symbolic link, so it substitutes the last part (as the pointed link is not absolute) and replaces it with ncurses/curses.h, and opening the file /usr/include/ncurses/curses.h.

更多推荐

本文发布于:2023-07-30 14:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338797.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   链接   include   links   soft

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!