This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to make a auto-call function when create a 51 library

I create a library, which needs some initialization. Currently i just provide a function mylib_init, and tells the users of my lib, call it before use other functions of my lib. but it seems somewhat stupid. so, is there any way to make mylib_init to be called automatically when my lib is used?

  • char mylib_initialized;
    
    void mylib_initialize() {
        if (!mylib_initialized) {
            ...
            mylib_initialized = TRUE;
        }
    }
    
    int mylib_do_something() {
        mylib_initialize();
        ...
    }
    
    int mylib_do_something_else() {
        mylib_initialize();
        ...
    }
    

    Or inline the test of mylib_initialized before the call - extra cheap if using a bit variable:

    int mylib_do_something() {
        if (!mylib_initialized) mylib_initialize();
        ...
    }