Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找C+的执行路径+;程序 < C++ >命令行程序中,我需要找到执行路径,即路径。从哪里调用exe文件。例如,如果文件是在D:\stored\location\中存储的,但它是从其他目录调用的,比如D:\executed\here\,我应该将D:\executed\here\作为路径。 命令提示符应类似于以下内容:- D:\executed\here> D:\stored\location\program_name.exe getpath Path of execution is : D:\executed\here_C++_Path - Fatal编程技术网

查找C+的执行路径+;程序 < C++ >命令行程序中,我需要找到执行路径,即路径。从哪里调用exe文件。例如,如果文件是在D:\stored\location\中存储的,但它是从其他目录调用的,比如D:\executed\here\,我应该将D:\executed\here\作为路径。 命令提示符应类似于以下内容:- D:\executed\here> D:\stored\location\program_name.exe getpath Path of execution is : D:\executed\here

查找C+的执行路径+;程序 < C++ >命令行程序中,我需要找到执行路径,即路径。从哪里调用exe文件。例如,如果文件是在D:\stored\location\中存储的,但它是从其他目录调用的,比如D:\executed\here\,我应该将D:\executed\here\作为路径。 命令提示符应类似于以下内容:- D:\executed\here> D:\stored\location\program_name.exe getpath Path of execution is : D:\executed\here,c++,path,C++,Path,我尝试使用GetModuleFileName,正如前面解释的那样,但是我得到了\storage\here 有没有跨平台的方法来找到“执行路径” 注意:-我使用的是VSCode 编辑:编译器的版本是:g++(i686-posix-dwarf-rev0,由MinGW-W64项目构建)8.1.0 旁注:-由于某种原因,在包含#include之后,std::filesystem显示错误:“std::filesystem”尚未声明,因此我无法使用filesystem.h回答C++17 我们现在用G+ +

我尝试使用GetModuleFileName,正如前面解释的那样,但是我得到了
\storage\here

有没有跨平台的方法来找到“执行路径”

注意:-我使用的是VSCode

编辑:编译器的版本是:g++(i686-posix-dwarf-rev0,由MinGW-W64项目构建)8.1.0 旁注:-由于某种原因,在包含
#include
之后,
std::filesystem
显示错误:“std::filesystem”尚未声明,因此我无法使用filesystem.h

回答C++17

我们现在用G+ + 6.3来运输,我认为这是相当古老的。所以,我写了另一个。


顺便说一句,我在
的帮助下解决了同样的问题
几天前

我已将相关部分放入MCVE:

// Declaration (Header):

// standard C++ header:
#include <filesystem>

// returns file path of this executable
std::filesystem::path getExecPath();

/**************************************************************************/

// Definition (C++ Source):

// standard C++ header:
#include <string>

// OS header:
#ifdef _MSC_VER // Is this MSVC?
#include <windows.h>
#else // (not) _MSC_VER // Then it's hopefully Linux/g++.
#include <unistd.h>
#endif // _MSC_VER

std::filesystem::path getExecPath()
{
#ifdef _MSC_VER // Is this MSVC?
  std::wstring path(1024, L'\0');
  const DWORD len
    = GetModuleFileNameW(NULL, &path[0], (DWORD)path.size());
  if (!len) return std::filesystem::path(); // ERROR!
  path.resize(len);
  return std::filesystem::path(path);
#else // (not) _MSC_VER // Then it's hopefully Linux/g++.
  std::string path(1024, '\0');
  ssize_t len
    = readlink("/proc/self/exe", &path[0], path.size());
  if (len < 0) return std::filesystem::path(); // ERROR!
  path.resize(len);
  return path;
#endif // _MSC_VER
}

/**************************************************************************/

// Test:

// standard C++ header:
#include <iostream>

int main()
{
  std::cout
    << "Exec. Path:   " << getExecPath() << '\n'
    << "Current Dir.: " << std::filesystem::current_path() << '\n';
}
注:coliru上的
g++
版本为(撰写本文时)


我添加了一个
CMakeLists.txt
来在我的本地框中测试它:

项目(ExecPath)
cmake_最低要求(3.10.0版)
设置\u属性(全局属性使用上的\u文件夹)
套装(CMAKE_CXX_标准17)
设置(上需要CMAKE\U CXX\U标准)
设置(CMAKE_CXX_扩展关闭)
if(UNIX)
#std::C++17中添加的文件系统
#似乎需要一个额外的lib.in g++。
#这可能与版本有关。。。
链接库(-lstdc++fs)
endif()
设置(CMAKE_运行时_输出_目录“${CMAKE_二进制_目录}/bin”)
添加_可执行文件(testExecPath testExecPath.cc)

MCVE构建并运行于2019年视觉研究中

输出:

Exec.Path:“D:\\ds32737\\Entwicklung\\tests\\C++\\execPath\\build-VS2019\\bin\\Debug\\testExecPath.exe”
当前目录:“D:\\ds32737\\Entwicklung\\tests\\C++\\execPath\\build-VS2019”

最后,我在DebianLinux上测试了这一点(在带有g++8.3.0的VM中)

测试会话:

