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.