不用自己写代码,
errno.h中提供了两个可直接使用的全局变量,GCC默认打开了宏_GNU_SOURCE:
(CUtils::get_program_name();
CUtils::get_program_short_name();)
-
const char* CUtils::get_program_name()
-
{
-
//#define _GNU_SOURCE
-
//#include <errno.h>
-
return program_invocation_name;
-
}
-
-
const char* CUtils::get_program_short_name()
-
{
-
//#define _GNU_SOURCE
-
//#include <errno.h>
-
return program_invocation_short_name;
-
}
效果代码(g++ -g -o x x.cpp):
-
//#define _GNU_SOURCE
-
#include <errno.h>
-
#include <stdio.h>
-
-
int main()
-
{
-
printf("program_invocation_name: %s\n", program_invocation_name);
-
printf("program_invocation_short_name: %s\n", program_invocation_short_name);
-
return 0;
-
}
运行结果:
# /tmp/X/x
program_invocation_name: /tmp/X/
x
program_invocation_short_name:
x
阅读(3704) | 评论(0) | 转发(0) |