Why name-mangling has no effect on main function in C++?












3














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?










share|improve this question




















  • 1




    Is it inconceivable that the compiler makes an exception for this one?
    – The Quantum Physicist
    Nov 20 at 13:02






  • 1




    There's no need to mangle main: it is UB to define anything with the name main unless it is int main() xor int main(int, char**).
    – YSC
    Nov 20 at 13:03










  • out of curiosity: why do you care?
    – user463035818
    Nov 20 at 13:10










  • Say main doesn't have to explicitly return anything, and nobody cares. Don't mangle its name, and people start losing their mind :)
    – StoryTeller
    Nov 20 at 13:15










  • Not much. Just curious. @user463035818
    – Hans Allen
    Nov 20 at 13:19


















3














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?










share|improve this question




















  • 1




    Is it inconceivable that the compiler makes an exception for this one?
    – The Quantum Physicist
    Nov 20 at 13:02






  • 1




    There's no need to mangle main: it is UB to define anything with the name main unless it is int main() xor int main(int, char**).
    – YSC
    Nov 20 at 13:03










  • out of curiosity: why do you care?
    – user463035818
    Nov 20 at 13:10










  • Say main doesn't have to explicitly return anything, and nobody cares. Don't mangle its name, and people start losing their mind :)
    – StoryTeller
    Nov 20 at 13:15










  • Not much. Just curious. @user463035818
    – Hans Allen
    Nov 20 at 13:19
















3












3








3







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?










share|improve this question















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?







c++ main name-mangling






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 13:05









YSC

20.4k34594




20.4k34594










asked Nov 20 at 13:02









Hans Allen

163




163








  • 1




    Is it inconceivable that the compiler makes an exception for this one?
    – The Quantum Physicist
    Nov 20 at 13:02






  • 1




    There's no need to mangle main: it is UB to define anything with the name main unless it is int main() xor int main(int, char**).
    – YSC
    Nov 20 at 13:03










  • out of curiosity: why do you care?
    – user463035818
    Nov 20 at 13:10










  • Say main doesn't have to explicitly return anything, and nobody cares. Don't mangle its name, and people start losing their mind :)
    – StoryTeller
    Nov 20 at 13:15










  • Not much. Just curious. @user463035818
    – Hans Allen
    Nov 20 at 13:19
















  • 1




    Is it inconceivable that the compiler makes an exception for this one?
    – The Quantum Physicist
    Nov 20 at 13:02






  • 1




    There's no need to mangle main: it is UB to define anything with the name main unless it is int main() xor int main(int, char**).
    – YSC
    Nov 20 at 13:03










  • out of curiosity: why do you care?
    – user463035818
    Nov 20 at 13:10










  • Say main doesn't have to explicitly return anything, and nobody cares. Don't mangle its name, and people start losing their mind :)
    – StoryTeller
    Nov 20 at 13:15










  • Not much. Just curious. @user463035818
    – Hans Allen
    Nov 20 at 13:19










1




1




Is it inconceivable that the compiler makes an exception for this one?
– The Quantum Physicist
Nov 20 at 13:02




Is it inconceivable that the compiler makes an exception for this one?
– The Quantum Physicist
Nov 20 at 13:02




1




1




There's no need to mangle main: it is UB to define anything with the name main unless it is int main() xor int main(int, char**).
– YSC
Nov 20 at 13:03




There's no need to mangle main: it is UB to define anything with the name main unless it is int main() xor int main(int, char**).
– YSC
Nov 20 at 13:03












out of curiosity: why do you care?
– user463035818
Nov 20 at 13:10




out of curiosity: why do you care?
– user463035818
Nov 20 at 13:10












Say main doesn't have to explicitly return anything, and nobody cares. Don't mangle its name, and people start losing their mind :)
– StoryTeller
Nov 20 at 13:15




Say main doesn't have to explicitly return anything, and nobody cares. Don't mangle its name, and people start losing their mind :)
– StoryTeller
Nov 20 at 13:15












Not much. Just curious. @user463035818
– Hans Allen
Nov 20 at 13:19






Not much. Just curious. @user463035818
– Hans Allen
Nov 20 at 13:19














2 Answers
2






