应用场景:
多个项目(或者同一项目不同分支)需要使用同一组依赖文件时,我们希望只在其中一个项目中提供原始文件、而在其它项目中则通过类似linux中的软连接的方式引入。最终既可以减少同一依赖文件库(一个目录中的所有库)的多副本维护(必须保持统一),又可以降低对磁盘空间的占用。
有一个Dependency的文件夹,里面存放了一堆公共库,有三个项目CppDev、CppMain、CppTest中都需要使用该公共库,初始的目录结构大致如下:
D:\CppDev\Dependency
D:\CppMain\Dependency
D:\CppTest\Dependency
由上可知,Dependency保留了三个副本。为了简化,我们使用mkLink来建立软连接
mklink /d D:\CppMain\Dependency D:\CppDev\Dependency
mklink /d D:\CppTest\Dependency D:\CppDev\Dependency
这样CppMain、CppTest中就不再需要维护一个副本了。
最终发现Win7引入了Mklink命令,可以很好地契合这个需求:
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file symbolic link. /H Creates a hard link instead of a symbolic link. /J Creates a Directory Junction.
/D creates a symbolic link, or a soft link.This essentially acts like a shortcut to a folder in prior versions of Windows, except you don’t have to use an actual shortcut.
/H creates a hard link, which points directly to the file.This option can’t be used for folders directly for some reason, you’ll have to use the next option.
/J creates a “Directory Junction”A Directory Junction is actually just a hard link to a directory. This is a feature that existed prior to Vista as well. If you are trying to symlink to a directory using a hard link, then you should use this option.
Understanding Hard vs Soft Links================================Hard Link
A hard link directly points to the file, and acts to the operating system as if it is the file itself. You’ll want to use this option the majority of the time if you are trying to fake an application’s directory.
Soft Link
A soft link is essentially a shortcut to a file or folder – if you are using Windows explorer, you’ll be redirected to the directory if you double-click on a shortcut, it won’t pretend its part of the filesystem. You can still directly reference or open a file with the symlinked path, and it mostly works.