Why can't main () in C ++ be inlined?

I read the C ++ FAQ, and I noticed one suggestion.

main () cannot be inline.

Why is this?

+62
c ++ main inline
Aug 08 2018-11-11T00:
source share
17 answers

In C ++, it is not practical to call the main function in your code, so there would be no relation to it.

+104
Aug 08 '11 at 11:01
source share

Because the standard says this:

[2003: 3.6.1/3] : The main function should not be used (3.2) inside the program. the relationship (3.5) main is determined by the implementation. A program that declares that the core is embedded or static is poorly formed. The main name is not otherwise reserved. [Example: member functions, classes, and enumerations can be called principal, as well as entities in other namespaces. ]

And why does he say that? Because he is trying to leave as much as possible about the main implementation for the individual .. well, the implementation .. as possible, and does not want to limit the implementation, requiring the inline be valid here when it may not be of practical use.




My friend on the committee confirmed this:

There is no reason why inline main() will not work on its own. [..] I could use a C ++ interpreter that can call inlined main() . [..] [But] inline / static main() forbidden to hope to avoid confusion. It’s hard for me to imagine that the justification would be something additional to what has already been said in [this Q & A].




By the way, do not confuse the inline hint keyword with the actually built-in functions. You can mark the inline function and it cannot be physically embedded.

So, even if it’s true that main “cannot be embedded” (and, strictly speaking, this is not true, although embedding main would be rather inconvenient and pointless, as explained in other answers), it could theoretically support the inline hint inline just fine.

This is not for the reason mentioned above, and in the lethal answer: it will complicate the situation without real benefits.

+65
Aug 08 '11 at 12:27
source share

The C runtime library must find this symbol in order to “know” which function to run.

+27
Aug 08 '11 at 11:02
source share

You cannot directly call main () (this is forbidden in C ++), so it makes no sense to embed it.

+17
Aug 08 '11 at 11:01
source share

Normally, main() is called from the init() system functions. Thus, main() requires exactly one definition .

Now, if we can inline use the main() function and include it in the header file, then for each translation unit there will be a different definition for main() . It is unacceptable. You can declare main() in namespace and inline . But not global main() .

+14
Aug 08 2018-11-11T00:
source share

firstly, you need to understand how the function works with the built-in

Example:

  inline void f() { int a = 3; a += 3; cout << a; } int main() { f(); return 0; } 

will look like a compiler:

  int main() { int a = 3; a += 3; cout << a; return 0; } 

looking at this example, how do you want to make the main input? This method is installed immediately.

+10
Aug 08 2018-11-11T00:
source share

Others noted that a call to main cannot be meaningfully embedded at the machine code level. This is trash. This will require a little help from the linker (for example, global optimization) or recompilation of the run-time library bits in one instance, but this is quite doable without a technical problem.

However, the effect of inline prompting that calls should preferably be inlined does not matter for a function that is called only once and at the top control level, since main .

The only guaranteed inline effect is to allow the definition of an external binding function (identically) in two or more translation units, i.e. influencing the rule of one definition.

In practical terms, this allows you to put the definition in the header file and put it in the header file is also practically necessary to ensure identical definitions.

This does not make sense for main , so there is no reason for main be inline .

+7
Aug 08 2018-11-12T00:
source share

The C ++ standard states that the main function cannot be inlined, in response to @Tomalak Geret'kal. This answer discusses the possibility of nesting the main function, whether there was a limitation in standard deletion.

Inline definition
The inline is a suggestion to the compiler to insert the contents of an in-situ function. One of the intentions is to remove the overhead when calling and returning from a function (subroutine).

An important embedding situation is when there is a pointer to a function. In this case, there must be at least one static copy of the function. In this case, the linker may allow "external links" to the built-in function, since there is one static version.

It is important to note that the compiler and linker determine whether to insert content or call one instance of a function.

It should also be noted that functions that are not marked by the programmer can also be built in by the compiler.

Attachment of the main function
Since only one call to main allowed , how is it associated with the compiler. Individual instances of built-in functions may be standard. The compiler is allowed to convert the inlined function into a function call for a single instance. Thus, the compiler ignores the built-in clause for the main function.

The compiler and linker must ensure that there is only one instance of the main function built-in. This is where the hard part comes in, especially with external communication. One process for providing a single instance is to leave information that the translation has a “core” function, regardless of whether it is built-in. Note. When a built-in function is called, the compiler is allowed to remove the function from the symbol tables for external binding, since the idea is that the function will not be called by external functions.

