How to emulate a constructor or static block in C

I am improving the tool. Please note that this tool will be associated with a test program that will have a function main( ), so my tool cannot be the main one. This tool has a number of functions that the test program will use.

Now, in addition, I want to add a timer to this tool. The idea is this: when a test program is connected to this tool and starts, the timer should start automatically.

If it were C ++, I would create a class with a constructor, so whenever a class loads, the constructor is called first, and I can initialize my timer inside the constructor.

If it was Java, I would just create a global static block and put the timer code inside the static block.

But my tool is purely in C on Linux, so how can I achieve this?

Please help me.

+3
source share
2 answers

It also looks like this:

How to get GCC __attribute__ ((constructor)) to work on OSX?

From the GCC docs:

Constructor
destructor
constructor (priority)
destructor (priority)

, main(). , , main() (). : , .

+4

crt*.o, main() .

+2

All Articles