ds32737@debian:/mnt/hostd/Entwicklung/tests/C++/execPath$mkdir build debian
ds32737@debian:/mnt/hostd/Entwicklung/tests/C++/execPath$cd build debian/
ds32737@debian:/mnt/hostd/Entwicklung/tests/C++/execPath/build debian$cmake。。
--C编译器标识为GNU8.3.0
--CXX编译器标识为GNU 8.3.0
--检查C编译器是否工作:/usr/bin/cc
--检查C编译器是否工作:/usr/bin/cc--works
--检测C编译器ABI信息
--检测C编译器ABI信息-完成
--检测C编译特性
--检测C编译特性-完成
--检查CXX编译器是否工作:/usr/bin/c++
--检查CXX编译器是否正常工作:/usr/bin/c++--正常工作
--检测CXX编译器ABI信息
--检测CXX编译器ABI信息-完成
--检测CXX编译特性
--检测CXX编译功能-完成
--配置完成
--生成完成
--生成文件已写入:/mnt/hostd/Entwicklung/tests/C++/execPath/Build debian
ds32737@debian:/mnt/hostd/Entwicklung/tests/C++/execPath/build debian$cmake--build。
扫描目标testExecPath的依赖项
[50%]构建CXX对象cmakfiles/testExecPath.dir/testExecPath.cc.o
[100%]链接CXX可执行文件bin/testExecPath
[100%]构建的目标testExecPath
ds32737@debian:/mnt/hostd/Entwicklung/tests/C++/execPath/build debian$bin/testExecPath
Exec.Path:“/mnt/hostd/Entwicklung/tests/C++/execPath/build debian/bin/testExecPath”
当前目录:“/mnt/hostd/Entwicklung/tests/C++/execPath/build debian”
ds32737@debian:/mnt/hostd/Entwicklung/tests/C++/execPath/build debian$
g++8.3
是我最近安装的Debian的默认版本。它有点过时了

因此,我必须添加
-lstdc++fs
,以解决
std::filesystem
的链接问题。 请注意,我在coliru中使用的
g++10.2


请注意,在任何情况下,我都注意到当前工作目录不是可执行文件所在的目录

为了解决这个任务,我考虑了编码问题。 (在过去,每当文件路径中出现ASCII字符以外的所有字符时,我常常会遇到文件系统和编码问题。)

因此,我在Windows实现中使用了
GetModuleHandleW()
,它以UTF-16返回文件路径

在Linux上,我假设总是使用UTF-8

严格地说,
std::filesystem::path
甚至不是真正必要的。 相反,路径可以返回,例如作为
std::string
。 为此,Windows impl.可以将UTF-16转换为UTF-8,以在任何平台上提供授权的编码。

C++17的答案

我们现在用G+ + 6.3来运输,我认为这是相当古老的。所以,我写了另一个。


顺便说一句,我在
的帮助下解决了同样的问题
几天前

我已将相关部分放入MCVE:

// Declaration (Header):

// standard C++ header:
#include <filesystem>

// returns file path of this executable
std::filesystem::path getExecPath();

/**************************************************************************/

// Definition (C++ Source):

// standard C++ header:
#include <string>

// OS header:
#ifdef _MSC_VER // Is this MSVC?
#include <windows.h>
#else // (not) _MSC_VER // Then it's hopefully Linux/g++.
#include <unistd.h>
#endif // _MSC_VER

std::filesystem::path getExecPath()
{
#ifdef _MSC_VER // Is this MSVC?
  std::wstring path(1024, L'\0');
  const DWORD len
    = GetModuleFileNameW(NULL, &path[0], (DWORD)path.size());
  if (!len) return std::filesystem::path(); // ERROR!
  path.resize(len);
  return std::filesystem::path(path);
#else // (not) _MSC_VER // Then it's hopefully Linux/g++.
  std::string path(1024, '\0');
  ssize_t len
    = readlink("/proc/self/exe", &path[0], path.size());
  if (len < 0) return std::filesystem::path(); // ERROR!
  path.resize(len);
  return path;
#endif // _MSC_VER
}

/**************************************************************************/

// Test:

// standard C++ header:
#include <iostream>

int main()
{
  std::cout
    << "Exec. Path:   " << getExecPath() << '\n'
    << "Current Dir.: " << std::filesystem::current_path() << '\n';
}
注:coliru上的
g++
版本为(撰写本文时)


我添加了一个
CMakeLists.txt
来在我的本地框中测试它:

项目(ExecPath)
cmake_最低要求(3.10.0版)
设置\u属性(全局属性使用上的\u文件夹)
套装(CMAKE_CXX_标准17)
设置(上需要CMAKE\U CXX\U标准)
设置(CMAKE_CXX_扩展关闭)
if(UNIX)
#std::C++17中添加的文件系统
#似乎需要一个额外的lib.in g++。
#这可能与版本有关。。。
链接库(-lstdc++fs)
endif()
设置(CMAKE_运行时_输出_目录“${CMAKE_二进制_目录}/bin”)
添加_可执行文件(testExecPath testExecPath.cc)

MCVE构建并运行于2019年视觉研究中

输出:

执行路径: