Author Topic: Automatic initialisation  (Read 850 times)

0 Members and 1 Guest are viewing this topic.

Offline WebbotTopic starter

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,150
  • Helpful? 109
Automatic initialisation
« on: January 27, 2010, 09:55:46 PM »
Several folk have asked me how WebbotLib manages to initialise things like the a2d and i2c code when the initialisation code is never called from anywhere.

So I'll let you into a little tip.

There are some attributes you can set on a given function which means that it automatically gets added into the C startup code and is called before you even get to the 'main' function. Here is an example

Code: [Select]
void __attribute__ ((constructor)) myfunc(void){
     ... do stuff ...
}

This function will get called automatically during power up.

But beware - if you have more than one of these then you cannot control the order in which they get called. So for WebbotLib thats fine as the I2C and a2d modules are not reliant upon each other and can be initialized in any order.

Also this is a non-standard C addition. It works with AVR gcc but don't expect it to work with say Microsoft products on your pc code.

But great for saving on all those 'init' function calls that you forget to add in.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here

data_list