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

大兔子大兔子 提交于 2020-07-15 02:47:09

问题


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

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

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


回答1:


On windows you can use:

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

szExeFileName will contain full path + executable name

[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.




回答2:


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

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:

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
}



回答3:


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

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.




回答4:


You can usually get the executable file name from 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.




回答5:


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

$ echo $_
echo

In 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).

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




回答6:


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

cout << argv[0] << endl;

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

cout << __FILE__ << endl;

Go to here and scroll to "Predefined macro names"




回答7:


You can use program_invocation_name from errno.h

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




回答8:


Here's a cross-platform way using 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.


来源:https://stackoverflow.com/questions/12254980/how-to-get-the-filename-of-the-currently-running-executable-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!