active

oldest

votes


















7














Per [basic.start.main]/1, [basic.start.main]/2 and [over]/1:




A program shall contain a global function called main. [...] This function shall not be overloaded. [...] When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded [...].




This is then undefined behavior to have anything in the global namespace with the name main. There is then no reason for an implementation to mangle main or even consider it to be a proper function.






share|improve this answer





























    5














    Name mangling is a process used by C++ compilers to give each function in your program a unique name. In C++, generally programs have at-least a few functions with the same name ie function overloading, however Main is special, its actually a global function of the C language that can never be overloaded, so it is not necessary.



    Take a look at https://en.cppreference.com/w/cpp/language/main_function



    And questions about name mangling in C++



    (Main) special properties




    It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace (since C++17))







    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393585%2fwhy-name-mangling-has-no-effect-on-main-function-in-c%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      7














      Per [basic.start.main]/1, [basic.start.main]/2 and [over]/1:




      A program shall contain a global function called main. [...] This function shall not be overloaded. [...] When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded [...].




      This is then undefined behavior to have anything in the global namespace with the name main. There is then no reason for an implementation to mangle main or even consider it to be a proper function.






      share|improve this answer


























        7














        Per [basic.start.main]/1, [basic.start.main]/2 and [over]/1:




        A program shall contain a global function called main. [...] This function shall not be overloaded. [...] When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded [...].




        This is then undefined behavior to have anything in the global namespace with the name main. There is then no reason for an implementation to mangle main or even consider it to be a proper function.






        share|improve this answer
























          7












          7








          7






          Per [basic.start.main]/1, [basic.start.main]/2 and [over]/1:




          A program shall contain a global function called main. [...] This function shall not be overloaded. [...] When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded [...].




          This is then undefined behavior to have anything in the global namespace with the name main. There is then no reason for an implementation to mangle main or even consider it to be a proper function.






          share|improve this answer












          Per [basic.start.main]/1, [basic.start.main]/2 and [over]/1:




          A program shall contain a global function called main. [...] This function shall not be overloaded. [...] When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded [...].




          This is then undefined behavior to have anything in the global namespace with the name main. There is then no reason for an implementation to mangle main or even consider it to be a proper function.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 13:11









          YSC

          20.4k34594




          20.4k34594

























              5














              Name mangling is a process used by C++ compilers to give each function in your program a unique name. In C++, generally programs have at-least a few functions with the same name ie function overloading, however Main is special, its actually a global function of the C language that can never be overloaded, so it is not necessary.



              Take a look at https://en.cppreference.com/w/cpp/language/main_function



              And questions about name mangling in C++



              (Main) special properties




              It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace (since C++17))







              share|improve this answer




























                5














                Name mangling is a process used by C++ compilers to give each function in your program a unique name. In C++, generally programs have at-least a few functions with the same name ie function overloading, however Main is special, its actually a global function of the C language that can never be overloaded, so it is not necessary.



                Take a look at https://en.cppreference.com/w/cpp/language/main_function



                And questions about name mangling in C++



                (Main) special properties




                It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace (since C++17))







                share|improve this answer


























                  5












                  5








                  5






                  Name mangling is a process used by C++ compilers to give each function in your program a unique name. In C++, generally programs have at-least a few functions with the same name ie function overloading, however Main is special, its actually a global function of the C language that can never be overloaded, so it is not necessary.



                  Take a look at https://en.cppreference.com/w/cpp/language/main_function



                  And questions about name mangling in C++



                  (Main) special properties




                  It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace (since C++17))







                  share|improve this answer














                  Name mangling is a process used by C++ compilers to give each function in your program a unique name. In C++, generally programs have at-least a few functions with the same name ie function overloading, however Main is special, its actually a global function of the C language that can never be overloaded, so it is not necessary.



                  Take a look at https://en.cppreference.com/w/cpp/language/main_function



                  And questions about name mangling in C++



                  (Main) special properties




                  It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace (since C++17))








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 at 13:14

























                  answered Nov 20 at 13:12









                  RyanN1220

                  788




                  788






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393585%2fwhy-name-mangling-has-no-effect-on-main-function-in-c%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin