Finding current executable's path without /proc/self/exe

前端 未结 13 823
有刺的猬
有刺的猬 2020-11-22 01:06

It seems to me that Linux has it easy with /proc/self/exe. But I\'d like to know if there is a convenient way to find the current application\'s directory in C/C++ with cros

13条回答
  •  伪装坚强ぢ
    2020-11-22 01:33

    Well, of course, this doesn't apply to all projects. Still, QCoreApplication::applicationFilePath() never failed me in 6 years of C++/Qt development.

    Of course, one should read documentation thoroughly before attempting to use it:

    Warning: On Linux, this function will try to get the path from the /proc file system. If that fails, it assumes that argv[0] contains the absolute file name of the executable. The function also assumes that the current directory has not been changed by the application.

    To be honest, I think that #ifdef and other solutions like that shouldn't be used in modern code at all.

    I'm sure that smaller cross-platform libraries also exist. Let them encapsulate all that platform-specific stuff inside.

提交回复
热议问题