main in namespace

前端 未结 3 668
鱼传尺愫
鱼传尺愫 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:30

    The linker is looking for ::main, not ::somenamespace::main. The main that is called at program startup must be in the global namespace.

    @Chubsdad has pointed you at the relevant language in the standard that states this. But the standard is now written in a bizarre 'standardese' that bears a lot of resemblance to legalese. I felt a plain english statement of what was going on might be better.

    Note: There is a reason the standard is written this way. In the standard you want every term you use to have a very precise and well-defined meaning, and you do not want that meaning to vary depending on context because it makes the standard much harder to interpret. This is actually very similar to the reason that legalese looks the way it does.

提交回复
热议问题