我的目的是在每个平台(Windows、Mac Os x、Linux)中使用相同的代码获取正在运行的可执行文件的名称。
每个平台都有单独的代码来查找可执行文件的名称。
在 Windows 中有 GetModuleFileName()
函数来获取可执行文件的名称,但它适用于 Visual Studio。
我还使用了argv[0]
来获取当前可执行文件的路径。
我正在使用 gcc
编译器,我需要生成适用于所有平台(Windows、Mac Os x、Linux)的代码以获取正在运行的可执行文件的名称。
引用自 C11
标准,第 5.1.2.2.1 章,
If the value of argc is greater than zero, the string pointed to by argv[0]
represents the program name; argv[0][0]
shall be the null character if the
program name is not available from the host environment.[...]
因此,在对 argv[0]
进行空检查后,您可以使用与程序名称相同的名称。
是的,它独立于平台。