_getcwd 不返回当前工作目录,而是返回程序存储的位置

_getcwd is not returning the current working directory, but instead the place where the program is stored

我在某个目录A中定义了一个程序,在另一个目录B中定义了该程序的快捷方式,并将B添加到PATH中。在另一个目录 C 中,我是 运行 cmd.exe 并且正在使用命令 progname.lnk arg1 arg2 执行该程序。我的理解是这种情况下的"current working directory"是C,但是当我使用_getcwd(见下文)获取当前工作目录时,我得到的是目录A,程序所在的地方。

#include <iostream>
#include <direct.h>
#define GCWD _getcwd

int main() {
  char cwd[256];
  GCWD(cwd, 256);
  std::cout << cwd << std::endl;
}

我用命令 mwe.lnk 编译并 运行 这个程序。该程序存储在 A 中,在 B 中有一个指向它的快捷方式,我从 C 中 运行 它,就像我的实际程序一样。

此代码由 computinglife 的answer to a related question, which is the same technique used here告知。

我对 "current working directory" 的理解正确吗?好像和computinglife第一段的回答很一致,那我哪里做错了?

创建快捷方式时,Windows 将它们配置为将工作目录设置为存储程序的位置。在快捷方式属性中清空此字段解决了这个问题。