How to change the entry point main() in c langauge

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshgupta024
    New Member
    • Jun 2008
    • 14

    How to change the entry point main() in c langauge

    Can anyone give me the solution, for changing the entry point instead of main function.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by maheshgupta024
    Can anyone give me the solution, for changing the entry point instead of main function.
    And why would you want to do that?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Check your linker options. There is usually an option where you can specify the entry point function. main() is just the default.

      If you are using Visual Studio.NET you select project properties->Configuratio n Properties->Linker->Advanced and on the right pane of the dialog is a place for the name of the entry point function.

      Comment

      • maheshgupta024
        New Member
        • Jun 2008
        • 14

        #4
        Originally posted by weaknessforcats
        Check your linker options. There is usually an option where you can specify the entry point function. main() is just the default.

        If you are using Visual Studio.NET you select project properties->Configuratio n Properties->Linker->Advanced and on the right pane of the dialog is a place for the name of the entry point function.
        sorry for delayed reply.. Im working as developer, we develop applications in Linux OS, so i want to know in linux

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          For gcc try the '-e <symbol>' flag to make your program start at <symbol>
          instead of the default 'main' entry point.

          kind regards,

          Jos

          Comment

          • AmberJain
            Recognized Expert Contributor
            • Jan 2008
            • 922

            #6
            OK, I went thorugh the complete thread but there are some doubts---->

            1. Why would one like to do something like this? (could not find appropriate explanation about this on GOOGLE) What are it's practical uses?


            2. Even if it's possible with some practical uses, this contradicts the standard i.e.
            According to Ritchie and Kernigham's "The C programming language" book (page10-second edition-ANSI C)-
            "........yo ur program begins executing at the beginning of main. This means that every program must have a main somewhere"

            So if program begins executin at main, how's this possible? Doesn't it contradicts the standard?

            _NOTE_I may be wrong here as I am a newbie
            PLUS I don't own a latest edition of The C programming language BOOK by Dennis Ritchie and Brian Kernigham
            PLUS I have just started reading the above mentioned book for the first time.
            So please guide me....


            THANKS TO EVERY ONE IN ADVANCE........ .......

            ============
            AmbrNewlearner
            ============

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              K&R C hasn't been standard for nearly 20 years now, though it's still a great book. As a beginner, yes, everything starts with main. This is because when you begin execution of a compiled C program, there is a function that calls main. By changing the entry point, you change what function that calls (I think). I'm not entirely sure why it would be useful, but I'm sure there are uses.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Only use I could come up with is to create "different" programs from the same source code.
                For example I suppose you could compile with "DebugMain" as your entry point to perform lots of other little things or something? And then compile with "ReleaseMai n" as entry point when you are satisfied.
                A quick change in the tool chain could do these things automatically, perhaps easier for developing?

                Comment

                • oler1s
                  Recognized Expert Contributor
                  • Aug 2007
                  • 671

                  #9
                  Why would one like to do something like this?
                  Writing a non-standard program. An example would be a Windows program, which uses WinMain, not main. Another would be the Boost.Test main for testing.

                  Even if it's possible with some practical uses, this contradicts the standard
                  Yep. The implications of writing OS specific or API specific programs.

                  So if program begins executin at main
                  Technically, main is not the entry point. It’s compiler implemented. That compiler implementation calls main(). From a standards perspective, the compiler implementation is irrelevant.

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    Originally posted by oler1s
                    Technically, main is not the entry point. It’s compiler implemented. That compiler implementation calls main(). From a standards perspective, the compiler implementation is irrelevant.
                    This becomes very obvious on some embedded platforms like the one I am using now.

                    The processor has a start-up mechanism as follows

                    1. Jump to the location stored in program address 0
                    2. Execute from there

                    This code all has to be implemented in assembler and includes initialising the C data segments as required as well as other platform related stuff like initialising the memory management unit.

                    A default implementation of this code is provided with the tool chain and the last thing it does is
                    bsr $main

                    If you don't know bsr is the assembler directive to branch to subroutine, basically start running from main. It could be anything though.

                    All programs written in C have this sort of start-up code in them somewhere, something has to initialise the C data segments (stack, heap, global data etc). But on some platforms, PCs for instance this is hidden from you by the tool chain. On many embedded platforms this is available for editing but a default implementation is provided.

                    Comment

                    • maheshgupta024
                      New Member
                      • Jun 2008
                      • 14

                      #11
                      Originally posted by JosAH
                      For gcc try the '-e <symbol>' flag to make your program start at <symbol>
                      instead of the default 'main' entry point.

                      kind regards,

                      Jos
                      This is the test program I have written and compiled with
                      $>cc -e test test.c -o test
                      #include <stdio.h>

                      int test(void)
                      {
                      printf("Hai\n") ;
                      main();
                      return 1;
                      }


                      main()
                      {
                      printf("OK\n");
                      }
                      The output is as follows :
                      Hai
                      OK
                      Segmentation fault

                      Can anyone tell what's the reason for the Segmentation fault

                      Thanks in advance

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        you have not declared the return type of main

                        [code=c]
                        int main()
                        {
                        }
                        [/code]

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by Banfa
                          you have not declared the return type of main

                          [code=c]
                          int main()
                          {
                          }
                          [/code]
                          That's not it: the -e flag changes the address of the startup code. Startup code
                          has the responsibility of returning to the OS again so it can perform its process
                          cleanup code and other bookkeeping stuff. The test() function simply returns;
                          but there is no place to return to and hence the segmentation fault.

                          That function should call the exit() function instead; maybe that'll work ...

                          kind regards,

                          Jos

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            An exit status should do the trick.
                            @OP You'd need to include stdlib for that of course.

                            Comment

                            • AmberJain
                              Recognized Expert Contributor
                              • Jan 2008
                              • 922

                              #15
                              Originally posted by JosAH
                              For gcc try the '-e <symbol>' flag to make your program start at <symbol>
                              instead of the default 'main' entry point.

                              kind regards,

                              Jos
                              I'm using Bloodshed Dev c++ 4.9.9.2 with Mingw port of GCC (GNU Compiler Collection) as its compiler.

                              Well, I wrote a simple code in C. It is given below--->
                              [CODE=C]#include <stdio.h>

                              int test(void);

                              int main()
                              {
                              int a=1;
                              printf("\nHELLO main %d",a);
                              printf("\n\nPRE SS ANY KEY TO EXIT");
                              getch();
                              return(0);
                              }

                              int test(void)
                              {
                              int val;
                              printf("\nHELLO test()");
                              val=main();
                              return(0);
                              }
                              [/CODE]

                              Now, what I want is that test() must be entry point instead of main(). What should I do?

                              THANKS IN ADVANCE........ .....

                              ============
                              AmbrNewlearner
                              ============

                              Comment

                              Working...