Run some part of code before int main

Hi guys ,
How to run some part of program before int main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 #include <iostream>
 using namespace std;
 void startUp()
 {
   cout << "I am running before main" << endl;
 }
 void clean()
 {
  cout << "I am running after main" << endl;
 }

 int main()
 {
   cout << "I am inside main" << endl;
   return 0;
 }


I want ouput as :
I am running before main
I am inside main
I am inside main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <cstdlib>

// All non-local variables with static storage duration are initialized as part of program startup,
// before the execution of the main function begins (unless deferred)
// http://en.cppreference.com/w/cpp/language/initialization#Non-local_variables
static auto& cout_alias = std::cout << "I am running before main\n" ;

void run_this_at_the_end() { std::cout << "I am running after main\n" ; }

struct A
{
    A() { std::cout << "this is A::constructor\n" ; }
    ~A() { std::cout << "this is A::destructor\n" ; }
};

static const A a ; // destroyed after the last statement in main is executed

int main()
{
    // It is implementation-defined whether dynamic initialization happens-before the first statement
    // of the main function (for statics) ... or deferred to happen after.
    // If the initialization is deferred to happen after the first statement of main/thread function,
    // it happens before the first odr-use of any variable with static/thread storage duration
    // defined in the same translation unit as the variable to be initialized.
    // http://en.cppreference.com/w/cpp/language/initialization#Non-local_variables

    const A* unused = std::addressof(a) ; // odr-use of a
    cout_alias << "-------------\nentered main()\n" ; // odr-use of cout_alias

    // http://en.cppreference.com/w/cpp/utility/program/atexit
    std::atexit( run_this_at_the_end ) ;

    std::cout << "about to exit main()\n-----------------\n" ;
}

http://coliru.stacked-crooked.com/a/a36ab9064d4ec191
Hi JLBorges
Thank you very much But I didn't understood static auto& cout_alias . i.e is cout_alias is inbuilt variable ? Please never mind if it is silly question I am still learning C++
1 : Why did you used static const ?
Also can you please explain below code segment
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
void start()__attribute__((constructor));
void stop()__attribute__((destructor));

void start()
{
	cout << "Start " <<endl;
}
void stop()
{
	cout << "stop"<< endl;
}
int main()
{
	cout << "Inside main" << endl;
	return 0;
}

Above code is working fine as I searched in internet but no body explained properly how it works .
Last edited on
I would have it as following
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;


int start()
{
	cout << "Start " <<endl;
	return 1;
}
int stop()
{
	cout << "stop"<< endl;
	return 1;
}
int main()
{
	start();
	cout << "Inside main" << endl;
	stop();
	return 0;
}
Last edited on
@crazyjoshua27 But that doesn't achieve what the OP wants - which is to execute code before main() starts executing.
> But I didn't understood static auto& cout_alias . i.e is cout_alias is inbuilt variable ?

No, it is just a variable name like any other name. We could have instead written:
1
2
// static auto& cout_alias = std::cout << "I am running before main\n" ;
static std::ostream& a_name = std::cout << "I am running before main\n" ;


and then, in main: a_name << "-------------\nentered main()\n" ; // odr-use of a_name


> Why did you used static const ?

In static const A a ;

the const-qualifier specifies that the object is not modifiable.

In this context, static specifies internal linkage ie. the name a is not programatically visible in other translation units. (It is superfluous here; the default linkage of a const-qualified, but not volatile-qualified variable is internal.)

A a ; would have sufficed for the point I was attempting to make.
static const came out of force of habit; instinctively, I limit the visibility of names and the mutability of objects.


> __attribute__((constructor))
> __attribute__((destructor))

Non-standard GNU extensions which are often useful in C, but completely unnecessary in C++
The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
Topic archived. No new replies allowed.