main in namespace

前端 未结 3 673
鱼传尺愫
鱼传尺愫 2021-02-05 01:43

Why doesn\'t this compile? The linker can\'t find main, but why is this the case?

namespace somenamespace{

int main(void){
 return 0;
}

}
3条回答
  •  攒了一身酷
    2021-02-05 02:18

    The linker is required to arrange for the program's execution to start in a global function called "main". The whole point of benig able to create your own namespaces - as you've done - is to avoid putting things in the global namespace so they won't be accidentally picked up by other code or the linker. You're explicitly saying "I'm calling this function main, but that's only meaningful within the context of somenamespace - if you don't know about somenamespace you won't find or be able to use it".

    Implementation wise, the mangled name of your main symbol has been changed from the expected name due to the namespace, so the linker just doesn't find the symbol table entry it needs.

提交回复
热议问题