Compiler not following symbolic links in Visual St

2019-04-20 07:23发布

问题:

I am using Visual Studio 2008 C++ project (Visa 32 bit).

I have the following #include directive in my source code.

#include <example/header.h>

In my include path I specify the parent directory of 'example', i.e.

C:/.../include

where the full path to the header looks like

C:/.../include/example/header.h

However, 'example' is a symbolic link (A '.lnk' created via file explorer 'new shortcut'). I get the following error

c:...\foo.cpp(37) : fatal error C1083: Cannot open include file: 'example/header.h': No such file or directory

If I replace the symbolic link with the actual directory, the project will compile correctly. For practical reasons I need it to be a symbolic link. Is there anyway to make the Visual Studio pre-processor follow the link?

回答1:

The link is a symbolic link (.lnk)

Are you sure you're not creating a shortcut? Shortcuts work on a higher level than symbolic links and mean nothing to applications.

Conversely, symbolic links (if properly created) should work fine with any application that reads/writes to files/folders.

For more details, you may wish to consider reading this article about symbolic links, which explains how you can create a symbolic link using mklink.

Here's a helpful snippet from a comment on that article by "Bernard Kerckenaere":

  • shortcut: on the operating system level (to applications who wish to read/write the link, it’s just a meaningless file)

  • soft link (or symbolic link): like a shortcut, but on the filesystem level (applications reading/writing the link, will actually read/write the file linked to) ... this will work across partitions, or drives

  • hard link: only for files, what happens is that there are multiple file entries that point to the same physical data, when you delete one entry, the other will still work, the data won’t be gone until all entries are deleted (if with a soft link you delete the original directory, the link won’t work anymore!) -> you can obviously only create hard links to a file on the same partition

What you want to create is a symbolic link which you can do with the /D parameter using mlink.



回答2:

.lnk is not symbolic link, it is shortcut file for Explorer. To create hard link, use

fsutil hardlink create link_name file_name

On Vista, there is mklink utility to create symbolic links.



回答3:

Older versions of Visual Studio are a bit finicky when it comes to symlinks. Hard links on files using the \H parameter hardly ever work, but symlinking entire directories using \D is usually fine. Afterwards, you might need to remove and re-add the folder from and to your project, though.