如何在 C++ 中获取当前运行的可执行文件的文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12254980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:05:25  来源:igfitidea点击:

How to Get the Filename of the Currently Running Executable in C++

c++filenames

提问by Phat Tran

I want to get the full path of the current process.

我想获取当前进程的完整路径。

I use _getcwdto get the current working directory. But it not includes file name.

_getcwd用来获取当前工作目录。但它不包括文件名。

How can I get file name like: filename.exe?

我怎样才能得到文件名,如:filename.exe

采纳答案by marcinj

On windows you can use:

在 Windows 上,您可以使用:

TCHAR szExeFileName[MAX_PATH]; 
GetModuleFileName(NULL, szExeFileName, MAX_PATH);

szExeFileName will contain full path + executable name

szExeFileName 将包含完整路径 + 可执行文件名称

[edit]

[编辑]

For more portable solution use argv[0]or some other platform specific code. You can find such aproach here: https://github.com/mirror/boost/blob/master/libs/log/src/process_name.cpp.

对于更便携的解决方案使用argv[0]或一些其他平台特定的代码。你可以在这里找到这样的方法:https: //github.com/mirror/boost/blob/master/libs/log/src/process_name.cpp

回答by Se?kin Sava???

argv[0]of your main function is your filename.

argv[0]您的主要功能是您的文件名。

A simple code snippet:

一个简单的代码片段:

#include<stdio.h>
int main(int argc, char** argv)
{
    //access argv[0] here
}

If you cannot access/change code in main(), you can do something like this:

如果您无法访问/更改 main() 中的代码,您可以执行以下操作:

std::string executable_name()
{
#if defined(PLATFORM_POSIX) || defined(__linux__) //check defines for your setup

    std::string sp;
    std::ifstream("/proc/self/comm") >> sp;
    return sp;

#elif defined(_WIN32)

    char buf[MAX_PATH];
    GetModuleFileNameA(nullptr, buf, MAX_PATH);
    return buf;

#else

    static_assert(false, "unrecognized platform");

#endif
}

回答by Ken Bloom

On Linux, the filename of your binary is the destination of a symlink at /proc/self/exe. You can use the readlinksystem call to find the destination of a symlink.

在 Linux 上,二进制文件的文件名是/proc/self/exe. 您可以使用readlink系统调用来查找符号链接的目的地。

Note that this tells you the actual location on disk where the binary is stored, not simply the command the user used to start your program.

请注意,这会告诉您二进制文件在磁盘上的实际存储位置,而不仅仅是用户用来启动程序的命令。

回答by E_net4 the Rustacean

You can usually get the executable file name from argv[0]:

您通常可以从argv[0]以下位置获取可执行文件名:

#include <stdio.h>
int main(int argc, char* argv[])
{
 printf("Running: %s\n", argv[0]);
 return 0;
}

Indeed, there are ways for an application to execl()another application (or another similar function) and override this argument. It still is unconventional for the system to change it for that sort of application.

事实上,有一些方法可以让一个应用程序到execl()另一个应用程序(或另一个类似的函数)并覆盖这个参数。系统为这种应用程序更改它仍然是非常规的。

回答by alfC

In Linux (POSIX?) there is an enviroment variable called _that contains the current process.

在 Linux (POSIX?) 中有一个名为的环境变量_,其中包含当前进程。

$ echo $_
echo

In C++

在 C++ 中

#include <stdlib.h>     /* getenv */
#include<iostream>
int main(){
    std::cout << getenv("_") << '\n';
    return 0;
}

compile

编译

$ c++ a.cpp -o a.out
$ ./a.out

prints ./a.out(or whatever is the executed line, including path).

打印./a.out(或任何执行的行,包括路径)。

This has certain advantages over the other approaches, it can be read globally (not passing argv[0]) and doesn't need file handling.

与其他方法相比,这具有某些优势,它可以全局读取(不传递argv[0])并且不需要文件处理。

回答by alfC

As others have mentioned, the name of your executableis contained in argv[0]. If you need that, you could:

正如其他人提到的,您的可执行文件的名称包含在 argv[0] 中。如果你需要,你可以:

cout << argv[0] << endl;

If you need the name of a source fileof the executable, C++ has a predefined macro you can use:

如果您需要可执行文件源文件的名称,C++ 有一个预定义的宏,您可以使用:

cout << __FILE__ << endl;

Go to hereand scroll to "Predefined macro names"

转到此处并滚动到“预定义的宏名称”

回答by vromanov

You can use program_invocation_name from errno.h

您可以使用 errno.h 中的 program_invocation_name

https://linux.die.net/man/3/program_invocation_short_name

https://linux.die.net/man/3/program_invocation_short_name

回答by gjvc

Here's a cross-platform way using boost (https://www.boost.org/)

这是使用 boost ( https://www.boost.org/)的跨平台方式

#include <iostream>
#include <boost/dll.hpp>

int main( int argc, char **argv ) {

    std::cout << "hello world, this is [" << boost::dll::program_location().filename().string() << "]" << std::endl;

    std::cout << "or [" << boost::dll::program_location().string() << "] if you're not into the whole brevity thing." << std::endl;

    return 0;
}

compiled via

编译通过

g++ -o hello_world hello_world.cpp -lboost_filesystem -lboost_system -ldl

results in the output

结果在输出

hello world, this is [hello_world]
or [/home/gjvc/tmp/hello_world] if you're not into the whole brevity thing.