Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Executing program with main() function
3.
Purpose of the _start() function
4.
Purpose of the _libc_start_main() function
5.
Executing program without main() function
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Execute main function in C++

Author Nagendra
0 upvote

Introduction

C++ is one of the most widely used programming languages for competitive programming. Learning C++ reinforces essential computer science principles while also providing access to a wide range of professional options.

The main() function is the program's entry point in C++ programming. However, from the standpoint of program execution, it is not. Calls to a few more functions are made before the execution flow reaches main(), which establishes arguments, prepares environment variables for program execution, and so on.

Executing program with main() function

An Executable and Linkable Format (ELF) file is the executable file created after compiling C source code. Every ELF file has an ELF header with an e entry field that includes the program memory location where the executable will begin to run. The _start() function is located at this memory address. The loader checks for the e_entry field in the ELF file header after loading the application. ELF (Executable and Linkable Format) is a standard file format for executable files, object code, shared libraries, and core dumps in UNIX systems.

Example :

Consider the following program saved with the file named ninja.cpp

int main()
{
   return(0);
}
You can also try this code with Online C++ Compiler
Run Code

Now run the following instructions to compile it.

gcc -o ninja.cpp
You can also try this code with Online C++ Compiler
Run Code

Now that an example executable has been produced, we may analyse it with the objdump utility.

objdump -f ninja.exe
You can also try this code with Online C++ Compiler
Run Code

On my PC, this outputs the following crucial information about the program. Look at the start address in the example below; this is the address that points to the _start() function.

ninja.exe: file format pei-i386
architecture: i386, flags 0x0000013a:
EXEC_P, HAS_DEBUG, HAS_SYMS, HAS_LOCALS, D_PAGED
start address 0x004012e0
You can also try this code with Online C++ Compiler
Run Code

Purpose of the _start() function

The input inputs for the next function, _libc start main(), are prepared by the _start() function. This is the _libc start main() function's prototype. The arguments supplied by the _start() function can be seen here.

int __libc_start_main(int (*main) (int, char **, char **), /*This is the address of main function*/
int argc, /*It contains number of command line args*/
char ** ubp_av, /* The command line arg array*/
void (*init) (void), /* It contains address of init function*/
void (*fini) (void), /* address of fini function*/
void (*rtld_fini) (void), /* address of dynamic linker fini function is loaded here*/
void (* stack_end) /*The end of the stack address*/
);
You can also try this code with Online C++ Compiler
Run Code

Also Read - C++ Interview Questions

Purpose of the _libc_start_main() function

The function _libc_start_main() serves the following purposes:

  • Getting the environment variables ready for the program's execution
  • The _init() method is called before the main() function is started, and it performs initialization.
  • After the programme is finished, use the _fini() and _rtld_fini() routines to clean up.

_libc start main() calls the main() function when all of the prerequisite activities have been accomplished.

Also readDecimal to Binary c++

Executing program without main() function

To be clear, main() is simply a standard word for startup code. Startup code can be given any name; it is not required to be called "main." Because the _start() function calls main() by default, we must update it if we wish to run our own startup code. We can call our custom startup code instead of main by overriding the _start() function(). Consider the following program saved with the file named ninja.cpp

#include<stdio.h>
#include<stdlib.h>
void _start()
{
    int x = Coding_Ninjas(); //User-Defined main function
    exit(x);
}

int Coding_Ninjas() // User-Defined main function
{
    printf("Happy Coding!!");
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Now we have to inform the compiler not to utilise its own _start implementation (). We can achieve this with GCC's -nostartfiles option.

gcc -nostartfiles -o ninja ninja.cpp

Executing the executable file

./ninja

Output:

Happy Coding!!

 

You can try by yourself with the help of online C++ Compiler .

Also check out this article - Pair in C++

FAQs

  1. Can you execute a program without the main() function in C++? 
    Yes, a program can be executed without the main() function in C++.
     
  2. What is the purpose of the _start() function?
    The input inputs for the next function, _libc start main(), are prepared by the _start() function.
     
  3. What is the purpose of the _libc_start_main() function?
    The purpose of the _libc_start_main() function varies from setting up the environment variable , calling the _init() method and use of _fini() and _rtld_fini() routines to clean up.

Key Takeaways

In this article, we have extensively discussed executing the main function in C++. The article explains the details about the main() function in C++ and executing programs without the main() function. The major roles of _start() function and _libc_start_main() are also explained with suitable examples.
We hope that this blog has helped you enhance your knowledge regarding the main() function in C++ and if you would like to learn more, check out our articles on C++. Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass