本文介绍了发现在C函数调用方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

嘿所有,我只是想知道是否有可能获得程序的名字那是一个函数内运行?

下面是一个例子:

说我叫: ./ runProgram

 的main(){一个();}函数A(){//可以检索runProgram如果我不能用主要的ARGC(argv的)常数?
}


解决方案

编译器相关的,因此:

  $ CC --version
i686的 - 苹果darwin10-GCC-4.2.1(GCC)4.2.1(苹果公司建立5646)

请程序

  $更多x.c
INT主(INT ARGC,CHAR *的argv []){
      的printf(程序:%S \\ N的argv [0]);
    富();
}
INT富(){
}$做出点¯x
CC x.c -o点¯x
x.c:在函数'主':
x.c:2:警告:内建函数'printf的不兼容的隐式声明
$ ./x
程序:./x

获取ARGC / V瓦尔的全局名称

  $纳米./x
0000000100000efe小号存根帮手
0000000100001048Ð_NXArgc
0000000100001050Ð_NXArgv
0000000100001060Ð___progname
00000001亿一__mh_execute_header
0000000100001058Ð_environ
                 ü_exit
0000000100000eebŧ_foo
0000000100000eb8ŧ_main
                 ü_printf
0000000100001020小号_pvars
                 üdyld_stub_binder
0000000100000e7cŧ启动

添加全局名称,声明为extern,并保持兼顾截断。

  $更多x2.c
INT主(INT ARGC,CHAR *的argv []){
      的printf(程序:%S \\ N的argv [0]);
    富();
}
INT富(){
    焦炭的extern ** NXArgv;
    的printf(在富:%S \\ n,NXArgv [0]);}

运行恐怖

  $作X2
CC x2.c -o X2
x2.c:在函数'主':
x2.c:2:警告:内建函数'printf的不兼容的隐式声明
x2.c:在函数'富':
x2.c:9:警告:内建函数'printf的不兼容的隐式声明
$ ./x2
程序:./x2
在富:./x2

请不要告诉我妈妈。

Hey all, I'm just wondering if it is possible to get the name of the program thats running within a function?

Here's an example:

Say I called: ./runProgram

main() {

A();

}

function A() {

// Possible to retrieve "runProgram" if I cannot use main's argc(argv) constants??
}
解决方案

Compiler dependent, so:

$ cc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5646)

Make the program

$ more x.c
int main(int argc, char *argv[]) {
      printf("program: %s\n", argv[0]);
    foo();
}


int foo() {
}

$ make x
cc     x.c   -o x
x.c: In function ‘main’:
x.c:2: warning: incompatible implicit declaration of built-in function ‘printf’
$ ./x
program: ./x

Get the global name of the argc/v vars

$ nm ./x
0000000100000efe s  stub helpers
0000000100001048 D _NXArgc
0000000100001050 D _NXArgv
0000000100001060 D ___progname
0000000100000000 A __mh_execute_header
0000000100001058 D _environ
                 U _exit
0000000100000eeb T _foo
0000000100000eb8 T _main
                 U _printf
0000000100001020 s _pvars
                 U dyld_stub_binder
0000000100000e7c T start

Add the global name, declared as extern, and keep into account the mangling.

$ more x2.c
int main(int argc, char *argv[]) {
      printf("program: %s\n", argv[0]);
    foo();
}


int foo() {
    extern char **NXArgv;
    printf("in foo: %s\n", NXArgv[0]);

}

Run the horror

$ make x2
cc     x2.c   -o x2
x2.c: In function ‘main’:
x2.c:2: warning: incompatible implicit declaration of built-in function ‘printf’
x2.c: In function ‘foo’:
x2.c:9: warning: incompatible implicit declaration of built-in function ‘printf’
$ ./x2
program: ./x2
in foo: ./x2

Please don't tell my mom.

这篇关于发现在C函数调用方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:47