in c++ main function is the entry point to program how i can change it to an other function?

前端 未结 13 1889
忘了有多久
忘了有多久 2020-12-03 01:12

I was asked an interview question to change the entry point of a C or C++ program from main() to any other function. How is it possible?

13条回答
  •  清歌不尽
    2020-12-03 01:43

    The entry point is actually the _start function (implemented in crt1.o) .

    The _start function prepares the command line arguments and then calls main(int argc,char* argv[], char* env[]), you can change the entry point from _start to mystart by setting a linker parameter:

    g++ file.o -Wl,-emystart -o runme
    

    Of course, this is a replacement for the entry point _start so you won't get the command line arguments:

    void mystart(){
    
    }
    

    Note that global/static variables that have constructors or destructors must be initialized at the beginning of the application and destroyed at the end. Keep that in mind if you are planning on bypassing the default entry point which does it automatically.

提交回复
热议问题