Summary
Technically, there is nothing to prevent the inclusion of the main function. A mechanism already exists to convert built-in functions into individual instances and to identify multiple instances of a function. When there is a pointer to an inline function, one instance of the function is created, so it has an address. This mechanism satisfies the requirements of the runtime library for main with an address. In the case of inline for the main function, it will be ignored, but there should be no reason to prevent this syntax (except for confused people). In the end, there are already cases of syntax that are redundant, for example, declaring a parameter that is passed by value (copy) as const .

"This is just my opinion, I could be wrong." - Dennis Miller, comedian.

+7
Aug 08 '11 at 16:32
source share

You can only define main once. Therefore, setting inline will not be any purpose - inline has only a significant goal for functions that you can define several times in the program (all definitions will be processed as if there was only one definition, and all definitions should be the same).

Since inline functions can be defined several times in a program, and inline also serves to quickly make calls to an inline labeled function, the Standard requires inline functions defined in each translation unit in which it is used. Therefore, compilers usually discard the definition of a function if it is inline , and this function was not used by the code in the current translation unit. It would be completely wrong to do this for main , which shows that inline and semantics of main completely incompatible.

Please note that the question in the heading "Why can't main () in C ++ be inlined?" and the expression that you quote from the Standard refers to different things. You ask if a function can be built-in, which is usually understood as fully or partially inserting the code of the called function into the calling function. A simple designation of the inline function does not imply inclusion of this function at all. This is a complete compiler solution, and of course, if you never call main (and you cannot do this), then there is nothing to embed.

+6
Aug 08 '11 at 16:41
source share

If you linked statically with CRT and enabled some link-time linking-attachments (e.g. MSVC), it might be possible to embed it.

But that doesn't make sense. It will be called once, and this function-function call is almost negligible compared to everything else that is performed before the first line in the main line is executed.

...

Aaand, this is an easy way to make a character appear only once in your executable. :)

+3
Aug 08 '11 at 11:10
source share

There are a number of key reasons. In principle, main is called from the main initialization execution routine, and only from there. This code was (obviously) compiled without knowing that your main was embeddable. Modern compiler technology is capable of embedding module boundaries, but it is an advanced feature not supported by many older compilers. And, of course, the benefits of inlining are present when a function is called very often; by definition, main will be called exactly once, no more, no less.

+2
Aug 08 2018-11-18T00:
source share

I see that the standard says so, but the real practical answer would be as simple as stating that the runtime added to each C and C ++ program should invoke the executable at some point. This function must have an external character (and an address at startup) so that the linker can find it to be called at the start of execution. Therefore, you cannot declare it as inline , because the built-in compiler does not generate an external character for it.

+2
Aug 09 2018-11-11T00:
source share

Since its main () function, which starts execution when the code is compiled into a binary file, everything is in main() . so you can say that it is already built in!

And yes, it is illegal to use inline for your C ++ program, which is more about the syntax!

+1
Aug 08 2018-11-11T00:
source share

For most compiler / archetype combinations, the main() function in the source becomes a reasonably normal function in the final binary. This is due only to the fact that it is convenient for these archaeologists, and not because the standard says that it is.

In limited arsenals of memory, many compilers that produce flat binary code (e.g., intex hex format) instead of a dynamic container-compatible linker (e.g., an elf or xcoff) optimize the entire template template, as it simply swells. Some architectures do not support calls at all functions (on these platforms only a limited subset of C ++ is possible).

To support the widest variety of such architectures and building environments, the standard selection keeps the semantics of main() as open as possible, so that the compiler can do what is right for a wide variety of platforms. This means that many of the functions available in the language as a whole cannot be applied to launching and terminating the application itself.

If you need something like inline main() (or reentrant or any fancy function), you can, of course, call the main function:

 inline int myMain(int argc, char **argv) { /* whatever */ } int main(int argc, char **argv) { return myMain(argc, argv); } 
+1
Aug 08 '11 at 19:59
source share

Built-in functions have static volume by default. This means that if we declare main () as inline, then the scope will be limited to the file in which it is defined. However, the C start-up library (provided by the compiler provider) needs the “main” to be a global symbol. There are some compilers that allow you to change the function of the entry point (for example, main) using the linker flags.

+1
Sep 16 '11 at 13:57
source share

built-in functions usually do not have an address, so there is no portable way to call main, main () needs an address that init code can go to. Nested functions are designed to get stuck in the calling function, if the main one is built-in, it must be embedded in the program initialization code, which is also not portable.

0
Aug 10 '11 at 7:10
source share
operating system

loads binary data into memory; looks for an entry point ("main" character in c / C ++); makes a far-reaching transition to addres label entry points. The operating system does not know anything about the main function of your code until the program is downloaded.

-2
Aug 08 '11 at 11:16
source share



All Articles