C++ compiler often mangle function names to support many features. Programer can suppress default name-mangling using extern "C" way. However, why int main(int, char **)
not affected ever?
// test.cpp
int max(int a, int b) {
return a > b ? a : b;
}
extern "C" {
int min(int a, int b) {
return a < b ? a : b;
}
}
int main (int argc, char *argv[]) {
return 0;
}
And
$ xcrun --sdk macosx clang -x c++ -c test.cpp -o test
$ xcrun nm -nm test
0000000000000000 (__TEXT,__text) external __Z3maxii
0000000000000030 (__TEXT,__text) external _min
0000000000000060 (__TEXT,__text) external _main
Obviously, int max(int, int)
is mangled to __Z3maxii
; int min(int int)
is free from mangling with extern "C" annotation.
How does main escape from mangling?
Is there any way else to keep name from mangling except the above annotation?