user2990606
user2990606

Reputation:

How to call a static function

I'm trying to call the following function

In daydream.c :

.....
static int create_new_account(void)
{
    DDPut(sd[newucstr]);
    switch (HotKey(HOT_NOYES)) {
    case 1:
            if (CreateNewAccount()) {
                    clog.cl_userid = user.user_account_id;
                    clog.cl_firstcall = user.user_firstcall;
                    clog.cl_logon = time(0);
                    if (user.user_connections == 0)
                            clog.cl_flags |= CL_NEWUSER;
                    clog.cl_bpsrate = bpsrate;

                    getin();
                    return 1;
            }
            return 0;
    case 2:
            DDPut("\n");
            return 0;
    default:
            return 1;
    }
}

From matrix.c:

int apply()
{
create_new_account();
}

However, it won't compile link:

matrix.o: In function `apply':
matrix.c:(.text+0xf0): undefined reference to `create_new_account'
collect2: error: ld returned 1 exit status
make: *** [daydream] Error 1

So, my question is, how do I call this function properly?

Upvotes: 1

Views: 115

Answers (1)

Fiddling Bits
Fiddling Bits

Reputation: 8861

You must not know what the keyword static means. static keeps create_new_account global to daydream.c only. Nobody else can access it. If you want other functions to access it:

  1. Remove the keyword static and extern this function into matrix.c.

    Option 1:

    /* daydream.c */
    int create_new_account(void)
    {
        ...
    }
    
    /* matrix.c */
    extern create_new_account(void);
    

    Option 2:

    /* daydream.c */
    int create_new_account(void)
    {
        ....
    }
    
    /* daydream.h */
    extern int create_new_account(void);
    
    /* matrix.c */
    #include "daydream.h"
    
  2. Create a external wrapper function to call that in turn calls your static function.

    /* daydream.c */
    
    static int create_new_account(void)
    {
        ...
    }
    
    int create_new_account_wrapper(void)
    {
        return create_new_account();
    }
    
    /* matrix.c */
    int apply(void)
    {
        return create_new_account_wrapper();
    }
    

My preference is answer 1, option 2.

Upvotes: 4

Related Questions