Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

main in namespace

Tags:

c++

namespaces

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

namespace somenamespace{  int main(void){  return 0; }  } 
like image 502
shuttle87 Avatar asked Oct 18 '10 04:10

shuttle87


People also ask

Can main be in a namespace?

The main that is called at program startup must be in the global namespace.

What is namespace example?

In an operating system, an example of namespace is a directory. Each name in a directory uniquely identifies one file or subdirectory. As a rule, names in a namespace cannot have more than one meaning; that is, different meanings cannot share the same name in the same namespace.

What namespace contains?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

What is C++ namespace?

Namespace is a feature added in C++ and is not present in C. A namespace is a declarative region that provides a scope to the identifiers (names of functions, variables or other user-defined data types) inside it. Multiple namespace blocks with the same name are allowed.


2 Answers

3.6.1/1 - "A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [ Note: in a freestanding environment, start-up and termination is implementation-defined; startup contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. —end note ]

Your example has 'main' (intended as the program entry point) as a namespace function and hence your code is ill-formed. This does not mean that a function 'main' can not be defined as you did. It just means that a global namespace scope definition of 'main' in accordance with the Standard defined signature is required for a free standing program. hosted program

like image 62
Chubsdad Avatar answered Sep 23 '22 23:09

Chubsdad


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.

like image 36
Omnifarious Avatar answered Sep 26 '22 23:09

Omnifarious